From e62e0cf076df90d05f164930ad41dd8a4d0d984f Mon Sep 17 00:00:00 2001 From: Akumatic Date: Mon, 8 May 2023 10:39:50 +0200 Subject: [PATCH] Change order of function calls, fix conversion of columns --- convert.py | 2 +- convert/utf8mb4converter.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/convert.py b/convert.py index d687338..3c9a6db 100644 --- a/convert.py +++ b/convert.py @@ -32,8 +32,8 @@ def main ( logging.getLogger("Main").info(f"Database statistics:\n{stats}") else: db.convert_charset_db() - db.convert_charset_all_tables() db.convert_charset_all_columns_all_tables() + db.convert_charset_all_tables() def parse_args ( ) -> argparse.Namespace: diff --git a/convert/utf8mb4converter.py b/convert/utf8mb4converter.py index 7e81e0d..ef19a8f 100644 --- a/convert/utf8mb4converter.py +++ b/convert/utf8mb4converter.py @@ -253,9 +253,9 @@ class UTF8MB4Converter: self, column: dict, table: str, - newtype: str = None, charset: str = DEFAULT_CHARSET, - collation: str = DEFAULT_COLLATION + collation: str = DEFAULT_COLLATION, + newtype: str = None ) -> None: """ Alters the charset and collation of a single column. @@ -283,17 +283,21 @@ class UTF8MB4Converter: if not column["charset"]: self.logger.debug(f"Column {col}(@{table}) has no default character set") return - if not column["type"] in ["char", "varchar", "text", "longtext"]: - self.logger.debug(f"Column {col}(@{table}) does contain data of type {column['type']}") - return if column["charset"] == charset: self.logger.debug(f"Column {col}(@{table}) already has character set {charset}") return + if column['nullable'] == "YES": + constraint = "NULL" + else: + constraint = "NOT NULL" + if column['dvalue'] is not None: + constraint += f" DEFAULT {column['dvalue']}" + query = " ".join(( f"ALTER TABLE {table} CHANGE {col} {col}", f"{newtype or column['ctype']} CHARACTER SET {charset} COLLATE {collation}", - "NULL" if column["nullable"] == "YES" else f"NOT NULL DEFAULT {column['dvalue']}" + constraint )) try: self.cur.execute(query)