db: add missing delete_case (cases_tools.case_delete was calling a ghost)
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 1m30s

The case_delete tool in tools/cases.py and the DELETE /api/cases endpoint
in web/app.py both invoke await db.delete_case(case_id), but no such
function existed in services/db.py — every call returned 500 with an
AttributeError. Discovered while wiping case 8174-24 for a clean rerun.

Implementation is straightforward because the FK graph already does the
work: 7 dependent tables CASCADE on cases.id (documents, document_chunks,
claims, appraiser_facts, decisions, qa_results, case_precedents) and 2
SET NULL (audit_log, chair_feedback). A single DELETE FROM cases is
enough — no manual ordering needed.

Documented in the docstring that this only touches the legal-ai DB —
Paperclip projects/issues and Gitea repos for the case are separate
systems and must be cleaned up by the caller.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-30 14:44:44 +00:00
parent 28f49defff
commit 903fb4d140

View File

@@ -681,6 +681,26 @@ async def restore_case(case_id: UUID) -> dict | None:
return _row_to_case(row) if row else None return _row_to_case(row) if row else None
async def delete_case(case_id: UUID) -> bool:
"""Delete a case row. Returns True if a row was actually removed.
All dependent rows are removed automatically by FK constraints:
• CASCADE: documents, document_chunks, claims, appraiser_facts,
decisions, qa_results, case_precedents
• SET NULL: audit_log.case_id, chair_feedback.case_id
NOTE: this only touches the legal-ai database. The Paperclip project
(issues, comments, runs) and Gitea repo for the case live in other
systems and are NOT cleaned up here — call sites that need a full
reset must handle those separately.
"""
pool = await get_pool()
async with pool.acquire() as conn:
result = await conn.execute("DELETE FROM cases WHERE id = $1", case_id)
# asyncpg execute returns "DELETE <n>" — extract count.
return int(result.split()[-1]) > 0
# ── Document CRUD ─────────────────────────────────────────────────── # ── Document CRUD ───────────────────────────────────────────────────
async def create_document( async def create_document(