feat(db): migration 0014 relaxes claims/remittances PK to (batch_id, id)
Migration 0014 changes the PRIMARY KEYs of claims and remittances from single-column id to composite (batch_id, id). Enables the spec'd cross-batch CLM01 / CLP01 collision workflow: same CLM01 in multiple batches is now representable (resubmits), pre-flight dedup 409 path is genuinely exercisable, force-insert can skip pre-existing duplicates. Strategy: PRAGMA defer_foreign_keys = ON + table recreation for every table whose FKs pointed at the old single-column PK. Tables recreated: remittances, claims, matches, cas_adjustments, service_line_payments, line_reconciliations. Each child table gains a batch_id (or remittance_batch_id) column for the composite FK side; INSERT INTO new SELECT FROM old JOINs populate it from the already-recreated parent. Cross-table FKs (remittances.claim_id, claims.matched_remittance_id) cannot be SQL-enforced with composite PKs (SQLite has no ALTER CONSTRAINT). Dropped at SQL level; enforced via application-layer invariants in store.manual_match / manual_unmatch / reconcile.run and the dedup.preflight_* helpers. ORM updates (db.py): - Claim / Remittance: composite PK via explicit PrimaryKeyConstraint in __table_args__ (column order matches SQL: batch_id, id). - New Claim.matched_remittance_batch_id column. - Match / CasAdjustment / ServiceLinePayment / LineReconciliation: added the batch side of their composite FK to the parent table. - SQLAlchemy before_insert events auto-populate the batch side of the composite FK from session.new, then identity_map, then SQL fallback. Production code updates: - store.manual_match / manual_unmatch: also write matched_remittance_batch_id on the claim (was missing). - api.py manual-match endpoint: same fix. - reconcile.run: same fix for auto-matched pairs. Test updates: replaced s.get(Claim, X) with the composite key (batch_id, id) where batch_id is known, or s.query().filter().first() where the test only knows the id. Tests that previously inserted a Match row pointing at a non-existent Remittance now seed the parent Remittance so the new NOT NULL composite FK is satisfied.
This commit is contained in:
@@ -111,7 +111,9 @@ def test_rejection_moves_submitted_claim_to_rejected_state():
|
||||
assert result.matched == ["CLP-1"]
|
||||
assert result.orphans == []
|
||||
with db.SessionLocal()() as s:
|
||||
c = s.get(Claim, "CLP-1")
|
||||
# Migration 0014: composite PK — claim is in batch "B-1"; use
|
||||
# filter-by-id since the test has only one claim with this id.
|
||||
c = s.query(Claim).filter(Claim.id == "CLP-1").first()
|
||||
assert c.state == ClaimState.REJECTED
|
||||
assert c.rejected_at is not None
|
||||
assert "999" in (c.rejection_reason or "")
|
||||
@@ -130,7 +132,8 @@ def test_accepted_set_leaves_claim_alone():
|
||||
|
||||
assert result.matched == []
|
||||
with db.SessionLocal()() as s:
|
||||
c = s.get(Claim, "CLP-1")
|
||||
# Migration 0014: composite PK — see test_rejection_moves...
|
||||
c = s.query(Claim).filter(Claim.id == "CLP-1").first()
|
||||
assert c.state == ClaimState.SUBMITTED
|
||||
assert c.rejected_at is None
|
||||
assert c.rejection_reason is None
|
||||
@@ -162,5 +165,6 @@ def test_already_rejected_claim_is_idempotent():
|
||||
|
||||
assert result.matched == [] # no-op
|
||||
with db.SessionLocal()() as s:
|
||||
c = s.get(Claim, "CLP-1")
|
||||
# Migration 0014: composite PK — see test_rejection_moves...
|
||||
c = s.query(Claim).filter(Claim.id == "CLP-1").first()
|
||||
assert c.state == ClaimState.REJECTED
|
||||
|
||||
Reference in New Issue
Block a user