Skip to content

Commit

Permalink
Migrate table names with uppercase to lowercase names (macOS, Windows)
Browse files Browse the repository at this point in the history
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
stweil committed Dec 1, 2024
1 parent 256951c commit 2871640
Showing 1 changed file with 65 additions and 0 deletions.
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;

0 comments on commit 2871640

Please sign in to comment.