hotfix(0031): DROP IF EXISTS before ADD (constraint pre-exists on PROD)#88
Conversation
…g constraint PROD already has `outcome_requires_inference_when_model_chosen` with an OLDER predicate (decision_rule <> 'cheapest_clearing_floor' OR inference_id IS NOT NULL) — added by the human+Claude repair session earlier today. Two issues with the old predicate: 1. PG CHECK can't be DEFERRABLE → it fires at statement time, breaking the two-phase outcome write (insert_decision sets the row with NULL inference_id; complete_decision links it). 2. The W1 code now uses decision_rule_override on pre-dispatch fails, so the predicate's left side already evaluates true for those paths; the old predicate didn't account for that explicitly. Fix: 0031 DROPs IF EXISTS first, then ADDs the new predicate with the `outcome_status IS NULL` escape clause for mid-flight rows. Idempotent for fresh databases (DROP IF EXISTS is a no-op there). Refs: AIN-300 · live PROD 2026-05-28 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
AIN-300 🔴 [DB/Gateway] routing_outcomes orphaned (NULL inference_id) on provider error — write-path linkage + Mistral 429 failover
🔴 Live data-integrity bug — routing_outcomes written without inference_id linkageFound 2026-05-28 during founder-directed DB save-error investigation on prod ( Symptom (DATA repaired — root cause still open)
Root cause (hypothesis — confirm in code)The gateway write path appears to be: (1) create/persist the Two defects, one cause:
Required fix (api repo —
|
Constraint was pre-applied to PROD by the repair team earlier today with the OLDER predicate (no mid-flight escape). Migration now DROPs it before re-ADDing with the AIN-300 W1 predicate.
Note
Low Risk
Schema-only migration idempotency fix; no application logic changes in this diff.
Overview
Makes Alembic revision 20260528_0031 safe to run on production, where the same-named CHECK constraint was already applied earlier with an older predicate that lacks the
outcome_status IS NULLmid-flight escape and can break the two-phaseinsert_decision→complete_decisionwrite path.The migration now
DROP CONSTRAINT IF EXISTS outcome_requires_inference_when_model_chosenonrouting_outcomesbefore re-adding the constraint with the AIN-300 W1 predicate (outcome_status IS NULL OR …), so deploys succeed whether or not the prior manual constraint is present. Comments in the migration document the PROD repair context and idempotent behavior on fresh databases.Reviewed by Cursor Bugbot for commit d17a812. Bugbot is set up for automated code reviews on this repo. Configure here.