Skip to content

Instantly share code, notes, and snippets.

@boppy
Created September 22, 2018 20:10
Show Gist options
  • Save boppy/ca937057c9274cfefc9c0d2ea9d3c6f6 to your computer and use it in GitHub Desktop.
Save boppy/ca937057c9274cfefc9c0d2ea9d3c6f6 to your computer and use it in GitHub Desktop.

Update a Full MySQL/MariaDB Database to a different Charset and Collation

Don't forget to add username and pwd-prompt if needed (-uroot -p or something...)

Licence?

MIT if I had to name one:

Copyright 2018 Henning Bopp <henning.bopp@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

If you write a nice E-Mail to amazing-amy@superhardcore.org you might also licence as you wish... What about WTFPL, Beerware, or something...

#!/bin/bash
mysql -N <<EOSQL | mysql -N
-- CONFIG HERE --
SET @dbName = 'boppy_test';
SET @toCharset = 'utf8mb4';
SET @toCollation = 'utf8mb4_unicode_ci';
-- NO CONFIG THERE --
USE information_schema;
SELECT CONCAT("SELECT 'Update DB: ", TABLE_SCHEMA, "'; ALTER DATABASE \`", TABLE_SCHEMA, "\` CHARACTER SET = ", @toCharset ," COLLATE = ", @toCollation ,";") FROM \`TABLES\` WHERE TABLE_SCHEMA = @dbName;
SELECT CONCAT("SELECT 'Update Table: ", TABLE_NAME, "'; ALTER TABLE \`", TABLE_SCHEMA, "\`.\`", TABLE_NAME, "\` CONVERT TO CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM \`TABLES\` WHERE TABLE_SCHEMA = @dbName;
SELECT CONCAT("SELECT 'Update Field: ", TABLE_NAME, ".", COLUMN_NAME, "'; ALTER TABLE \`", TABLE_SCHEMA, "\`.\`", TABLE_NAME, "\` CHANGE \`", COLUMN_NAME, "\` \`", COLUMN_NAME, "\` ", COLUMN_TYPE, " CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM COLUMNS WHERE TABLE_SCHEMA = @dbName AND (CHARACTER_SET_NAME IS NOT NULL OR COLLATION_NAME IS NOT NULL);
SELECT "SELECT 'kthnxbye.';";
-- END --
EOSQL
-- CONFIG HERE --
SET @dbName = 'boppy_test';
SET @toCharset = 'utf8mb4';
SET @toCollation = 'utf8mb4_unicode_ci';
-- NO CONFIG THERE --
USE information_schema;
SELECT CONCAT("ALTER DATABASE `", TABLE_SCHEMA, "` CHARACTER SET = ", @toCharset ," COLLATE = ", @toCollation ,";") FROM `TABLES` WHERE TABLE_SCHEMA = @dbName
UNION SELECT CONCAT("ALTER TABLE `", TABLE_SCHEMA, "`.`", TABLE_NAME, "` CONVERT TO CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM `TABLES` WHERE TABLE_SCHEMA = @dbName
UNION SELECT CONCAT("ALTER TABLE `", TABLE_SCHEMA, "`.`", TABLE_NAME, "` CHANGE `", COLUMN_NAME, "` `", COLUMN_NAME, "` ", COLUMN_TYPE, " CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM COLUMNS WHERE TABLE_SCHEMA = @dbName AND (CHARACTER_SET_NAME IS NOT NULL OR COLLATION_NAME IS NOT NULL)
-- END --
;
#!/bin/bash
mysql -N <<EOSQL
-- CONFIG HERE --
SET @dbName = 'boppy_test';
SET @toCharset = 'utf8mb4';
SET @toCollation = 'utf8mb4_unicode_ci';
-- NO CONFIG THERE --
USE information_schema;
SELECT CONCAT("ALTER DATABASE \`", TABLE_SCHEMA, "\` CHARACTER SET = ", @toCharset ," COLLATE = ", @toCollation ,";") FROM \`TABLES\` WHERE TABLE_SCHEMA = @dbName
UNION SELECT CONCAT("ALTER TABLE \`", TABLE_SCHEMA, "\`.\`", TABLE_NAME, "\` CONVERT TO CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM \`TABLES\` WHERE TABLE_SCHEMA = @dbName
UNION SELECT CONCAT("ALTER TABLE \`", TABLE_SCHEMA, "\`.\`", TABLE_NAME, "\` CHANGE \`", COLUMN_NAME, "\` \`", COLUMN_NAME, "\` ", COLUMN_TYPE, " CHARACTER SET ", @toCharset ," COLLATE ", @toCollation ,";") FROM COLUMNS WHERE TABLE_SCHEMA = @dbName AND (CHARACTER_SET_NAME IS NOT NULL OR COLLATION_NAME IS NOT NULL)
-- END --
;
EOSQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment