-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate table names with uppercase to lowercase names (macOS, Windows)
For case sensitive filesystems which are typically used by Linux such renames were already done in earlier migration steps, but those steps have no effect on macOS and Windows. The new SQL migration code renames any table name with uppercase letters to a lowercase table name. The script was created by Claude.AI. Fixes: #6139 Signed-off-by: Stefan Weil <[email protected]>
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Kitodo-DataManagement/src/main/resources/db/migration/V2_137__Use_lowercase_table_names.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
-- | ||
-- (c) Kitodo. Key to digital objects e. V. <[email protected]> | ||
-- | ||
-- This file is part of the Kitodo project. | ||
-- | ||
-- It is licensed under GNU General Public License version 3 or later. | ||
-- | ||
|
||
-- This SQL script renames all tables to lowercase. | ||
-- This is required for MariaDB on macOS or Windows because these | ||
-- operating systems use case insensitive filesystems. | ||
|
||
-- Table renames on such filesystems must use an intermediate table name | ||
-- because a direct `RENAME TABLE A TO a;` does not work. | ||
|
||
-- First, create a procedure to handle the renames | ||
DELIMITER // | ||
|
||
CREATE PROCEDURE rename_tables_to_lowercase() | ||
BEGIN | ||
DECLARE done INT DEFAULT FALSE; | ||
DECLARE table_name_var VARCHAR(255); | ||
|
||
-- Cursor for all tables with uppercase characters | ||
DECLARE table_cursor CURSOR FOR | ||
SELECT TABLE_NAME | ||
FROM INFORMATION_SCHEMA.TABLES | ||
WHERE TABLE_SCHEMA = DATABASE() | ||
AND TABLE_NAME != LOWER(TABLE_NAME); | ||
|
||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; | ||
|
||
OPEN table_cursor; | ||
|
||
rename_loop: LOOP | ||
FETCH table_cursor INTO table_name_var; | ||
IF done THEN | ||
LEAVE rename_loop; | ||
END IF; | ||
|
||
-- Execute renames using dynamic SQL | ||
SET @temp_name = CONCAT(LOWER(table_name_var), '_temp'); | ||
SET @rename1 = CONCAT('RENAME TABLE `', table_name_var, '` TO `', @temp_name, '`'); | ||
SET @rename2 = CONCAT('RENAME TABLE `', @temp_name, '` TO `', LOWER(table_name_var), '`'); | ||
|
||
PREPARE stmt1 FROM @rename1; | ||
EXECUTE stmt1; | ||
DEALLOCATE PREPARE stmt1; | ||
|
||
PREPARE stmt2 FROM @rename2; | ||
EXECUTE stmt2; | ||
DEALLOCATE PREPARE stmt2; | ||
|
||
END LOOP; | ||
|
||
CLOSE table_cursor; | ||
END // | ||
|
||
DELIMITER ; | ||
|
||
-- Execute the procedure | ||
CALL rename_tables_to_lowercase(); | ||
|
||
-- Clean up | ||
DROP PROCEDURE rename_tables_to_lowercase; |