Skip to content

Instantly share code, notes, and snippets.

@jonespm
Created July 22, 2020 13:49
Convert database to utf8mb4 as admin
-- Fill in the name of your DB in @dbname then run the following as the admin user, should be all updated to utf8mb4!
-- mysql --silent < generateAlter.sql > alterTables.sql
-- mysql < alterTables.sql
SET @dbname = "myla_dev";
use information_schema;
-- Update DB to utf8mb4
SELECT "SET FOREIGN_KEY_CHECKS=0;";
SELECT concat("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like @dbname group by table_schema;
-- Update all tables to dynamic row format and utf8mb4
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name,"` ROW_FORMAT = DYNAMIC, CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `TABLES` where table_schema like @dbname group by table_schema, table_name;
-- Update all varchar to utf8mb4
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like @dbname and data_type in ('varchar');
-- Update all text to utf8mb4
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as _sql FROM `COLUMNS` where table_schema like @dbname and data_type in ('text','tinytext','mediumtext','longtext');
SELECT "SET FOREIGN_KEY_CHECKS=1;";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment