backfill materials columns for recommendation table

This commit is contained in:
Daniel Roth 2026-06-05 10:13:31 +00:00
parent 62b10ef808
commit 694bac47e1
3 changed files with 10803 additions and 0 deletions

View file

@ -0,0 +1,26 @@
-- Guard: fail if any recommendation maps to more than one material row.
-- The modelled fabric measures never produce this; if real data violates it
-- the backfill cannot pick deterministically and must not proceed silently.
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM recommendation_materials
GROUP BY recommendation_id
HAVING count(*) > 1
) THEN
RAISE EXCEPTION
'recommendation_material_id backfill aborted: one or more recommendations '
'have multiple recommendation_materials rows. Resolve cardinality before re-running.';
END IF;
END $$;
-- Backfill all four columns in a single pass.
UPDATE recommendation r
SET material_id = rm.material_id,
material_quantity = rm.quantity,
material_quantity_unit = rm.quantity_unit,
material_depth = rm.depth
FROM recommendation_materials rm
WHERE rm.recommendation_id = r.id
AND r.material_id IS NULL;

File diff suppressed because it is too large Load diff

View file

@ -1569,6 +1569,13 @@
"when": 1780653770494,
"tag": "0224_busy_nitro",
"breakpoints": true
},
{
"idx": 225,
"version": "7",
"when": 1780654800000,
"tag": "0225_recommendation_material_id_backfill",
"breakpoints": true
}
]
}