add guard clause when backfilling plan_id to recommendation

This commit is contained in:
Daniel Roth 2026-06-05 10:13:49 +00:00
parent 694bac47e1
commit d2c9c0696a

View file

@ -1,3 +1,21 @@
-- Guard: fail if any recommendation is linked to more than one plan.
-- The spec asserts each recommendation belongs to exactly one plan; if real
-- data violates that the backfill cannot pick deterministically and must not
-- proceed silently.
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM plan_recommendations
GROUP BY recommendation_id
HAVING count(*) > 1
) THEN
RAISE EXCEPTION
'plan_id backfill aborted: one or more recommendations appear in multiple '
'plan_recommendations rows. Resolve cardinality before re-running.';
END IF;
END $$;
UPDATE recommendation r
SET plan_id = pr.plan_id
FROM plan_recommendations pr