Files
group_fqcd_jr/tools/normalize_memory_sync_outbox.py
T
张胜宇 e239eb778b docs: 品牌全量口径统一为「南方基金」+ 作废文档清理
1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富
   统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」;
   同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。
2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本),
   新增《文档规整方案与开发前待决事项-2026-09-17》。
3) 客服agent 四份交付文档首次纳入本分支。
2026-09-17 15:15:22 +08:00

92 lines
3.2 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.
"""数据订正:把 `memory_sync_outbox` 的历史取值改齐为全仓小写口径。
**默认 dry-run**(只报告将影响几行),加 `--apply` 才真正提交。幂等,可重复运行。
### 背景
`docs/00` §6.4.6 的取值栏曾写作大写 `MILVUS`/`NEO4J`、`UPSERT` 与中文 `待处理`,
与全仓实现从未对齐。按那份文档写入的事件会**静默失效**:
- 消费端 `MemorySyncOutboxWorker` 按 `handlers.get(event.target_store)` 分派 handler;
- 领取条件是 `status.in_({"pending","failed"})`。
大写 + 中文两个条件都不满足 ⇒ 事件任何消费者都领不到、永久滞留且不报错
(唯一键 `(event_uuid, target_store)` 对大小写没有约束,MySQL 也不会报错)。
因此凡是在本仓写入过 `memory_sync_outbox` 的环境,都可能有这类脏行。
### 安全口径
- 只改 `target_store` / `operation` / `status` 的**值**;
- **不触碰**主键、唯一键 `uk_memory_sync_event (event_uuid, target_store)`、
`payload`、时间戳、`retry_count`;
- 就地改值不会新增行(两组取值大小写不同,本会各自成行,这里把它们并到正确的一组);
- 中文字符不受 `LOWER()` 影响(非 ASCII 字节不变),故 `LOWER()` 是安全的。
用法:
.\\.venv\\Scripts\\python.exe tools\\normalize_memory_sync_outbox.py # 先看
.\\.venv\\Scripts\\python.exe tools\\normalize_memory_sync_outbox.py --apply # 再改
"""
import asyncio
import sys
from sqlalchemy import text
from app.infrastructure.db import engine
_STATUS_CASE = """
CASE status
WHEN '待处理' THEN 'pending'
WHEN '处理中' THEN 'processing'
WHEN '已完成' THEN 'processed'
WHEN '失败' THEN 'failed'
ELSE LOWER(status)
END
"""
_UPDATE = text(f"""
UPDATE memory_sync_outbox
SET target_store = LOWER(target_store),
operation = LOWER(operation),
status = {_STATUS_CASE}
WHERE target_store <> LOWER(target_store)
OR operation <> LOWER(operation)
OR status <> {_STATUS_CASE}
""")
_GROUP = text(
"SELECT target_store, operation, status, COUNT(*) n "
"FROM memory_sync_outbox GROUP BY target_store, operation, status"
)
_DETAIL = text(
"SELECT id, event_uuid, target_store, operation, status, retry_count "
"FROM memory_sync_outbox ORDER BY id"
)
async def main(apply: bool) -> int:
async with engine.connect() as conn:
before = (await conn.execute(_GROUP)).mappings().all()
print("改前:", [dict(row) for row in before] or "(空表)")
affected = (await conn.execute(_UPDATE)).rowcount
print("将订正行数:", affected)
if not apply:
print("dry-run:未提交。确认无误后加 --apply 再运行。")
await conn.rollback()
return 0
await conn.commit()
after = (await conn.execute(_GROUP)).mappings().all()
print("改后:", [dict(row) for row in after] or "(空表)")
for row in (await conn.execute(_DETAIL)).mappings().all():
print(" ", dict(row))
return 0
if __name__ == "__main__":
sys.exit(asyncio.run(main("--apply" in sys.argv)))