-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add do command to change MySQL charset to utf8mb4 #1079
Changes from 3 commits
7dc81d9
c7090ba
89104e5
128958d
2a92c49
dfd5f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- 💥[Feature] Upgrade default charset and collation of mysql to utf8mb4 and utf8mb4_unicode_ci respectively (by @Danyal-Faheem) | ||
- Add do command to upgrade the charset and collation of tables in mysql. | ||
- The command will perform the following upgrades: | ||
- Upgrade all `utf8mb3` charset to `utf8mb4` | ||
- Upgrade collation `utf8mb3_general_ci` to `utf8mb4_unicode_ci` | ||
- Upgrade collation `utf8mb3_bin` to `utf8mb4_bin` | ||
- Upgrade collation `utf8mb3_*` to `utf8mb4_*` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
def get_mysql_change_charset_query( | ||
regisb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
database: str, | ||
charset: str, | ||
collation: str, | ||
query_to_append: str, | ||
charset_to_upgrade_from: str, | ||
) -> str: | ||
""" | ||
Helper function to generate the mysql query to upgrade the charset and collation of tables | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is not just tables, it is columns, tables and database. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
|
||
Utilized in the `tutor local do convert-mysql-utf8mb4-charset` command | ||
""" | ||
return f""" | ||
|
||
DROP PROCEDURE IF EXISTS UpdateColumns; | ||
DELIMITER $$ | ||
|
||
CREATE PROCEDURE UpdateColumns() | ||
BEGIN | ||
|
||
DECLARE done_columns_loop INT DEFAULT FALSE; | ||
DECLARE _table_name VARCHAR(255); | ||
DECLARE _table_name_copy VARCHAR(255) DEFAULT ""; | ||
DECLARE _column_name VARCHAR(255); | ||
DECLARE _column_type VARCHAR(255); | ||
DECLARE _collation_name VARCHAR(255); | ||
|
||
# We explicitly upgrade the utf8mb3_general_ci collations to utf8mb4_unicode_ci | ||
# The other collations are upgraded from utf8mb3_* to utf8mb4_* | ||
# For any other collation, we leave it as it is | ||
DECLARE columns_cur CURSOR FOR | ||
SELECT | ||
TABLE_NAME, | ||
COLUMN_NAME, | ||
COLUMN_TYPE, | ||
CASE | ||
WHEN COLLATION_NAME LIKE CONCAT('{charset_to_upgrade_from}', '_general_ci') THEN 'utf8mb4_unicode_ci' | ||
WHEN COLLATION_NAME LIKE CONCAT('{charset_to_upgrade_from}', '_%') THEN CONCAT('{charset}', SUBSTRING_INDEX(COLLATION_NAME, '{charset_to_upgrade_from}', -1)) | ||
ELSE COLLATION_NAME | ||
END AS COLLATION_NAME | ||
FROM | ||
INFORMATION_SCHEMA.COLUMNS | ||
WHERE | ||
TABLE_SCHEMA = '{database}' | ||
AND COLLATION_NAME IS NOT NULL {query_to_append}; | ||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_columns_loop = TRUE; | ||
OPEN columns_cur; | ||
columns_loop: LOOP | ||
FETCH columns_cur INTO _table_name, _column_name, _column_type, _collation_name; | ||
|
||
IF done_columns_loop THEN | ||
LEAVE columns_loop; | ||
END IF; | ||
|
||
# First, upgrade the default charset and collation of the table | ||
If _table_name <> _table_name_copy THEN | ||
select _table_name; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
SET @stmt = CONCAT('ALTER TABLE `', _table_name, '` CONVERT TO CHARACTER SET {charset} COLLATE {collation};'); | ||
PREPARE query FROM @stmt; | ||
EXECUTE query; | ||
DEALLOCATE PREPARE query; | ||
SET FOREIGN_KEY_CHECKS = 1; | ||
SET _table_name_copy = _table_name; | ||
END IF; | ||
|
||
# Then, upgrade the default charset and collation of each column | ||
# This sequence of table -> column is necessary to preserve column defaults | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
SET @statement = CONCAT('ALTER TABLE `', _table_name, '` MODIFY `', _column_name, '` ', _column_type,' CHARACTER SET {charset} COLLATE ', _collation_name, ';'); | ||
PREPARE query FROM @statement; | ||
EXECUTE query; | ||
DEALLOCATE PREPARE query; | ||
SET FOREIGN_KEY_CHECKS = 1; | ||
|
||
END LOOP; | ||
CLOSE columns_cur; | ||
|
||
END$$ | ||
|
||
DELIMITER ; | ||
|
||
DROP PROCEDURE IF EXISTS UpdateTables; | ||
DELIMITER $$ | ||
|
||
CREATE PROCEDURE UpdateTables() | ||
# To upgrade the default character set and collation of any tables that were skipped from the previous procedure | ||
BEGIN | ||
|
||
DECLARE done INT DEFAULT FALSE; | ||
DECLARE table_name_ VARCHAR(255); | ||
DECLARE cur CURSOR FOR | ||
SELECT table_name FROM information_schema.tables | ||
WHERE table_schema = '{database}' AND table_type = "BASE TABLE" AND table_collation not like 'utf8mb4_%' {query_to_append}; | ||
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; | ||
|
||
select table_name_; | ||
|
||
SET FOREIGN_KEY_CHECKS = 0; | ||
SET @stmt = CONCAT('ALTER TABLE `', table_name_, '` CONVERT TO CHARACTER SET {charset} COLLATE {collation};'); | ||
PREPARE query FROM @stmt; | ||
EXECUTE query; | ||
DEALLOCATE PREPARE query; | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; | ||
|
||
END LOOP; | ||
CLOSE cur; | ||
|
||
END$$ | ||
DELIMITER ; | ||
|
||
use {database}; | ||
ALTER DATABASE {database} CHARACTER SET {charset} COLLATE {collation}; | ||
CALL UpdateColumns(); | ||
CALL UpdateTables(); | ||
""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,10 @@ def upgrade_from_quince(context: click.Context, config: Config) -> None: | |
upgrade_mongodb(context, config, "5.0.26", "5.0") | ||
upgrade_mongodb(context, config, "6.0.14", "6.0") | ||
upgrade_mongodb(context, config, "7.0.7", "7.0") | ||
fmt.echo_alert( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this would be moved to upgrade from sumac section because we are targeting this for nightly and makes sense to have this there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean to upgrade to Sumac, right? I agree, but we don't have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, right. I meant upgrade_from_redwood section of sumac branch. I will take care of that in sumac branch once this branch is merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated this in sumac branch 2ad4be0 |
||
"""It is recommended to upgrade your character set and collation of the MySQL database after upgrading to Redwood. | ||
You can use the convert-mysql-utf8mb4-charset do job to upgrade the collation and character set. You can find more details regarding the command at https://docs.tutor.edly.io/local.html#changing-the-mysql-charset-and-collation""" | ||
) | ||
|
||
|
||
def upgrade_mongodb( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the usage of you/second person pronouns in the guide should be reduced/removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reworded the docs to reduce the usage.