Files
zentao-flow/tmp/sync_us2461_to_test.py
T

74 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""200 → 161/zentao_dev_2026:补同步用户需求 2461(9197 的上游)。
源库只读;目标库纯 INSERT,前置检查无冲突。
"""
import io, sys, time, pymysql
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
SRC = dict(host='192.168.3.200', user='root', password='PX4fTAAsJ#T!1',
database='zentao_dev', charset='utf8mb4', connect_timeout=15)
DST = dict(host='192.168.1.161', user='devgps', password='dev@2021GPS',
database='zentao_dev_2026', charset='utf8mb4', connect_timeout=15, autocommit=False)
USID = 2461
def log(m):
print(f'[{time.strftime("%H:%M:%S")}] {m}', flush=True)
src = pymysql.connect(**SRC); scur = src.cursor()
dst = pymysql.connect(**DST); dcur = dst.cursor()
# 先摸清 200 上 2461 的 action/附件 情况(只读)
scur.execute("SELECT DISTINCT objectType FROM zt_action WHERE objectID=%s", (USID,))
print('200 zt_action objectID=2461 的 objectType:', [r[0] for r in scur.fetchall()])
scur.execute("SELECT COUNT(*) FROM zt_file WHERE objectID=%s AND objectType IN ('userstory','storyUser','story_user')", (USID,))
print('200 2461 附件数:', scur.fetchone()[0])
def cols_of(cur, t):
cur.execute("""SELECT column_name FROM information_schema.columns
WHERE table_schema=DATABASE() AND table_name=%s
ORDER BY ordinal_position""", (t,))
return [r[0] for r in cur.fetchall()]
def sync(t, where, params, desc):
scur.execute(f'SELECT * FROM `{t}` WHERE {where}', params)
scols = [d[0] for d in scur.description]
rows = scur.fetchall()
if not rows:
log(f'{t}: 源库 0 行,跳过'); return 0
dcols = set(cols_of(dcur, t))
use = [c for c in scols if c in dcols]
dropped = [c for c in scols if c not in dcols]
if dropped:
log(f'{t}: 目标缺列 {dropped},跳过这些列')
col_sql = ','.join(f'`{c}`' for c in use)
ph = ','.join(['%s'] * len(use))
idx = [scols.index(c) for c in use]
dcur.executemany(f'INSERT INTO `{t}` ({col_sql}) VALUES ({ph})',
[tuple(r[i] for i in idx) for r in rows])
log(f'{t}: 插入 {len(rows)} 行({desc})')
return len(rows)
# 前置检查
dcur.execute('SELECT COUNT(*) FROM zt_story_user WHERE id=%s', (USID,))
if dcur.fetchone()[0]:
log('!!! zt_story_user 2461 目标库已存在,中止'); sys.exit(1)
dcur.execute('SELECT COUNT(*) FROM zt_story_userspec WHERE story=%s', (USID,))
if dcur.fetchone()[0]:
log('!!! zt_story_userspec 2461 目标库已存在,中止'); sys.exit(1)
log('前置检查通过')
n = 0
n += sync('zt_story_user', 'id=%s', (USID,), 'id=2461')
n += sync('zt_story_userspec', 'story=%s', (USID,), 'story=2461')
n += sync('zt_action', "objectID=%s AND objectType IN ('userstory','storyUser','story_user')", (USID,), '用户需求2461历史')
dst.commit()
log(f'已提交,共 {n} 行')
log('--- 回读验证 ---')
dcur.execute('SELECT id, product, LEFT(title,40), status FROM zt_story_user WHERE id=%s', (USID,))
log(f'zt_story_user: {dcur.fetchone()}')
dcur.execute('SELECT story, version, LEFT(title,40), LENGTH(spec) FROM zt_story_userspec WHERE story=%s', (USID,))
log(f'zt_story_userspec: {dcur.fetchall()}')
src.close(); dst.close()
log('完成')