For database writes that can partially succeed (multi-step deletes/updates, bulk upserts with conflict handling), enforce invariants with atomic transactions, explicit outcome validation, and engine-specific SQL encapsulation.
Apply this standard:
@DB.atomic() (or an equivalent transaction boundary).Example (pattern):
@classmethod
@DB.atomic()
def delete_chunks(cls, chunk_ids, doc_id, kb_id):
# dedupe inputs to make expected counts deterministic
unique_ids = list(dict.fromkeys(chunk_ids or []))
if not unique_ids:
return 0
condition = {"id": unique_ids, "doc_id": doc_id}
try:
deleted_count = settings.docStoreConn.delete(
condition,
search.index_name(DocumentService.get_tenant_id(doc_id)),
kb_id,
)
except Exception:
return 0
# If partial deletion happened, restore consistency deterministically.
if deleted_count == 0:
raise ValueError("chunk deleting failure")
if deleted_count < len(unique_ids):
return settings.docStoreConn.delete(
{"doc_id": doc_id},
search.index_name(DocumentService.get_tenant_id(doc_id)),
kb_id,
)
return deleted_count
Also ensure bulk upsert/bulk insert logic handles DB-specific conflict semantics (e.g., Postgres vs MySQL) inside the DB utility layer, while still using the same atomicity and validation rules at the call site.
Enter the URL of a public GitHub repository