HEL-270: 修复闭期补录未提交即回滚丢失(补回归测试)

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
总工
2026-08-30 22:29:52 +08:00
co-authored by multica-agent
parent e857a0e46d
commit 267ebd37f5
3 changed files with 66 additions and 34 deletions
+34 -30
View File
@@ -237,37 +237,41 @@ def record_late_arrivals(
return 0
now = utc_now()
inserted = 0
for row_id, year_month in items:
existing = connection.execute(
"SELECT id FROM period_late_arrivals WHERE source_row_id = ?",
(row_id,),
).fetchone()
if existing is not None:
continue
connection.execute(
"""
INSERT INTO period_late_arrivals (
year_month, source_row_id, status, created_at, actor_user_id, actor_username
) VALUES (?, ?, 'open', ?, ?, ?)
""",
(
# One transaction so the rows and their audit trail commit or roll back
# together; without it the caller's connection.close() silently rolled
# the late-arrival records back while the API still reported them.
with connection:
for row_id, year_month in items:
existing = connection.execute(
"SELECT id FROM period_late_arrivals WHERE source_row_id = ?",
(row_id,),
).fetchone()
if existing is not None:
continue
connection.execute(
"""
INSERT INTO period_late_arrivals (
year_month, source_row_id, status, created_at, actor_user_id, actor_username
) VALUES (?, ?, 'open', ?, ?, ?)
""",
(
year_month,
row_id,
now,
actor["id"] if actor is not None else None,
_actor_name(actor),
),
)
inserted += 1
_append_audit(
connection,
actor,
"late_arrival",
year_month,
row_id,
now,
actor["id"] if actor is not None else None,
_actor_name(actor),
),
)
inserted += 1
_append_audit(
connection,
actor,
"late_arrival",
year_month,
object_label=f"源行 {row_id}",
reason="闭期后到达的流水,未改写已结账快照",
after={"source_row_id": row_id},
)
object_label=f"源行 {row_id}",
reason="闭期后到达的流水,未改写已结账快照",
after={"source_row_id": row_id},
)
return inserted