Files
bj_power/sql/gen_comments.py
T

232 lines
8.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
从 ent schema 源文件抽取 表注释(类型 doc) + 列注释(.Comment 或字段上方 inline //),
与真实数据库目录(information_schema 导出 TSV) join,生成可重复执行的 COMMENT ON SQL。
仅对库中真实存在的 表/列 生成语句;不猜测业务含义。
"""
import os, re, sys
ROOT = "D:/hardman/bj_power"
SCHEMA_DIRS = {
"mes": os.path.join(ROOT, "bj_power_mes", "schema"),
"wms": os.path.join(ROOT, "bj_power_wms", "schema"),
}
CATALOG = {
"mes": os.path.join(ROOT, "sql", "_mes_catalog.tsv"),
"wms": os.path.join(ROOT, "sql", "_wms_catalog.tsv"),
}
OUT = {
"mes": os.path.join(ROOT, "sql", "db_comments_mes.sql"),
"wms": os.path.join(ROOT, "sql", "db_comments_wms.sql"),
}
# ---------- helpers ----------
def snake(name):
out = []
for i, ch in enumerate(name):
if ch.isupper() and i > 0:
out.append('_')
out.append(ch.lower())
return "".join(out)
def pluralize(s):
if s.endswith('y') and s[-2:-1] not in 'aeiou':
return s[:-1] + 'ies'
if s.endswith(('s', 'x', 'ch', 'sh')):
return s + 'es'
return s + 's'
SYS_MAP = {
'id': '主键ID',
'created_at': '创建时间',
'updated_at': '更新时间',
'deleted_at': '逻辑删除时间',
'created_by': '创建人',
'updated_by': '更新人',
}
# 兜底:声明了但既无 .Comment 也无 inline // 的列,按列名给出安全中文(字面翻译,低误判)
NAME_MAP = {
'remark': '备注',
'operator': '操作人',
'order_no': '关联工单号',
'material_name': '物料名称',
'spec': '规格',
'unit': '单位',
'icon': '图标',
'sort': '排序',
'target_dock': '目标接驳台',
'process_name': '工序名称',
'step_name': '工序名称',
'criterion_name': '判据名称',
'criterion_logic': '判据逻辑',
'is_ok': '是否合格',
'started_at': '开始时间',
'ended_at': '结束时间',
'resolved_at': '处理时间',
'parent_code': '父级编码',
'target_type': '目标类型',
'target_id': '目标ID',
'material_code': '物料编码',
'zone_code': '区域编码',
'shelf_no': '货架号',
'layer_no': '层号',
'position_no': '位置号',
'book_qty': '账面数量',
'scanned_qty': '扫描数量',
'diff_qty': '差异数量',
'counted': '是否已盘点',
'counted_by': '盘点人',
'counted_at': '盘点时间',
'product_name': '产品名称',
'completed_at': '完成时间',
}
def find_block(text, brace_idx):
depth = 0
for i in range(brace_idx, len(text)):
if text[i] == '{':
depth += 1
elif text[i] == '}':
depth -= 1
if depth == 0:
return text[brace_idx+1:i]
return ""
FIELD_RE = re.compile(r'^\s*field\.\w+\(\s*"(\w+)"')
COMMENT_RE = re.compile(r'\.Comment\(\s*"((?:[^"\\]|\\.)*)"')
TABLE_RE = re.compile(r'entsql\.Annotation\{\s*Table:\s*"(\w+)"')
TYPE_RE = re.compile(r'^type\s+(\w+)\s+struct', re.MULTILINE)
def parse_file(path):
with open(path, encoding='utf-8') as f:
text = f.read()
results = [] # list of (type_name, doc, table_name, [(col, comment), ...])
for tm in TYPE_RE.finditer(text):
type_name = tm.group(1)
type_line = text[:tm.start()].count('\n')
# doc comment: walk up from type line over consecutive // lines
lines = text.split('\n')
doc = []
j = type_line - 1
while j >= 0 and lines[j].strip().startswith('//'):
doc.insert(0, lines[j].strip()[2:].strip())
j -= 1
doc_text = doc[0] if doc else type_name
# table name
ann_m = re.search(r'func\s*\(\w+\)\s*Annotations\(\)\s*\[\]schema\.Annotation\s*\{', text)
table = None
if ann_m:
ab = find_block(text, ann_m.end()-1)
tm2 = TABLE_RE.search(ab)
if tm2:
table = tm2.group(1)
if not table:
table = pluralize(snake(type_name))
# fields block
fm = re.search(r'func\s*\(\w+\)\s*Fields\(\)\s*\[\]ent\.Field\s*\{', text)
fields = []
if fm:
fb = find_block(text, fm.end()-1)
flines = fb.split('\n')
starts = [(i, FIELD_RE.match(flines[i]).group(1))
for i in range(len(flines))
if FIELD_RE.match(flines[i])]
for k, (i, fname) in enumerate(starts):
end = starts[k+1][0] if k+1 < len(starts) else len(flines)
seg = '\n'.join(flines[i:end])
cm = COMMENT_RE.search(seg)
if cm:
comment = cm.group(1)
else:
# inline // immediately above the field start line (within fb)
buf = []
jj = i - 1
while jj >= 0 and flines[jj].strip().startswith('//'):
buf.insert(0, flines[jj].strip()[2:].strip())
jj -= 1
comment = " ".join(buf) if buf else ""
col = snake(fname)
if not comment:
comment = SYS_MAP.get(col, "")
fields.append((col, comment))
results.append((type_name, doc_text, table, fields))
return results
def clean(s):
s = s.replace("\\", "\\\\")
s = s.replace("'", "''")
s = re.sub(r'\s+', ' ', s).strip()
return s
def main():
for side in ("mes", "wms"):
# parse all schema files
schema_map = {} # table_name -> (doc, {col: comment})
unparsed_files = []
for fn in sorted(os.listdir(SCHEMA_DIRS[side])):
if not fn.endswith('.go'):
continue
if fn in ('gen.go', 'mixin.go'):
continue
parsed = parse_file(os.path.join(SCHEMA_DIRS[side], fn))
for type_name, doc, table, fields in parsed:
colmap = {}
for col, comment in fields:
colmap[col] = comment
schema_map[table] = (doc, colmap, type_name)
# read catalog
cat = {} # table -> [(col, dtype, existing_comment)]
with open(CATALOG[side], encoding='utf-8') as f:
header = f.readline()
for line in f:
line = line.rstrip('\n')
if not line:
continue
parts = line.split('\t')
if len(parts) < 4:
continue
t, c, dt, ec = parts[0], parts[1], parts[2], parts[3]
cat.setdefault(t, []).append((c, dt, ec))
# generate SQL
out_lines = []
out_lines.append("-- ============================================================")
out_lines.append(f"-- {side.upper()} 数据库 表/列 注释(COMMENT ON),可重复执行")
out_lines.append("-- 由 ent schema 源文件抽取生成;表名/列名均以真实库目录为准")
out_lines.append("-- ============================================================\n")
tables_done = set()
table_comments_emitted = 0
col_comments_emitted = 0
cols_without = [] # (table, col) no comment available
for t in sorted(cat.keys()):
if t in schema_map:
doc, colmap, type_name = schema_map[t]
# table comment
if doc:
out_lines.append(f'COMMENT ON TABLE "{t}" IS \'{clean(doc)}\';')
table_comments_emitted += 1
# column comments
for (c, dt, ec) in cat[t]:
comment = colmap.get(c, "") or NAME_MAP.get(c, "") or SYS_MAP.get(c, "")
if comment:
out_lines.append(f'COMMENT ON COLUMN "{t}"."{c}" IS \'{clean(comment)}\';')
col_comments_emitted += 1
else:
cols_without.append((t, c))
tables_done.add(t)
out_lines.append("")
with open(OUT[side], 'w', encoding='utf-8') as f:
f.write('\n'.join(out_lines))
# report
unmatched = [t for t in cat.keys() if t not in schema_map]
print(f"[{side.upper()}] schema tables matched: {len(tables_done)}")
print(f"[{side.upper()}] table comments emitted: {table_comments_emitted}")
print(f"[{side.upper()}] column comments emitted: {col_comments_emitted}")
print(f"[{side.upper()}] columns WITHOUT comment (skipped): {len(cols_without)}")
for t, c in cols_without:
print(f" NO-COMMENT {t}.{c}")
print(f"[{side.upper()}] DB tables WITHOUT schema file: {unmatched}")
print(f"[{side.upper()}] -> wrote {OUT[side]}\n")
if __name__ == '__main__':
main()