提交
This commit is contained in:
1 parent
fb85230d25
commit
05684ba2a4
56 files changed
+26110
-13601
No files matched your search
@@ -1,17 +0,0 @@
|
||||
-- /api/web/admin/api-request-logs 列表性能索引补强
|
||||
-- 典型查询:
|
||||
-- WHERE is_del=0 [AND http_method=?] [AND status_code=?]
|
||||
-- [AND create_at between ? and ?]
|
||||
-- ORDER BY create_at DESC
|
||||
|
||||
-- 默认列表 + 时间倒序分页
|
||||
ALTER TABLE sys_api_request_log
|
||||
ADD INDEX idx_arl_isdel_createat (is_del, create_at);
|
||||
|
||||
-- 按方法过滤
|
||||
ALTER TABLE sys_api_request_log
|
||||
ADD INDEX idx_arl_isdel_method_createat (is_del, http_method, create_at);
|
||||
|
||||
-- 按状态码过滤
|
||||
ALTER TABLE sys_api_request_log
|
||||
ADD INDEX idx_arl_isdel_status_createat (is_del, status_code, create_at);
|
||||
@@ -1,12 +0,0 @@
|
||||
-- 给 /api/web/admin/products 列表(GET,前端加密传输时为 POST + X-HTTP-Method-Override: GET)做索引补强
|
||||
-- 典型 SQL:WHERE is_del = 0 [AND category_id = ?] [AND (name LIKE ... OR description LIKE ...)] ORDER BY create_at DESC
|
||||
-- LIKE '%keyword%' 无法有效走 name/description 索引;无关键词时下面索引可帮助 COUNT + 分页排序。
|
||||
-- 执行前请先 SHOW INDEX FROM bas_product; 若索引已存在可跳过对应语句。
|
||||
|
||||
-- 1) 全量列表 / 仅启用筛选:is_del + 排序字段
|
||||
ALTER TABLE bas_product
|
||||
ADD INDEX idx_product_isdel_createat (is_del, create_at);
|
||||
|
||||
-- 2) 按分类筛选 + 排序(与 1 并存;分类列表走此索引更省扫描)
|
||||
ALTER TABLE bas_product
|
||||
ADD INDEX idx_product_isdel_category_createat (is_del, category_id, create_at);
|
||||
@@ -1,26 +0,0 @@
|
||||
-- /api/web/delivery-tasks 列表性能索引补强
|
||||
-- 目标查询特征:
|
||||
-- 1) WHERE is_del=0 ORDER BY create_at DESC(默认列表)
|
||||
-- 2) WHERE is_del=0 AND region_id=? AND status=? ORDER BY create_at DESC
|
||||
-- 3) WHERE is_del=0 AND deliver_id=? ORDER BY create_at DESC
|
||||
-- 4) 列表中按 task_id 聚合明细数量(biz_delivery_task_item)
|
||||
|
||||
-- 1) 默认列表与时间倒序分页
|
||||
ALTER TABLE biz_delivery_task
|
||||
ADD INDEX idx_dt_isdel_createat (is_del, create_at);
|
||||
|
||||
-- 2) 区域 + 状态过滤场景
|
||||
ALTER TABLE biz_delivery_task
|
||||
ADD INDEX idx_dt_isdel_region_status_createat (is_del, region_id, status, create_at);
|
||||
|
||||
-- 3) 配送员维度过滤场景
|
||||
ALTER TABLE biz_delivery_task
|
||||
ADD INDEX idx_dt_isdel_deliver_createat (is_del, deliver_id, create_at);
|
||||
|
||||
-- 4) 供应商维度过滤场景(供应商端任务列表)
|
||||
ALTER TABLE biz_delivery_task
|
||||
ADD INDEX idx_dt_isdel_supplier_createat (is_del, supplier_id, create_at);
|
||||
|
||||
-- 5) 列表 itemCount 聚合:WHERE task_id IN (...) AND is_del=0 GROUP BY task_id
|
||||
ALTER TABLE biz_delivery_task_item
|
||||
ADD INDEX idx_dti_task_isdel (task_id, is_del);
|
||||
@@ -1,21 +0,0 @@
|
||||
-- 给 /api/web/admin/orders 列表接口做性能索引补强(幂等尽量实现)
|
||||
-- 注意:不同 MySQL 版本对 IF NOT EXISTS 支持不同;若执行失败,请先 SHOW INDEX 再手动判断是否已存在。
|
||||
|
||||
-- 1) 覆盖最常见列表:WHERE is_del=0 ORDER BY create_at DESC
|
||||
-- 目标:减少 Using filesort / 全表扫描风险
|
||||
ALTER TABLE biz_order
|
||||
ADD INDEX idx_order_isdel_createat (is_del, create_at);
|
||||
|
||||
-- 2) 常用筛选:区域 + 订单主状态 + 时间排序
|
||||
ALTER TABLE biz_order
|
||||
ADD INDEX idx_order_isdel_region_orderstatus_createat (is_del, region_id, order_status, create_at);
|
||||
|
||||
-- 3) 常用筛选:区域 + 创建时间范围(startDate/endDate)
|
||||
ALTER TABLE biz_order
|
||||
ADD INDEX idx_order_isdel_region_createat (is_del, region_id, create_at);
|
||||
|
||||
-- 4) 单号查询建议:
|
||||
-- 当前实现是 order_no LIKE '%xxx%',该写法通常无法走索引。
|
||||
-- 若业务可改为前缀匹配(LIKE 'xxx%')或精确匹配(=),将显著提升性能。
|
||||
-- 订单号已有唯一索引 uk_order_no(可用于 '=' 或前缀匹配)。
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
-- /api/web/admin/price-versions 列表性能索引补强
|
||||
-- 典型查询:
|
||||
-- WHERE is_del=0 [AND region_id=?] [AND supplier_id=?] [AND status=?]
|
||||
-- ORDER BY create_at DESC
|
||||
|
||||
-- 默认列表 + 时间倒序分页
|
||||
ALTER TABLE biz_price_list
|
||||
ADD INDEX idx_pl_isdel_createat (is_del, create_at);
|
||||
|
||||
-- 区域维度过滤
|
||||
ALTER TABLE biz_price_list
|
||||
ADD INDEX idx_pl_isdel_region_createat (is_del, region_id, create_at);
|
||||
|
||||
-- 供应商维度过滤
|
||||
ALTER TABLE biz_price_list
|
||||
ADD INDEX idx_pl_isdel_supplier_createat (is_del, supplier_id, create_at);
|
||||
|
||||
-- 状态维度过滤
|
||||
ALTER TABLE biz_price_list
|
||||
ADD INDEX idx_pl_isdel_status_createat (is_del, status, create_at);
|
||||
@@ -1,7 +0,0 @@
|
||||
-- 询价报价界面“区域”下拉/Tab:启用区域列表索引补强
|
||||
-- 对应接口:GET /api/web/admin/regions/active
|
||||
-- 查询:WHERE is_del=0 AND is_active=1 ORDER BY sort_order
|
||||
|
||||
ALTER TABLE sys_region
|
||||
ADD INDEX idx_region_isdel_active_sort (is_del, is_active, sort_order);
|
||||
|
||||
@@ -0,0 +1,573 @@
|
||||
#!/usr/bin/env python3
|
||||
"""字段一致性检查器 — 检测 CRUD → API Schema → 前端 formData 字段缺失问题。
|
||||
|
||||
用法:
|
||||
python backend/scripts/check_field_consistency.py
|
||||
python backend/scripts/check_field_consistency.py --entity consumable
|
||||
python backend/scripts/check_field_consistency.py --json
|
||||
|
||||
退出码:
|
||||
0 — 全部通过
|
||||
1 — 存在警告(字段缺失)
|
||||
2 — 存在错误(文件/类找不到等)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 项目根目录(从脚本位置推断)
|
||||
# ---------------------------------------------------------------------------
|
||||
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
_PROJECT_ROOT = os.path.abspath(os.path.join(_SCRIPT_DIR, "..", ".."))
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 只读字段 — 这些字段出现在 _row_to_dict 但不应出现在 CreateBody/UpdateBody/formData
|
||||
# ---------------------------------------------------------------------------
|
||||
READ_ONLY_FIELDS: Set[str] = {
|
||||
"id",
|
||||
"createTime",
|
||||
"updateTime",
|
||||
"stockQty",
|
||||
"tenantNo",
|
||||
"attachmentFiles", # 计算字段,由后端从 URLs 生成
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 实体注册表
|
||||
# ---------------------------------------------------------------------------
|
||||
ENTITIES: List[Dict[str, str]] = [
|
||||
{
|
||||
"name": "consumable",
|
||||
"crud_file": "backend/app/crud/consumable.py",
|
||||
"schema_file": "backend/app/api/production.py",
|
||||
"create_body": "ConsumableCreateBody",
|
||||
"update_body": "ConsumableUpdateBody",
|
||||
"frontend_file": "web-admin/src/views/production/consumable/index.vue",
|
||||
},
|
||||
{
|
||||
"name": "reagent",
|
||||
"crud_file": "backend/app/crud/reagent.py",
|
||||
"schema_file": "backend/app/api/production.py",
|
||||
"create_body": "ReagentCreateBody",
|
||||
"update_body": "ReagentUpdateBody",
|
||||
"frontend_file": "web-admin/src/views/production/reagent/index.vue",
|
||||
},
|
||||
{
|
||||
"name": "material (market)",
|
||||
"crud_file": "backend/app/crud/material.py",
|
||||
"schema_file": "backend/app/api/material.py",
|
||||
"create_body": "MaterialBody",
|
||||
"update_body": "MaterialUpdateBody",
|
||||
"frontend_file": "web-admin/src/views/market/material/index.vue",
|
||||
},
|
||||
{
|
||||
"name": "qc_material",
|
||||
"crud_file": "backend/app/crud/qc_material.py",
|
||||
"schema_file": "backend/app/api/qc.py",
|
||||
"create_body": "QcMaterialBody",
|
||||
"update_body": "QcMaterialUpdateBody",
|
||||
"frontend_file": "web-admin/src/views/quality/material/index.vue",
|
||||
},
|
||||
{
|
||||
"name": "sop_document",
|
||||
"crud_file": "backend/app/crud/sop_document.py",
|
||||
"schema_file": "backend/app/api/production.py",
|
||||
"create_body": "SopDocumentCreateBody",
|
||||
"update_body": "SopDocumentUpdateBody",
|
||||
"frontend_file": "web-admin/src/views/production/sop/index.vue",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 终端颜色辅助
|
||||
# ===================================================================
|
||||
class _C:
|
||||
"""ANSI 颜色常量"""
|
||||
|
||||
RESET = "\033[0m"
|
||||
GREEN = "\033[32m"
|
||||
YELLOW = "\033[33m"
|
||||
RED = "\033[31m"
|
||||
CYAN = "\033[36m"
|
||||
BOLD = "\033[1m"
|
||||
DIM = "\033[2m"
|
||||
|
||||
|
||||
def _supports_color() -> bool:
|
||||
"""检测终端是否支持颜色"""
|
||||
if os.environ.get("NO_COLOR"):
|
||||
return False
|
||||
if not hasattr(sys.stdout, "isatty"):
|
||||
return False
|
||||
return sys.stdout.isatty()
|
||||
|
||||
|
||||
_COLOR = _supports_color()
|
||||
|
||||
|
||||
def cprint(text: str, color: str = "") -> None:
|
||||
if _COLOR and color:
|
||||
print("{}{}{}".format(color, text, _C.RESET))
|
||||
else:
|
||||
print(text)
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 工具函数
|
||||
# ===================================================================
|
||||
def _read_file(rel_path: str) -> Optional[str]:
|
||||
"""读取项目根目录下的相对路径文件"""
|
||||
full = os.path.join(_PROJECT_ROOT, rel_path)
|
||||
if not os.path.isfile(full):
|
||||
return None
|
||||
with open(full, "r", encoding="utf-8", errors="replace") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def _snake_to_camel(name: str) -> str:
|
||||
"""snake_case → camelCase: warning_qty → warningQty"""
|
||||
parts = name.split("_")
|
||||
return parts[0] + "".join(p.capitalize() for p in parts[1:])
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 提取逻辑 — _row_to_dict
|
||||
# ===================================================================
|
||||
_RE_DICT_KEY = re.compile(r'"([a-zA-Z][a-zA-Z0-9]*)"\s*:')
|
||||
|
||||
|
||||
def extract_row_to_dict_fields(source: str) -> List[str]:
|
||||
"""从 CRUD 文件提取 _row_to_dict 函数的字典键(camelCase)。
|
||||
|
||||
支持一个文件中有多个 _row_to_dict 的情况 — 只取第一个。
|
||||
"""
|
||||
# 定位函数起始
|
||||
func_match = re.search(
|
||||
r"def\s+_row_to_dict\s*\([^)]*\)\s*->\s*[^:]+:", source
|
||||
)
|
||||
if not func_match:
|
||||
return []
|
||||
|
||||
# 从函数开始往后找到第一个 return { ... } 块
|
||||
start = func_match.end()
|
||||
# 找到 return {
|
||||
ret_match = re.search(r"return\s*\{", source[start:])
|
||||
if not ret_match:
|
||||
return []
|
||||
|
||||
brace_start = start + ret_match.end() - 1 # 指向 {
|
||||
|
||||
# 匹配大括号
|
||||
depth = 0
|
||||
end = brace_start
|
||||
for i in range(brace_start, len(source)):
|
||||
ch = source[i]
|
||||
if ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
end = i
|
||||
break
|
||||
|
||||
block = source[brace_start : end + 1]
|
||||
keys = _RE_DICT_KEY.findall(block)
|
||||
# 去重但保持顺序
|
||||
seen = set()
|
||||
result = []
|
||||
for k in keys:
|
||||
if k not in seen:
|
||||
seen.add(k)
|
||||
result.append(k)
|
||||
return result
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 提取逻辑 — Pydantic Schema 类
|
||||
# ===================================================================
|
||||
_RE_FIELD_ALIAS = re.compile(r'alias\s*=\s*"([^"]+)"')
|
||||
_RE_FIELD_LINE = re.compile(
|
||||
r"^\s+([a-z_][a-z0-9_]*)\s*:\s*"
|
||||
)
|
||||
|
||||
|
||||
def extract_schema_fields(source: str, class_name: str) -> Optional[List[str]]:
|
||||
"""从 Pydantic 类提取字段名(返回 camelCase 列表)。
|
||||
|
||||
优先使用 alias,否则 snake_case 转 camelCase。
|
||||
返回 None 表示类未找到。
|
||||
"""
|
||||
# 找到类定义
|
||||
class_match = re.search(
|
||||
r"class\s+{}\s*\([^)]*\)\s*:".format(re.escape(class_name)),
|
||||
source,
|
||||
)
|
||||
if not class_match:
|
||||
return None
|
||||
|
||||
class_start = class_match.end()
|
||||
|
||||
# 收集类体 — 到下一个顶层 class 或 def(不缩进)或文件末尾
|
||||
body_lines = []
|
||||
for line in source[class_start:].split("\n"):
|
||||
stripped = line.lstrip()
|
||||
if stripped and not stripped.startswith("#") and not line[0].isspace():
|
||||
# 到达下一个顶层定义
|
||||
break
|
||||
if stripped.startswith("class ") or (stripped.startswith("def ") and not line.startswith(" ")):
|
||||
break
|
||||
body_lines.append(line)
|
||||
|
||||
body = "\n".join(body_lines)
|
||||
|
||||
fields = []
|
||||
# 按行解析
|
||||
for line in body.split("\n"):
|
||||
field_match = _RE_FIELD_LINE.match(line)
|
||||
if not field_match:
|
||||
continue
|
||||
field_name = field_match.group(1)
|
||||
if field_name == "model_config":
|
||||
continue
|
||||
|
||||
# 检查是否有 alias
|
||||
alias_match = _RE_FIELD_ALIAS.search(line)
|
||||
if alias_match:
|
||||
fields.append(alias_match.group(1))
|
||||
else:
|
||||
fields.append(_snake_to_camel(field_name))
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 提取逻辑 — 前端 formData
|
||||
# ===================================================================
|
||||
_RE_FORMDATA_KEY = re.compile(r"^\s+([a-zA-Z][a-zA-Z0-9]*)\s*:")
|
||||
|
||||
|
||||
def extract_frontend_formdata(source: str) -> List[str]:
|
||||
"""从 Vue 文件提取 formData 的键名(camelCase)。"""
|
||||
# 匹配多种写法: reactive({ ... }), reactive<SomeType>({ ... })
|
||||
match = re.search(
|
||||
r"(?:const|let|var)\s+formData\s*=\s*reactive(?:<[^>]*>)?\s*\(\s*\{",
|
||||
source,
|
||||
)
|
||||
if not match:
|
||||
return []
|
||||
|
||||
start = match.end() - 1 # 指向 {
|
||||
depth = 0
|
||||
end = start
|
||||
for i in range(start, len(source)):
|
||||
ch = source[i]
|
||||
if ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
end = i
|
||||
break
|
||||
|
||||
block = source[start : end + 1]
|
||||
fields = []
|
||||
for line in block.split("\n"):
|
||||
m = _RE_FORMDATA_KEY.match(line)
|
||||
if m:
|
||||
fields.append(m.group(1))
|
||||
return fields
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 单个实体检查
|
||||
# ===================================================================
|
||||
def check_entity(entity: Dict[str, str]) -> Dict[str, Any]:
|
||||
"""检查单个实体的字段一致性,返回报告字典。"""
|
||||
name = entity["name"]
|
||||
report: Dict[str, Any] = {
|
||||
"entity": name,
|
||||
"status": "ok",
|
||||
"crud_fields": [],
|
||||
"create_body_fields": None,
|
||||
"update_body_fields": None,
|
||||
"frontend_fields": [],
|
||||
"warnings": [],
|
||||
"errors": [],
|
||||
}
|
||||
|
||||
# 1. 读取 CRUD 文件
|
||||
crud_src = _read_file(entity["crud_file"])
|
||||
if crud_src is None:
|
||||
report["status"] = "error"
|
||||
report["errors"].append("CRUD file not found: {}".format(entity["crud_file"]))
|
||||
return report
|
||||
|
||||
crud_fields = extract_row_to_dict_fields(crud_src)
|
||||
report["crud_fields"] = crud_fields
|
||||
if not crud_fields:
|
||||
report["status"] = "error"
|
||||
report["errors"].append(
|
||||
"No _row_to_dict fields found in {}".format(entity["crud_file"])
|
||||
)
|
||||
return report
|
||||
|
||||
writable_fields = [f for f in crud_fields if f not in READ_ONLY_FIELDS]
|
||||
|
||||
# 2. 读取 Schema 文件
|
||||
schema_src = _read_file(entity["schema_file"])
|
||||
if schema_src is None:
|
||||
report["status"] = "error"
|
||||
report["errors"].append(
|
||||
"Schema file not found: {}".format(entity["schema_file"])
|
||||
)
|
||||
return report
|
||||
|
||||
# 3. 提取 CreateBody
|
||||
create_fields = extract_schema_fields(schema_src, entity["create_body"])
|
||||
report["create_body_fields"] = create_fields
|
||||
if create_fields is None:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"Create body class '{}' not found in {}".format(
|
||||
entity["create_body"], entity["schema_file"]
|
||||
)
|
||||
)
|
||||
else:
|
||||
missing = [f for f in writable_fields if f not in create_fields]
|
||||
if missing:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"CreateBody missing fields: {}".format(", ".join(sorted(missing)))
|
||||
)
|
||||
|
||||
# 4. 提取 UpdateBody
|
||||
update_fields = extract_schema_fields(schema_src, entity["update_body"])
|
||||
report["update_body_fields"] = update_fields
|
||||
if update_fields is None:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"Update body class '{}' not found in {}".format(
|
||||
entity["update_body"], entity["schema_file"]
|
||||
)
|
||||
)
|
||||
else:
|
||||
missing = [f for f in writable_fields if f not in update_fields]
|
||||
if missing:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"UpdateBody missing fields: {}".format(", ".join(sorted(missing)))
|
||||
)
|
||||
|
||||
# 5. 读取前端文件
|
||||
fe_src = _read_file(entity["frontend_file"])
|
||||
if fe_src is None:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"Frontend file not found: {}".format(entity["frontend_file"])
|
||||
)
|
||||
return report
|
||||
|
||||
fe_fields = extract_frontend_formdata(fe_src)
|
||||
report["frontend_fields"] = fe_fields
|
||||
if not fe_fields:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"No formData found in {}".format(entity["frontend_file"])
|
||||
)
|
||||
return report
|
||||
|
||||
missing_fe = [f for f in writable_fields if f not in fe_fields]
|
||||
if missing_fe:
|
||||
report["status"] = "warning"
|
||||
report["warnings"].append(
|
||||
"Frontend formData missing fields: {}".format(
|
||||
", ".join(sorted(missing_fe))
|
||||
)
|
||||
)
|
||||
|
||||
# 6. 反向检查: Schema 有但 _row_to_dict 没有的字段
|
||||
if create_fields is not None:
|
||||
extra_create = [f for f in create_fields if f not in crud_fields]
|
||||
if extra_create:
|
||||
report.setdefault("info", []).append(
|
||||
"CreateBody has extra fields (not in _row_to_dict): {}".format(
|
||||
", ".join(sorted(extra_create))
|
||||
)
|
||||
)
|
||||
|
||||
# 7. 反向检查: formData 有但 _row_to_dict 没有的字段
|
||||
extra_fe = [f for f in fe_fields if f not in crud_fields]
|
||||
if extra_fe:
|
||||
report.setdefault("info", []).append(
|
||||
"Frontend formData has extra fields (not in _row_to_dict): {}".format(
|
||||
", ".join(sorted(extra_fe))
|
||||
)
|
||||
)
|
||||
|
||||
return report
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 报告输出
|
||||
# ===================================================================
|
||||
def print_report(report: Dict[str, Any]) -> None:
|
||||
"""打印单个实体的检查报告(终端彩色输出)"""
|
||||
name = report["entity"]
|
||||
status = report["status"]
|
||||
|
||||
if status == "ok":
|
||||
cprint(" [OK] {}".format(name), _C.GREEN)
|
||||
elif status == "warning":
|
||||
cprint(" [!!] {} — warnings".format(name), _C.YELLOW)
|
||||
else:
|
||||
cprint(" [XX] {} — errors".format(name), _C.RED)
|
||||
|
||||
# CRUD fields
|
||||
crud_fields = report.get("crud_fields", [])
|
||||
if crud_fields:
|
||||
writable = [f for f in crud_fields if f not in READ_ONLY_FIELDS]
|
||||
read_only = [f for f in crud_fields if f in READ_ONLY_FIELDS]
|
||||
cprint(
|
||||
" _row_to_dict ({} total, {} writable): {}".format(
|
||||
len(crud_fields),
|
||||
len(writable),
|
||||
", ".join(writable),
|
||||
),
|
||||
_C.DIM,
|
||||
)
|
||||
if read_only:
|
||||
cprint(
|
||||
" read-only: {}".format(", ".join(sorted(read_only))),
|
||||
_C.DIM,
|
||||
)
|
||||
|
||||
# Schema fields
|
||||
for body_label, body_key in [
|
||||
("CreateBody", "create_body_fields"),
|
||||
("UpdateBody", "update_body_fields"),
|
||||
]:
|
||||
fields = report.get(body_key)
|
||||
if fields is not None:
|
||||
cprint(
|
||||
" {}: {}".format(body_label, ", ".join(fields)),
|
||||
_C.DIM,
|
||||
)
|
||||
|
||||
# Frontend fields
|
||||
fe_fields = report.get("frontend_fields", [])
|
||||
if fe_fields:
|
||||
cprint(
|
||||
" formData: {}".format(", ".join(fe_fields)),
|
||||
_C.DIM,
|
||||
)
|
||||
|
||||
# Warnings
|
||||
for w in report.get("warnings", []):
|
||||
cprint(" WARN: {}".format(w), _C.YELLOW)
|
||||
|
||||
# Errors
|
||||
for e in report.get("errors", []):
|
||||
cprint(" ERR: {}".format(e), _C.RED)
|
||||
|
||||
# Info
|
||||
for info in report.get("info", []):
|
||||
cprint(" INFO: {}".format(info), _C.CYAN)
|
||||
|
||||
print()
|
||||
|
||||
|
||||
def print_json_report(reports: List[Dict[str, Any]]) -> None:
|
||||
"""输出 JSON 格式报告"""
|
||||
print(json.dumps(reports, indent=2, ensure_ascii=False))
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# 主入口
|
||||
# ===================================================================
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="字段一致性检查器 — CRUD ↔ Schema ↔ 前端 formData"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--entity",
|
||||
type=str,
|
||||
default=None,
|
||||
help="只检查指定实体名称(如 consumable)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--json",
|
||||
action="store_true",
|
||||
dest="json_output",
|
||||
help="输出 JSON 格式",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# 筛选实体
|
||||
entities = ENTITIES
|
||||
if args.entity:
|
||||
entities = [e for e in ENTITIES if e["name"] == args.entity]
|
||||
if not entities:
|
||||
cprint(
|
||||
"Entity '{}' not found. Available: {}".format(
|
||||
args.entity, ", ".join(e["name"] for e in ENTITIES)
|
||||
),
|
||||
_C.RED,
|
||||
)
|
||||
return 2
|
||||
|
||||
if not args.json_output:
|
||||
cprint(
|
||||
"Field Consistency Checker — checking {} entities".format(
|
||||
len(entities)
|
||||
),
|
||||
_C.BOLD,
|
||||
)
|
||||
cprint("Project root: {}".format(_PROJECT_ROOT), _C.DIM)
|
||||
print()
|
||||
|
||||
reports = []
|
||||
has_warnings = False
|
||||
has_errors = False
|
||||
|
||||
for entity in entities:
|
||||
report = check_entity(entity)
|
||||
reports.append(report)
|
||||
if report["status"] == "warning":
|
||||
has_warnings = True
|
||||
elif report["status"] == "error":
|
||||
has_errors = True
|
||||
|
||||
if args.json_output:
|
||||
print_json_report(reports)
|
||||
else:
|
||||
for report in reports:
|
||||
print_report(report)
|
||||
|
||||
# 汇总
|
||||
ok_count = sum(1 for r in reports if r["status"] == "ok")
|
||||
warn_count = sum(1 for r in reports if r["status"] == "warning")
|
||||
err_count = sum(1 for r in reports if r["status"] == "error")
|
||||
|
||||
cprint(
|
||||
"Summary: {} OK, {} warnings, {} errors (total {})".format(
|
||||
ok_count, warn_count, err_count, len(reports)
|
||||
),
|
||||
_C.BOLD,
|
||||
)
|
||||
|
||||
if has_errors:
|
||||
return 2
|
||||
if has_warnings:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -1,68 +0,0 @@
|
||||
-- 运营端权限改造:super_admin + 多区域 + 菜单 code 授权
|
||||
|
||||
-- 1) 角色迁移:admin -> super_admin
|
||||
UPDATE sys_user
|
||||
SET role = 'super_admin',
|
||||
update_at = NOW()
|
||||
WHERE is_del = 0
|
||||
AND role = 'admin';
|
||||
|
||||
-- 2) 用户-区域权限表
|
||||
CREATE TABLE IF NOT EXISTS sys_user_region_scope (
|
||||
id VARCHAR(32) NOT NULL COMMENT '主键ID',
|
||||
user_id VARCHAR(32) NOT NULL COMMENT '用户ID',
|
||||
region_id VARCHAR(32) NOT NULL COMMENT '区域ID',
|
||||
is_del TINYINT NOT NULL DEFAULT 0 COMMENT '是否删除',
|
||||
create_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
create_by VARCHAR(32) DEFAULT NULL COMMENT '创建人ID',
|
||||
create_name VARCHAR(100) DEFAULT NULL COMMENT '创建人姓名',
|
||||
update_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
|
||||
update_by VARCHAR(32) DEFAULT NULL COMMENT '修改人ID',
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uk_user_region_scope (user_id, region_id, is_del),
|
||||
KEY idx_user_region_scope_user (user_id, is_del),
|
||||
KEY idx_user_region_scope_region (region_id, is_del)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户区域权限';
|
||||
|
||||
-- 3) 用户-区域权限历史表(无索引、无 is_del)
|
||||
CREATE TABLE IF NOT EXISTS sys_user_region_scope_his (
|
||||
id VARCHAR(32) NOT NULL COMMENT '主键ID',
|
||||
user_id VARCHAR(32) NOT NULL COMMENT '用户ID',
|
||||
region_id VARCHAR(32) NOT NULL COMMENT '区域ID',
|
||||
create_at DATETIME DEFAULT NULL COMMENT '创建时间',
|
||||
create_by VARCHAR(32) DEFAULT NULL COMMENT '创建人ID',
|
||||
create_name VARCHAR(100) DEFAULT NULL COMMENT '创建人姓名',
|
||||
update_at DATETIME DEFAULT NULL COMMENT '修改时间',
|
||||
update_by VARCHAR(32) DEFAULT NULL COMMENT '修改人ID',
|
||||
his_create_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '历史记录创建时间'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户区域权限历史';
|
||||
|
||||
-- 4) 用户-菜单 code 权限表
|
||||
CREATE TABLE IF NOT EXISTS sys_user_menu_scope (
|
||||
id VARCHAR(32) NOT NULL COMMENT '主键ID',
|
||||
user_id VARCHAR(32) NOT NULL COMMENT '用户ID',
|
||||
menu_code VARCHAR(100) NOT NULL COMMENT '菜单编码',
|
||||
is_del TINYINT NOT NULL DEFAULT 0 COMMENT '是否删除',
|
||||
create_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
create_by VARCHAR(32) DEFAULT NULL COMMENT '创建人ID',
|
||||
create_name VARCHAR(100) DEFAULT NULL COMMENT '创建人姓名',
|
||||
update_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
|
||||
update_by VARCHAR(32) DEFAULT NULL COMMENT '修改人ID',
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uk_user_menu_scope (user_id, menu_code, is_del),
|
||||
KEY idx_user_menu_scope_user (user_id, is_del),
|
||||
KEY idx_user_menu_scope_code (menu_code, is_del)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户菜单权限';
|
||||
|
||||
-- 5) 用户-菜单 code 权限历史表(无索引、无 is_del)
|
||||
CREATE TABLE IF NOT EXISTS sys_user_menu_scope_his (
|
||||
id VARCHAR(32) NOT NULL COMMENT '主键ID',
|
||||
user_id VARCHAR(32) NOT NULL COMMENT '用户ID',
|
||||
menu_code VARCHAR(100) NOT NULL COMMENT '菜单编码',
|
||||
create_at DATETIME DEFAULT NULL COMMENT '创建时间',
|
||||
create_by VARCHAR(32) DEFAULT NULL COMMENT '创建人ID',
|
||||
create_name VARCHAR(100) DEFAULT NULL COMMENT '创建人姓名',
|
||||
update_at DATETIME DEFAULT NULL COMMENT '修改时间',
|
||||
update_by VARCHAR(32) DEFAULT NULL COMMENT '修改人ID',
|
||||
his_create_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '历史记录创建时间'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户菜单权限历史';
|
||||
Reference in new issue
Block a user