对于表结构进中二进制值进行修正,避免建表时语法错误

This commit is contained in:
hanxuanyu 2024-03-28 13:54:59 +08:00
parent 4d8fe412d2
commit 0e5c49b138
1 changed files with 15 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import List
import logging
import re
# 默认配置文件路径
DEFAULT_CONFIG_PATH = "config.yaml"
@ -111,13 +112,26 @@ class DBMigrator:
with source_db.cursor() as source_cursor, target_db.cursor() as target_cursor:
source_cursor.execute(f"SHOW CREATE TABLE {table};")
create_table_sql = source_cursor.fetchone()[1]
target_cursor.execute(create_table_sql)
# 检查并修正BIT(1)类型字段的默认值表示
corrected_sql = self.correct_bit_default_value(create_table_sql)
target_cursor.execute(corrected_sql)
target_db.commit()
return True
except Exception as e:
logging.error(f"Error copying table structure for {table}: {e}")
return False
def correct_bit_default_value(self, sql):
"""
检查并修正BIT(1)类型字段的默认值表示
将错误的表示'0'/'1'转换为正确的二进制表示b'0'/b'1'
"""
# 使用正则表达式匹配并修正BIT(1)类型字段的默认值表示
corrected_sql = re.sub(r"BIT$1$( NOT NULL)? DEFAULT '([01])'", r"BIT(1)\1 DEFAULT b'\2'", sql)
return corrected_sql
def migrate_table_data(self, table):
# 为每个表迁移数据
try: