From 5f8aebf5b1fb4a7225c8112097917ad6a92d987c Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Sat, 14 Mar 2026 21:45:00 +0100 Subject: [PATCH] fix: make migrations 048/049 safe for environments without canonical tables Migrations 048 and 049 reference canonical_processed_chunks and canonical_controls tables which may not exist on all environments. Wrap ALTER TABLE statements in DO blocks that check for table existence first. This unblocks migrations 050-053 on production. Co-Authored-By: Claude Opus 4.6 --- .../migrations/048_processing_path_expand.sql | 33 +++++++++++-------- .../migrations/049_target_audience.sql | 15 ++++++--- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/backend-compliance/migrations/048_processing_path_expand.sql b/backend-compliance/migrations/048_processing_path_expand.sql index 2712239..0cd6e71 100644 --- a/backend-compliance/migrations/048_processing_path_expand.sql +++ b/backend-compliance/migrations/048_processing_path_expand.sql @@ -1,17 +1,22 @@ -- 048: Expand processing_path CHECK constraint for new pipeline paths -- New values: prefilter_skip, no_control, store_failed, error +-- Safe: only runs if the table exists (may not exist on all environments) -ALTER TABLE canonical_processed_chunks - DROP CONSTRAINT IF EXISTS canonical_processed_chunks_processing_path_check; - -ALTER TABLE canonical_processed_chunks - ADD CONSTRAINT canonical_processed_chunks_processing_path_check - CHECK (processing_path IN ( - 'structured', -- Rule 1/2: structured from original text - 'llm_reform', -- Rule 3: LLM reformulated - 'skipped', -- Legacy: skipped for other reasons - 'prefilter_skip', -- Local LLM determined chunk has no requirement - 'no_control', -- Processing ran but no control could be derived - 'store_failed', -- Control generated but DB store failed - 'error' -- Processing error occurred - )); +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'canonical_processed_chunks') THEN + ALTER TABLE canonical_processed_chunks + DROP CONSTRAINT IF EXISTS canonical_processed_chunks_processing_path_check; + ALTER TABLE canonical_processed_chunks + ADD CONSTRAINT canonical_processed_chunks_processing_path_check + CHECK (processing_path IN ( + 'structured', + 'llm_reform', + 'skipped', + 'prefilter_skip', + 'no_control', + 'store_failed', + 'error' + )); + END IF; +END $$; diff --git a/backend-compliance/migrations/049_target_audience.sql b/backend-compliance/migrations/049_target_audience.sql index 7d1f93c..a6d33c1 100644 --- a/backend-compliance/migrations/049_target_audience.sql +++ b/backend-compliance/migrations/049_target_audience.sql @@ -1,8 +1,13 @@ -- 049: Add target_audience field to canonical_controls -- Distinguishes who a control is relevant for: enterprises, authorities, providers, or all. +-- Safe: only runs if the table exists (may not exist on all environments) -ALTER TABLE canonical_controls ADD COLUMN IF NOT EXISTS - target_audience VARCHAR(20) DEFAULT NULL - CHECK (target_audience IN ('enterprise', 'authority', 'provider', 'all')); - -CREATE INDEX IF NOT EXISTS idx_cc_target_audience ON canonical_controls(target_audience); +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'canonical_controls') THEN + ALTER TABLE canonical_controls ADD COLUMN IF NOT EXISTS + target_audience VARCHAR(20) DEFAULT NULL + CHECK (target_audience IN ('enterprise', 'authority', 'provider', 'all')); + CREATE INDEX IF NOT EXISTS idx_cc_target_audience ON canonical_controls(target_audience); + END IF; +END $$;