Skip to content

Instantly share code, notes, and snippets.

@jduhls
Last active May 6, 2016 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jduhls/dd77f7dfea879766d01ee60279ab0c3e to your computer and use it in GitHub Desktop.
Save jduhls/dd77f7dfea879766d01ee60279ab0c3e to your computer and use it in GitHub Desktop.
Convert an entire database to utf8mb4 (credit: http://stackoverflow.com/a/24391682)
delimiter //
DROP PROCEDURE IF EXISTS convert_database_to_utf8mb4 //
CREATE PROCEDURE convert_database_to_utf8mb4()
BEGIN
DECLARE table_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
SELECT t.table_name FROM information_schema.tables t WHERE t.table_schema = DATABASE() AND t.table_type='BASE TABLE';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
tables_loop: LOOP
FETCH cur INTO table_name;
IF done THEN
LEAVE tables_loop;
END IF;
SET @sql = CONCAT("ALTER TABLE ", table_name, " CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
END LOOP;
CLOSE cur;
END //
delimiter ;
call convert_database_to_utf8mb4();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment