fix(06.2): WR-08 delete_document defers commit so audit log writes in same transaction
This commit is contained in:
@@ -658,11 +658,13 @@ async def delete_document(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
ok = await storage.delete_document(session, doc_id, skip_quota=is_cloud)
|
# auto_commit=False defers the commit so the audit log write below happens
|
||||||
|
# in the same transaction — avoids the split-transaction gap (WR-08).
|
||||||
|
ok = await storage.delete_document(session, doc_id, skip_quota=is_cloud, auto_commit=False)
|
||||||
if not ok:
|
if not ok:
|
||||||
raise HTTPException(404, "Document not found")
|
raise HTTPException(404, "Document not found")
|
||||||
|
|
||||||
# D-13: document deleted event — written AFTER successful delete, size_bytes only (T-04-07-02)
|
# D-13: document deleted event — written in the same transaction as the delete (WR-08).
|
||||||
await write_audit_log(
|
await write_audit_log(
|
||||||
session,
|
session,
|
||||||
event_type="document.deleted",
|
event_type="document.deleted",
|
||||||
|
|||||||
@@ -140,7 +140,12 @@ async def list_metadata(
|
|||||||
return rows
|
return rows
|
||||||
|
|
||||||
|
|
||||||
async def delete_document(session: AsyncSession, doc_id: str, skip_quota: bool = False) -> bool:
|
async def delete_document(
|
||||||
|
session: AsyncSession,
|
||||||
|
doc_id: str,
|
||||||
|
skip_quota: bool = False,
|
||||||
|
auto_commit: bool = True,
|
||||||
|
) -> bool:
|
||||||
"""Delete a document's MinIO object and its PostgreSQL row.
|
"""Delete a document's MinIO object and its PostgreSQL row.
|
||||||
|
|
||||||
Returns False if the document is not found; True on success.
|
Returns False if the document is not found; True on success.
|
||||||
@@ -149,6 +154,11 @@ async def delete_document(session: AsyncSession, doc_id: str, skip_quota: bool =
|
|||||||
|
|
||||||
skip_quota=True skips the quota decrement — used for cloud-stored documents
|
skip_quota=True skips the quota decrement — used for cloud-stored documents
|
||||||
that were never charged against the user's MinIO quota (T-06.2-03-01).
|
that were never charged against the user's MinIO quota (T-06.2-03-01).
|
||||||
|
|
||||||
|
auto_commit=False defers the session.commit() to the caller, allowing the
|
||||||
|
caller to write an audit log entry in the same transaction before committing
|
||||||
|
(avoids the split-transaction gap where a failed audit write loses the record
|
||||||
|
while the document row is already gone).
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
uid = uuid.UUID(doc_id)
|
uid = uuid.UUID(doc_id)
|
||||||
@@ -179,6 +189,7 @@ async def delete_document(session: AsyncSession, doc_id: str, skip_quota: bool =
|
|||||||
)
|
)
|
||||||
|
|
||||||
await session.delete(doc)
|
await session.delete(doc)
|
||||||
|
if auto_commit:
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user