dashboard/check_cn_map.py
"""列映射一致性自检 —— 加了新表/新字段后必跑。
为什么需要它
------------
「SELECT 读得通但 INSERT 报 500」是本项目最难查的一类问题:
读接口走 `SELECT *`,写接口走显式列名,两条路都依赖 cn_map 的同一套映射。
映射写错时,读接口往往"看起来正常"(只是少个字段),
写接口则直接被云库拒绝(column does not exist → 500)。
本脚本把「代码里用的英文列名 → 应翻译成的中文列名」写成一张期望表,
与 information_schema 的真实列名逐一比对,**新增字段时立刻能发现遗漏**。
用法
----
python check_cn_map.py # 只查映射表(不需要网络)
python check_cn_map.py --live # 再拉云库真实 schema 交叉核对
"""
import sys
sys.path.insert(0, ".")
import cn_map as m
# 表 -> {英文列名: 云库实际中文列名}
# 改代码或加表时,先更新这里,再跑本脚本
EXPECT = {
"positions": {"id": "编号", "name": "名称", "store": "归属门店", "level": "职级",
"parent_id": "上级编号", "created_at": "创建时间", "updated_at": "更新时间"},
"tasks": {"id": "编号", "title": "标题", "bg": "背景", "dispatcher": "下发人",
"executor": "执行人", "approver": "验收人", "priority": "优先级",
"status": "状态", "progress": "进度", "deadline": "截止",
"acceptance": "验收标准", "store": "门店", "feedback": "反馈",
"created_at": "创建时间", "updated_at": "更新时间"},
"inbox": {"id": "编号", "title": "标题", "body": "正文", "link": "链接",
"read_flag": "已读", "receiver": "接收人", "created_at": "创建时间"},
"op_logs": {"id": "编号", "actor": "操作人", "action": "动作", "target": "对象",
"detail": "详情", "created_at": "创建时间"},
"perm_applications": {"id": "编号", "account": "账号", "name": "姓名",
"want_role": "期望角色", "want_scope": "期望范围",
"want_stores": "期望门店", "reason": "申请理由",
"status": "状态", "reviewed_by": "审批人",
"review_note": "审批备注", "applied_at": "申请时间",
"reviewed_at": "审批时间"},
"monthly_sales": {"id": "编号", "month": "月份", "store": "门店",
"target": "目标", "actual": "实际",
"created_at": "创建时间", "updated_at": "更新时间"},
"users": {"id": "编号", "account": "账号", "name": "姓名",
"password_hash": "密码哈希", "role": "角色", "store_scope": "门店范围",
"compare_enabled": "可对比", "enabled": "启用",
"last_login": "最后登录", "created_at": "创建时间"},
"alert_rules": {"id": "编号", "type_id": "类型编号", "dataset_id": "数据表编号",
"column_name": "监控列", "operator": "运算符", "threshold": "阈值",
"message": "提示语", "enabled": "启用", "updated_at": "更新时间"},
"alert_types": {"id": "编号", "name": "名称", "icon": "图标", "color": "颜色",
"enabled": "启用", "created_at": "创建时间"},
"stores": {"id": "编号", "name": "名称", "brand": "品牌", "manager": "负责人",
"phone": "电话", "note": "备注", "created_at": "创建时间"},
"datasets": {"id": "编号", "name": "名称", "platform": "平台",
"source_url": "来源网址", "store_id": "门店编号",
"sheet_name": "工作表名", "columns": "列清单", "rows": "内容",
"platform_token": "平台表标识", "range": "拉取区域",
"auto_sync": "自动同步", "field_mapping": "字段映射",
"fetched_at": "抓取时间", "created_at": "创建时间"},
"change_log": {"id": "编号", "resource_type": "变更类型",
"resource_id": "变更对象", "created_at": "创建时间"},
"dashboard_config": {"id": "编号", "config": "配置", "updated_at": "更新时间"},
}
def main():
bad = 0
print("=== 列映射一致性检查 ===")
for table, cols in EXPECT.items():
cn_table = m.TABLE_CN.get(table)
if not cn_table:
print(f" ❌ 表 {table} 未在 TABLE_CN 登记")
bad += 1
continue
if cn_table not in m.TABLE_EN:
print(f" ❌ 表 {cn_table} 缺少反向映射")
bad += 1
for en, expect_cn in cols.items():
got = m._col_cn(table, en)
if got != expect_cn:
print(f" ❌ {table}.{en}: 翻译为「{got or '(空)'}」,期望「{expect_cn}」")
bad += 1
print(f"\n检查 {len(EXPECT)} 张表,"
f"{sum(len(c) for c in EXPECT.values())} 个字段,"
f"不一致 {bad} 处")
# 危险信号:COLUMN_CN 里存在会覆盖已有表含义的键
if "--live" in sys.argv:
print("\n(--live 模式需要能访问云库 information_schema,"
"请用 MCP db_exec_sql 交叉核对)")
return 1 if bad else 0
if __name__ == "__main__":
sys.exit(main())