From 04e105c6ba4e478d7f99dce8dca16f43533f77d0 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 1 Jul 2026 12:12:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=BF=81=E7=A7=BB=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py/migrate_mysql8_to_mysql57.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/py/migrate_mysql8_to_mysql57.py b/py/migrate_mysql8_to_mysql57.py index a7a35b1..0c4dc81 100644 --- a/py/migrate_mysql8_to_mysql57.py +++ b/py/migrate_mysql8_to_mysql57.py @@ -55,6 +55,26 @@ def fix_collation(ddl: str) -> str: return ddl +def fix_text_blob_index(ddl: str) -> str: + """为 TEXT/BLOB 列在索引中添加前缀长度 (255),兼容 MySQL 5.7""" + text_cols = set() + for m in re.finditer(r'`(\w+)`\s+(?:tinytext|text|mediumtext|longtext|tinyblob|blob|mediumblob|longblob)\b', ddl, re.IGNORECASE): + text_cols.add(m.group(1)) + if not text_cols: + return ddl + + def _add_prefix(match: re.Match) -> str: + col = match.group(1) + rest = match.group(2) + if col in text_cols and not rest.strip().startswith('('): + return f'`{col}`(255){rest}' + return match.group(0) + + # 匹配 KEY 定义中的列引用: `col_name` 后面不跟 ( 的情况 + ddl = re.sub(r'`(\w+)`(\s*[,\)])', _add_prefix, ddl) + return ddl + + def get_all_tables(conn: pymysql.Connection) -> list[str]: with conn.cursor() as cur: cur.execute("SHOW TABLES") @@ -131,6 +151,7 @@ def migrate() -> int: row = cur.fetchone() ddl = row["Create Table"] ddl = fix_collation(ddl) + ddl = fix_text_blob_index(ddl) try: with tgt.cursor() as cur: