refactor: Attachment 表关联字段 NS 更名为 SN
数据库侧已将 Common.Attachment 的 NS 列重命名为 SN,同步更新代码: - db.py: upsert_attachments 的 DELETE/INSERT 改用 [SN],distinct_sn 变量与 相关注释、日志文案统一更新 - write_attachments.py: 模块 docstring 与 convert_results_to_rows 的行列说明 由 (NS, ...) 改为 (SN, ...);写入目标描述 Common.Attachment.SN - README.md: 落库约定与字段表中 NS 全部改为 SN 已用 --sn 26B10 实测写入并用 SELECT [SN] 验证数据正确落库。 Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
This commit is contained in:
22
db.py
22
db.py
@@ -133,7 +133,7 @@ def fetch_param_by_id(
|
||||
# 附件分类落库(目标表 Common.Attachment,与源表 productionContractData 分属不同 schema)
|
||||
# ---------------------------------------------------------------------------
|
||||
# 落库约定(与项目记忆 MEMORY.md 中写入约定一致,请勿在此处用字面量以外的值):
|
||||
# - 无附件(has_attachment=false):写哨兵行 (NS, '无附件', '无')
|
||||
# - 无附件(has_attachment=false):写哨兵行 (SN, '无附件', '无')
|
||||
# - 无小类的行(coarse 真实附件 / 哨兵行):MinorCategory 统一填 '无'
|
||||
ATTACHMENT_SCHEMA = "Common"
|
||||
ATTACHMENT_TABLE = "Attachment"
|
||||
@@ -175,9 +175,9 @@ def upsert_attachments(
|
||||
) -> tuple[int, int]:
|
||||
"""幂等写入 Common.Attachment。
|
||||
|
||||
rows: 本次要写入的 (NS, MajorCategory, MinorCategory) 列表。
|
||||
对出现的每个 NS 先 DELETE 其旧行,再批量 INSERT——保证重跑总是反映
|
||||
最新分类结果,不会因唯一索引 (NS, 大类, 小类) 冲突而失败。
|
||||
rows: 本次要写入的 (SN, MajorCategory, MinorCategory) 列表。
|
||||
对出现的每个 SN 先 DELETE 其旧行,再批量 INSERT——保证重跑总是反映
|
||||
最新分类结果,不会因唯一索引 (SN, 大类, 小类) 冲突而失败。
|
||||
|
||||
返回 (deleted_rows, inserted_rows) 计数。
|
||||
"""
|
||||
@@ -185,30 +185,30 @@ def upsert_attachments(
|
||||
return (0, 0)
|
||||
|
||||
qualified = f"[{ATTACHMENT_SCHEMA}].[{ATTACHMENT_TABLE}]"
|
||||
distinct_ns = sorted({r[0] for r in rows})
|
||||
distinct_sn = sorted({r[0] for r in rows})
|
||||
|
||||
conn_str = _build_conn_str(db_cfg)
|
||||
try:
|
||||
with pyodbc.connect(conn_str, timeout=db_cfg["connect_timeout"]) as conn:
|
||||
conn.timeout = db_cfg["query_timeout"]
|
||||
cursor = conn.cursor()
|
||||
# 1) 删除本批所有 NS 的旧行
|
||||
del_ph = ",".join("?" for _ in distinct_ns)
|
||||
# 1) 删除本批所有 SN 的旧行
|
||||
del_ph = ",".join("?" for _ in distinct_sn)
|
||||
cursor.execute(
|
||||
f"DELETE FROM {qualified} WHERE [NS] IN ({del_ph})", distinct_ns
|
||||
f"DELETE FROM {qualified} WHERE [SN] IN ({del_ph})", distinct_sn
|
||||
)
|
||||
deleted = cursor.rowcount
|
||||
# 2) 插入新行
|
||||
cursor.executemany(
|
||||
f"INSERT INTO {qualified} ([NS], [MajorCategory], [MinorCategory]) "
|
||||
f"INSERT INTO {qualified} ([SN], [MajorCategory], [MinorCategory]) "
|
||||
f"VALUES (?, ?, ?)",
|
||||
rows,
|
||||
)
|
||||
inserted = len(rows)
|
||||
conn.commit()
|
||||
logger.info(
|
||||
"upsert_attachments: 删除 %d 行, 插入 %d 行, 涉及 %d 个 NS",
|
||||
deleted, inserted, len(distinct_ns),
|
||||
"upsert_attachments: 删除 %d 行, 插入 %d 行, 涉及 %d 个 SN",
|
||||
deleted, inserted, len(distinct_sn),
|
||||
)
|
||||
return (deleted, inserted)
|
||||
except pyodbc.Error as e:
|
||||
|
||||
Reference in New Issue
Block a user