gaoluyang
12 小时以前 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
import sys,json
from pathlib import Path
sys.stdout.reconfigure(encoding='utf-8')
sys.path.insert(0,str(Path('scripts/iqc-import-20261012/deps').resolve()))
import pymysql
P=Path(__file__).parent
c=pymysql.connect(host='36.213.150.237',port=10000,user='root',password=sys.stdin.readline().strip(),database='mom-hm',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor,connect_timeout=15,read_timeout=60)
prefixes=('crm_','erp_sale_','mes_pro_','mes_qc_','mes_wm_','mes_energy_','energy_','mes_pd_process_param')
snapshot={}
with c.cursor() as q:
 q.execute('SHOW TABLES');tables=[next(iter(x.values())) for x in q.fetchall()]
 selected=[t for t in tables if t.startswith(prefixes) or t in ['mdm_item','mdm_unit_measure','mes_md_item','mes_md_unit_measure','system_users','system_dept','system_user_post','system_post','system_role','system_user_role','system_dict_data']]
 for t in selected:
  q.execute('SHOW FULL COLUMNS FROM `'+t+'`');cols=q.fetchall();fields={x['Field'] for x in cols}
  where='WHERE deleted=0' if 'deleted' in fields else ''
  q.execute('SELECT COUNT(*) total FROM `'+t+'` '+where);total=q.fetchone()['total']
  if 'id' not in fields:continue
  q.execute(('SELECT id,username,nickname,dept_id,status FROM system_users ' if t=='system_users' else 'SELECT * FROM `'+t+'` ')+where+' ORDER BY id DESC LIMIT 3');rows=q.fetchall()
  snapshot[t]={'columns':cols,'count':total,'latest':rows}
  print(json.dumps({'table':t,'count':total,'columns':list(fields)},ensure_ascii=False))
 q.execute('SELECT id,username,nickname,dept_id,status 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({'users':snapshot['users'],'departments':snapshot['departments']},ensure_ascii=False,default=str))
(P/'schema-snapshot.json').write_text(json.dumps(snapshot,ensure_ascii=False,indent=2,default=str),encoding='utf8')
c.close()