12451 ee92102f3c feat(scope)+fix(db)+feat(security): 数据范围(项目级)生效、库列类型对齐、安全加固、状态位分层展示、能耗实时聚合
- user_scopes 由"假开关"做成生效:粒度到项目级(组织隔离 ∧ 项目∈范围,取交集);未配置=不限制(逐字向后兼容);
  总管理员豁免;脏数据保守忽略不锁死;配置期校验存在性+归属;逐接口叠加(列表/详情/写操作/Excel 导出落 scope_project_ids);
  前端新增"数据范围"弹窗入口。顺带修 GET /api/charge-orders 此前完全没有组织隔离(跨租户泄露)、
  GET /api/projects/{id}/cabinets 的 500
- 数据库列类型系统性对齐:扫描并修复 pms_dev 32 项 / pms 18 项漂移(时间列统一 DATETIME,sqlx NaiveDateTime 只兼容 DATETIME);
  DECIMAL→BIGINT 与列注释口径拉平;修复生产 GET /api/charge-orders、GET /api/device-logs 必然 500 的根因;
  恢复 7 张表丢失的 updated_at ON UPDATE(行为变化,已记入 AGENTS.md);修正 SCHEMA_SQL 前向引用(空库建表必崩);
  新增 tools/db_schema_diff.py(漂移检查,退出码可接部署前置)与 test/schema_alignment_e2e.py
- 安全加固四项:上传去 .svg + 魔数嗅探(堵存储型 XSS);登录失败锁定(5次/15分)+challenge 限流+并发闸门+验证码容差收紧;
  服务降权 root→pms;nginx 安全头 + limit_req + CORS 白名单收敛
- TCP 连接登录门控(上行+下行同一规则):login 前只放行 auth_str/login(PRE_LOGIN_ALLOWED_ACTS)
- 状态位按归属层级展示:柜级 status1/status2 显示在柜子(含柜门 + 开门按钮,独立动作不联动业务)、
  仓体级 batt_status/pow_status 显示在仓体;两端解析函数同源同名
- 能耗报表改实时聚合 charge_records(原 energy_stats 全表只读不写导致报表恒 0)+ 补复合索引
- 组织可见性:新增 GET /api/my-organizations(工厂全量、客户仅本组织)
- 仓体 status 仅表占用,充满/故障由 Redis 通道状态派生
- 订单/占用/队列运维缺口:h5 建单漏单、214 仓体自愈、device:reports 上限、死信 TTL、LimitNOFILE、
  每日 mysqldump + 回滚点保留 5 次
- 文档:AGENTS.md 补层级可见性/数据范围/状态位约定/数据库工具/行为变化;docs/API.md 删除废弃 TCP 章节、接口总表对齐 mod.rs
2026-09-20 02:44:51 +08:00

77 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""只读比对:pms_dev 与 pms 的目标列定义 + organizations.parent_id 索引/外键
用法: python .tmp-schema-align/compare.py
只发 SELECT,不做任何写入。
"""
import pymysql
COLS = [
("charge_records", "energy"),
("charge_records", "start_time"),
("charge_records", "end_time"),
("energy_stats", "total_energy"),
("download_tasks", "completed_at"),
("organizations", "parent_id"),
]
COL_SQL = """
SELECT COLUMN_TYPE, IS_NULLABLE, IFNULL(CAST(COLUMN_DEFAULT AS CHAR),'<NULL>'),
IFNULL(CAST(COLUMN_COMMENT AS CHAR),'')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA=%s AND TABLE_NAME=%s AND COLUMN_NAME=%s
"""
IDX_SQL = """
SELECT INDEX_NAME, SEQ_IN_INDEX, COLUMN_NAME, NON_UNIQUE
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA=%s AND TABLE_NAME='organizations'
ORDER BY INDEX_NAME, SEQ_IN_INDEX
"""
FK_SQL = """
SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA=%s AND TABLE_NAME='organizations' AND REFERENCED_TABLE_NAME IS NOT NULL
"""
def snapshot(db):
c = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database=db)
cur = c.cursor()
cols = {}
for t, col in COLS:
cur.execute(COL_SQL, (db, t, col))
cols[f"{t}.{col}"] = cur.fetchone() or ("<MISSING>",) * 4
cur.execute(IDX_SQL, (db,))
idx = cur.fetchall()
cur.execute(FK_SQL, (db,))
fk = cur.fetchall()
c.close()
return cols, idx, fk
dev_cols, dev_idx, dev_fk = snapshot("pms_dev")
prd_cols, prd_idx, prd_fk = snapshot("pms")
print("=" * 100)
print("%-32s | %-44s | %-44s | %s" % ("列", "pms_dev(测试)", "pms(生产)", "一致"))
print("=" * 100)
all_same = True
for key in [f"{t}.{c}" for t, c in COLS]:
d, p = dev_cols[key], prd_cols[key]
same = d == p
all_same &= same
fmt = lambda r: "%s null=%s def=%s cmt=%s" % r
print("%-32s | %-44s | %-44s | %s" % (key, fmt(d), fmt(p), "OK" if same else "DIFF"))
print("-" * 100)
print("列定义完全一致:", all_same)
print()
print("organizations 索引 pms_dev :", dev_idx)
print("organizations 索引 pms :", prd_idx)
print("parent_id 处于索引最左列 :", any(r[1] == 1 and r[2] == "parent_id" for r in dev_idx),
"/", any(r[1] == 1 and r[2] == "parent_id" for r in prd_idx))
print()
print("organizations 自引用外键 pms_dev :", dev_fk)
print("organizations 自引用外键 pms :", prd_fk)
print("外键均存在:", bool(dev_fk) and bool(prd_fk))