fix: 修复数据迁移中的事务处理,确保每张表的迁移在独立事务中进行

This commit is contained in:
Windpicker-owo
2025-11-28 00:08:51 +08:00
parent 86e04638a2
commit c9c6a11593

View File

@@ -663,13 +663,14 @@ class DatabaseMigrator:
self._drop_target_tables() self._drop_target_tables()
# 开始迁移 # 开始迁移
with self.source_engine.connect() as source_conn, self.target_engine.connect() as target_conn: with self.source_engine.connect() as source_conn:
for source_table in tables: for source_table in tables:
try: try:
# 在目标库中创建表结构 # 在目标库中创建表结构
target_table = copy_table_structure(source_table, MetaData(), self.target_engine) target_table = copy_table_structure(source_table, MetaData(), self.target_engine)
# 迁移数据 # 每张表单独事务,避免退出上下文被自动回滚
with self.target_engine.begin() as target_conn:
migrated_rows, error_count = migrate_table_data( migrated_rows, error_count = migrate_table_data(
source_conn, source_conn,
target_conn, target_conn,