gaoluyang
11 小时以前 e40a608ef9bf51db294d8eefada6a8e2e80cebca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys, json
from pathlib import Path
sys.stdout.reconfigure(encoding='utf-8')
sys.path.insert(0, str(Path(__file__).parent / 'iqc-import-20261012/deps'))
import pymysql
out = Path(__file__).parent / 'module-backfill-20261014'
out.mkdir(exist_ok=True)
tables = ['crm_follow_up_record', 'crm_contract', 'mes_pro_mps', 'mes_pro_workorder', 'mes_pro_feedback', 'mes_qc_iqc', 'mes_qc_ipqc', 'mes_qc_oqc', 'mes_wm_product_produce', 'mes_wm_product_recpt', 'mes_wm_item_recpt', 'mes_energy_record']
snapshot = {}
with pymysql.connect(host='36.213.150.237', port=10000, user='root', password=sys.stdin.readline().strip(), database='mom-hm', charset='utf8mb4', connect_timeout=15, read_timeout=40, cursorclass=pymysql.cursors.DictCursor) as c:
    with c.cursor() as q:
        q.execute('SELECT DATABASE() db')
        assert q.fetchone()['db'] == 'mom-hm'
        q.execute('SHOW TABLES')
        existing = {next(iter(x.values())) for x in q.fetchall()}
        for table in tables:
            if table not in existing:
                print(json.dumps({'table': table, 'exists': False})); continue
            q.execute(f'SHOW FULL COLUMNS FROM `{table}`')
            columns = q.fetchall()
            names = {x['Field'] for x in columns}
            where = 'WHERE deleted=0' if 'deleted' in names else ''
            q.execute(f'SELECT COUNT(*) n FROM `{table}` {where}')
            count = q.fetchone()['n']
            q.execute(f'SELECT * FROM `{table}` {where} ORDER BY id DESC LIMIT 4')
            rows = q.fetchall()
            snapshot[table] = {'columns': columns, 'rows': rows, 'count': count}
            print(json.dumps({'table':table, 'count':count, 'columns':sorted(names), 'latest':rows[:1]},ensure_ascii=False,default=str))
        q.execute('SELECT id, username, nickname, dept_id FROM system_users WHERE deleted=0')
        snapshot['users'] = q.fetchall()
        q.execute('SELECT id,name,parent_id FROM system_dept WHERE deleted=0')
        snapshot['departments'] = q.fetchall()
        print(json.dumps({'departments':snapshot['departments']}, ensure_ascii=False))
(out/'read-snapshot.json').write_text(json.dumps(snapshot,ensure_ascii=False,default=str,indent=2),encoding='utf-8')