first commit
This commit is contained in:
83
init_full_sync.py
Normal file
83
init_full_sync.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# init_full_sync.py
|
||||
import pyodbc
|
||||
import config
|
||||
import db_utils
|
||||
import time
|
||||
import os
|
||||
|
||||
def run_full_sync():
|
||||
print("=== 开始全量初始化同步 ===")
|
||||
print("注意:将基于 config.SYNC_MAPPING 中的文件路径进行处理")
|
||||
|
||||
sql_conn = db_utils.get_sql_conn()
|
||||
sql_cursor = sql_conn.cursor()
|
||||
sql_cursor.fast_executemany = True
|
||||
|
||||
# 第一层循环:遍历配置文件中的所有文件路径
|
||||
for acc_path, tables_map in config.SYNC_MAPPING.items():
|
||||
if not os.path.exists(acc_path):
|
||||
print(f"⚠️ [跳过] 文件不存在: {acc_path}")
|
||||
continue
|
||||
|
||||
print(f"\n📂 正在处理文件: {acc_path}")
|
||||
|
||||
try:
|
||||
acc_conn = db_utils.get_access_conn(acc_path)
|
||||
acc_cursor = acc_conn.cursor()
|
||||
|
||||
# 第二层循环:遍历该文件下的所有表映射
|
||||
for acc_table, target_config in tables_map.items():
|
||||
target_schema = target_config['target_schema']
|
||||
target_table = target_config['target_table']
|
||||
full_target_name = db_utils.fmt_table(target_schema, target_table)
|
||||
|
||||
print(f" 👉 同步表: [{acc_table}] -> {full_target_name}")
|
||||
|
||||
try:
|
||||
# 1. 检查 Access 表是否存在
|
||||
acc_cursor.execute(f"SELECT TOP 1 * FROM [{acc_table}]")
|
||||
|
||||
# 2. 获取列结构
|
||||
columns = [col[0] for col in acc_cursor.description]
|
||||
insert_sql = db_utils.generate_insert_sql(target_schema, target_table, columns)
|
||||
|
||||
# 3. TRUNCATE 目标表
|
||||
sql_cursor.execute(f"TRUNCATE TABLE {full_target_name}")
|
||||
|
||||
# 4. 开启 IDENTITY_INSERT (尝试)
|
||||
try: sql_cursor.execute(f"SET IDENTITY_INSERT {full_target_name} ON")
|
||||
except: pass
|
||||
|
||||
# 5. 传输数据
|
||||
print(f" 正在读取并写入数据...")
|
||||
acc_cursor.execute(f"SELECT * FROM [{acc_table}]")
|
||||
|
||||
total_rows = 0
|
||||
while True:
|
||||
rows = acc_cursor.fetchmany(config.BATCH_SIZE)
|
||||
if not rows: break
|
||||
sql_cursor.executemany(insert_sql, rows)
|
||||
total_rows += len(rows)
|
||||
print(f" 已写入 {total_rows} 行...", end='\r')
|
||||
|
||||
# 6. 关闭 IDENTITY_INSERT
|
||||
try: sql_cursor.execute(f"SET IDENTITY_INSERT {full_target_name} OFF")
|
||||
except: pass
|
||||
|
||||
sql_conn.commit()
|
||||
print(f"\n ✅ 完成。")
|
||||
|
||||
except Exception as tbl_err:
|
||||
print(f" ❌ 表级错误: {tbl_err}")
|
||||
sql_conn.rollback()
|
||||
|
||||
acc_conn.close()
|
||||
|
||||
except Exception as file_err:
|
||||
print(f" ❌ 文件级错误: {file_err}")
|
||||
|
||||
sql_conn.close()
|
||||
print("\n=== 全量同步结束 ===")
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_full_sync()
|
||||
Reference in New Issue
Block a user