318 lines
16 KiB
Python
318 lines
16 KiB
Python
#!/usr/bin/env python3
|
||
"""写回配置的**全库体检**:躺在库里带病的行,能不能被照出来。
|
||
|
||
PYTHONPATH=. python3 scripts/check_writeback_config.py # 体检 + 自检,有病就红
|
||
PYTHONPATH=. python3 scripts/check_writeback_config.py --report # 只报,不当闸门
|
||
|
||
为什么要有这一份(口径见 ``docs/node-writeback-config.md`` §9.5):保存时才验的那把尺子
|
||
只管得到"这一次保存的那一条"。而这一片的病全都长一个样——**界面上配上了,运行期什么也
|
||
不发生**,或者"该发一条结果发了两条",发件箱里连一条记录都不留:
|
||
|
||
* 模板上那句「到达某环节」存的是**某一条流程自己的环节主键**。复制流程、恢复版本、
|
||
导到另一套环境都会换掉它,那条写回从此一声不响;
|
||
* 环节挂载点名的模板可以早被删了、停用了、绑到别的表单上;
|
||
* 挂在环节上的模板又勾了终态 = 这一环节一次、进终态那一刻一次,两条幂等键不同,
|
||
上游当成两笔(凭证开两张);
|
||
* 「被退回时 / 被撤回时」配在了压根没有这一刻的环节上(条件分支没人按按钮),或者配在
|
||
那颗按钮已经被关掉的环节上——同样是界面上配上了、运行期一声不响。
|
||
|
||
这些行都是"上一次保存时还是好的",之后环境变了、别人改了流程,没人回头看过。
|
||
所以这里要钉住两件事:**先报现库**(部署到新环境第一件事就该看见这份),
|
||
**再故意造一遍病**(每种病各造一次,证明尺子真的认得它——体检工具自己没人验过,
|
||
它报"一切正常"和最开始的"没人发现"是同一种失效)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
from datetime import datetime
|
||
from types import SimpleNamespace
|
||
|
||
from sqlalchemy import delete, select, text
|
||
from starlette.testclient import TestClient
|
||
|
||
from app import models, seed
|
||
from app.api import integrations
|
||
from app.db import SessionLocal, _backfill_literal, engine
|
||
from app.main import app
|
||
from app.security import create_access_token
|
||
|
||
STAMP = datetime.now().strftime("%m%d%H%M%S")
|
||
SOURCE_CODE = f"chk_cfg_src_{STAMP}"
|
||
ACTIVITY_NAME = f"体检环节_{STAMP}"
|
||
GHOST_ACTIVITY = f"chk_cfg_ghost_{STAMP}"
|
||
REPORT_ONLY = "--report" in sys.argv
|
||
|
||
FAILS: list[str] = []
|
||
PASSED = 0
|
||
CODES: list[str] = []
|
||
|
||
|
||
def check(label: str, ok: bool, detail: str = "") -> None:
|
||
global PASSED
|
||
if ok:
|
||
PASSED += 1
|
||
print(f"[通过] {label}")
|
||
else:
|
||
FAILS.append(label)
|
||
print(f"[FAIL] {label}{f' -> {detail}' if detail else ''}")
|
||
|
||
|
||
def code(name: str) -> str:
|
||
value = f"chk_cfg_{name}_{STAMP}"
|
||
CODES.append(value)
|
||
return value
|
||
|
||
|
||
def findings(db) -> list[dict]:
|
||
"""全库体检那一份。每次先 ``rollback()`` 换一个快照再读。
|
||
|
||
接口那条路用的是另一个 Session,MySQL 默认 REPEATABLE READ——这边不换快照就会抱着
|
||
旧的一份看不见刚提交的行(这不是 bug,是这一层脚本都要记得的一件事)。
|
||
"""
|
||
db.rollback()
|
||
return integrations.audit(db)
|
||
|
||
|
||
def problems_of(items: list[dict], *needles: str) -> list[str]:
|
||
"""把提到了任一关键字的问题条目原文挑出来(体检文案会变,判定只认关键的那几个字)。"""
|
||
out: list[str] = []
|
||
for item in items:
|
||
for problem in item.get("problems") or []:
|
||
if any(needle in problem for needle in needles):
|
||
out.append(problem)
|
||
return out
|
||
|
||
|
||
def add_template(db, name: str, **columns) -> str:
|
||
"""直接落库:体检要看的恰恰是"没走保存那道闸就躺在库里的行"。"""
|
||
value = code(name)
|
||
row = {
|
||
"code": value, "name": f"体检模板 {name}", "source": SOURCE_CODE, "enabled": True,
|
||
"trigger_event": models.Integration.EVENT_MANUAL, "trigger_events": [],
|
||
"payload": {"method": "POST", "path": "/none", "fields": []},
|
||
}
|
||
row.update(columns)
|
||
db.add(models.Integration(**row))
|
||
db.commit()
|
||
return value
|
||
|
||
|
||
def report(items: list[dict]) -> int:
|
||
count = sum(len(item.get("problems") or []) for item in items)
|
||
if not count:
|
||
print("[体检] 现库干净:没有一条写回配置会静默不发或多发")
|
||
return 0
|
||
print(f"[体检] 现库有 {len(items)} 处配置、共 {count} 条问题:")
|
||
for item in items:
|
||
where = item.get("name") or item.get("code") or "?"
|
||
kind = {"mount": "环节挂载", "terminate": "终止挂载"}.get(
|
||
str(item.get("kind") or ""), "写回模板")
|
||
print(f" · {kind}「{where}」")
|
||
for problem in item.get("problems") or []:
|
||
print(f" - {problem}")
|
||
return count
|
||
|
||
|
||
def main() -> int:
|
||
db = SessionLocal()
|
||
client = TestClient(app, raise_server_exceptions=False)
|
||
token, _ = create_access_token("1")
|
||
headers = {"Authorization": f"Bearer {token}"}
|
||
activity_id = ""
|
||
dirty = 0
|
||
try:
|
||
db.add(models.DataSource(
|
||
code=SOURCE_CODE, name=f"体检数据源 {STAMP}", direction=models.DataSource.DIRECTION_OUT,
|
||
provider=models.DataSource.PROVIDER_MOCK, mock_payload={"ok": True}, enabled=True,
|
||
))
|
||
# 先提交再往下走:下面每一次体检都先 rollback() 换快照,没提交的这一行会被 rollback
|
||
# 一起丢掉,于是每一条造出来的模板都多一句"数据源不存在"
|
||
db.commit()
|
||
process = db.execute(
|
||
select(models.Process).order_by(models.Process.name, models.Process.id).limit(1)
|
||
).scalars().first()
|
||
if process is None:
|
||
print("[跳过] 库里一条流程都没有,挂载那一层的校不了")
|
||
return 0
|
||
|
||
# 先报现库:这一份才是部署到新环境时真正要看的东西(造病之前)
|
||
dirty = report(findings(db))
|
||
|
||
activity = models.Activity(
|
||
process=process.id, type="manual", name=ACTIVITY_NAME, alias=ACTIVITY_NAME, x=0, y=0,
|
||
)
|
||
db.add(activity)
|
||
db.commit()
|
||
db.refresh(activity)
|
||
activity_id = activity.id
|
||
|
||
# 1) 尺子本身:一条好配置不该被点名
|
||
clean = add_template(db, "clean", trigger_events=["completed"])
|
||
items = findings(db)
|
||
check("体检不冤枉干净的模板",
|
||
not any(item.get("code") == clean for item in items),
|
||
str([item for item in items if item.get("code") == clean])[:300])
|
||
|
||
# 2) 接口那一条路:界面上那条横幅靠它
|
||
response = client.get("/api/v1/design/integrations/audit", headers=headers)
|
||
body = response.json() if response.status_code < 500 else {}
|
||
payload = body.get("data") or {}
|
||
check("体检接口出得来,条数与直接调用一致",
|
||
response.status_code == 200 and payload.get("count") ==
|
||
sum(len(item.get("problems") or []) for item in findings(db)),
|
||
f"{response.status_code} {str(body)[:200]}")
|
||
|
||
# 3) 旧写法指着一条已经不存在的环节:一条都不发,界面上以前什么都看不见
|
||
ghost = add_template(db, "ghost", trigger_event=models.Integration.EVENT_ACTIVITY,
|
||
event_key=GHOST_ACTIVITY)
|
||
items = findings(db)
|
||
hit = [item for item in items if item.get("code") == ghost]
|
||
check("旧写法指向已消失的环节被点名", bool(hit) and bool(
|
||
problems_of(hit, "不存在")), str(hit)[:300])
|
||
|
||
# 4) 终态那一格里混进一个环节的时刻:认不得的取值 = 静默不发
|
||
wrong = add_template(db, "wrong", trigger_events=["leave"])
|
||
items = findings(db)
|
||
hit = [item for item in items if item.get("code") == wrong]
|
||
check("终态那一格里混进环节的时刻被点名", bool(hit) and bool(
|
||
problems_of(hit, "不支持的取值")), str(hit)[:300])
|
||
|
||
# 5) 勾了重发却没带 {revision}:第二次走到这一环,上游拿不到改过的值
|
||
stale = add_template(db, "stale", refire_revisit=True,
|
||
idem_rule="{source}:{code}:{job}:{event}:{event_key}")
|
||
items = findings(db)
|
||
hit = [item for item in items if item.get("code") == stale]
|
||
check("勾了重发却没带 {revision} 被点名", bool(hit) and bool(
|
||
problems_of(hit, "{revision}")), str(hit)[:300])
|
||
|
||
# 6) 冲销模板被删了:作废那一刻只剩一个"需人工作废"的标记
|
||
gone = add_template(db, "reversal", trigger_events=["completed"], reversal_code=f"chk_cfg_nope_{STAMP}")
|
||
items = findings(db)
|
||
hit = [item for item in items if item.get("code") == gone]
|
||
check("冲销模板不存在被点名", bool(hit) and bool(problems_of(hit, "冲销模板不存在")), str(hit)[:300])
|
||
|
||
# 7) 环节挂载点名的模板早被删了:这一环节一条都不发
|
||
mount_missing = f"chk_cfg_deleted_{STAMP}"
|
||
activity.config = {**(activity.config or {}),
|
||
"writeback": {"integrations": [mount_missing], "trigger": "arrive"}}
|
||
db.commit()
|
||
items = findings(db)
|
||
check("挂载点名一个已被删掉的模板被点名",
|
||
bool(problems_of([item for item in items if item.get("kind") == "mount"],
|
||
"写回模板不存在")), str(items)[:300])
|
||
|
||
# 8) 挂在环节上又勾了终态:这一环节一次、终态那一刻一次,凭证开两张
|
||
both = add_template(db, "both", trigger_events=["completed"])
|
||
activity = db.execute(select(models.Activity).where(
|
||
models.Activity.id == activity_id)).scalar_one()
|
||
activity.config = {**(activity.config or {}),
|
||
"writeback": {"integrations": [both, mount_missing], "trigger": "arrive"}}
|
||
db.commit()
|
||
items = findings(db)
|
||
mount_items = [item for item in items if item.get("kind") == "mount"]
|
||
check("挂在环节上又勾了终态被点名(幂等键不同、上游当两笔)",
|
||
bool(problems_of(mount_items, "各发一次", "幂等键不同")), str(mount_items)[:300])
|
||
|
||
# 9) 体检说的每一处都得能落到人:模板那一半要带编码(界面点了能跳过去),
|
||
# 环节挂载与流程那一格终止挂载要带流程 id(跳流程设计器),空着就等于报了一条没法处理的病
|
||
shaped = all(
|
||
(item.get("kind") == "template" and item.get("code")) or
|
||
(item.get("kind") in ("mount", "terminate") and item.get("process"))
|
||
for item in findings(db)
|
||
)
|
||
check("每一条体检结论都带着能跳转的目标", shaped)
|
||
|
||
# 10) 这一列的形状:JSON 列不能写 DEFAULT,补列时靠回填,回填认错过一次
|
||
column = models.Integration.__table__.c.trigger_events
|
||
check("新加的 JSON 数组列补列时回填成 [](不是 {})",
|
||
_backfill_literal(column) == "'[]'", _backfill_literal(column))
|
||
check("JSON 对象列仍旧回填成 {}(别把另一头改坏)",
|
||
_backfill_literal(models.Integration.__table__.c.send_condition) == "'{}'")
|
||
check("这一列在启动自检的清洗名单里",
|
||
("oa_integration", "trigger_events") in seed.JSON_LIST_COLUMNS,
|
||
str(seed.JSON_LIST_COLUMNS))
|
||
|
||
# 11) 已经脏了的存量行:启动自检那一步真的洗得动(用刚造的这一行真洗一次)
|
||
with engine.begin() as conn:
|
||
conn.execute(text("UPDATE oa_integration SET trigger_events = CAST('{}' AS JSON)"
|
||
" WHERE code = :code"), {"code": clean})
|
||
db.rollback() # 换快照才看得见另一条连接刚改的那一行
|
||
seed._repair_json_list_columns(db)
|
||
db.commit()
|
||
with engine.begin() as conn:
|
||
shape = conn.execute(text("SELECT JSON_TYPE(trigger_events) FROM oa_integration"
|
||
" WHERE code = :code"), {"code": clean}).scalar()
|
||
check("脏成 {} 的存量行被启动自检洗成数组", str(shape) == "ARRAY", str(shape))
|
||
db.rollback()
|
||
|
||
# 12) 「本环节被退回时」挂在了条件分支上:这一环没有人按按钮,一条都不会发
|
||
branch = models.Activity(
|
||
process=process.id, type="choice", name=ACTIVITY_NAME, alias=ACTIVITY_NAME, x=0, y=0,
|
||
config={"writeback": {"integrations": [both], "trigger": "goBack"}},
|
||
)
|
||
db.add(branch)
|
||
db.commit()
|
||
mount_items = [item for item in findings(db) if item.get("kind") == "mount"]
|
||
check("分支环节配了「本环节被退回时」被点名(这一环没有这一刻)",
|
||
bool(problems_of(mount_items, "不会有这一刻")), str(mount_items)[:400])
|
||
|
||
# 13) 这一环把「撤回」按钮关了却还配着那一刻:按钮按不动,等于死配置
|
||
no_recall = models.Activity(
|
||
process=process.id, type="manual", name=ACTIVITY_NAME, alias=ACTIVITY_NAME, x=0, y=0,
|
||
config={"allowRecall": False, "writeback": {"integrations": [both], "trigger": "recall"}},
|
||
)
|
||
db.add(no_recall)
|
||
db.commit()
|
||
mount_items = [item for item in findings(db) if item.get("kind") == "mount"]
|
||
check("按钮关了还配那一刻被点名(allowRecall=false)",
|
||
bool(problems_of(mount_items, "把「撤回」按钮关了")), str(mount_items)[:400])
|
||
|
||
# 14) 一条规则勾「通过后 + 被退回」两个时刻:这次新用法的主干。幂等键里多出来的
|
||
# 那一截 :goBack 让两条报文分得开,绝不能被上面那条"重复"误伤
|
||
twice = add_template(db, "twice")
|
||
split = models.Activity(
|
||
process=process.id, type="manual", name=ACTIVITY_NAME, alias=ACTIVITY_NAME, x=0, y=0,
|
||
config={"writeback": {"integrations": [twice], "trigger": ["leave", "goBack"]}},
|
||
)
|
||
db.add(split)
|
||
db.commit()
|
||
wrongly = [text for text in problems_of([item for item in findings(db)
|
||
if item.get("kind") == "mount"], "重复")
|
||
if "体检模板 twice" in text]
|
||
check("同一模板勾两个时刻不误判成重复(幂等键分得开)", not wrongly, str(wrongly)[:300])
|
||
|
||
# 15) 反面对照:同一环节两条规则、同一条模板、时刻是「到达」与「通过后」——这两个
|
||
# 时刻在幂等键里本来就分不开,新判据不许把它们放过
|
||
twin = models.Activity(
|
||
process=process.id, type="manual", name=ACTIVITY_NAME, alias=ACTIVITY_NAME, x=0, y=0,
|
||
config={"writeback": {"blocks": [
|
||
{"integrations": [twice], "trigger": "arrive"},
|
||
{"integrations": [twice], "trigger": "leave"},
|
||
]}},
|
||
)
|
||
db.add(twin)
|
||
db.commit()
|
||
clash = [text for text in problems_of([item for item in findings(db)
|
||
if item.get("kind") == "mount"], "重复")
|
||
if "体检模板 twice" in text]
|
||
check("到达与通过后同模板仍旧被点名(幂等键分不开)", bool(clash), str(clash)[:300])
|
||
finally:
|
||
db.rollback()
|
||
db.execute(delete(models.OutboundMessage).where(models.OutboundMessage.integration.in_(CODES or [""])))
|
||
db.execute(delete(models.Integration).where(models.Integration.code.in_(CODES or [""])))
|
||
db.execute(delete(models.Activity).where(models.Activity.name == ACTIVITY_NAME))
|
||
db.execute(delete(models.DataSource).where(models.DataSource.code == SOURCE_CODE))
|
||
db.commit()
|
||
db.close()
|
||
print()
|
||
print(f"共 {PASSED + len(FAILS)} 项,失败 {len(FAILS)} 项"
|
||
+ (f";现库待修 {dirty} 条" if dirty else ""))
|
||
if dirty and not REPORT_ONLY:
|
||
print("(现库那几处按上面每一条的出路改完再跑;只想看报告就加 --report)")
|
||
return 1 if (FAILS or (dirty and not REPORT_ONLY)) else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|