From be58b16365a4dfa5cfd207a3a3f7d499c375ba67 Mon Sep 17 00:00:00 2001 From: HotelSync Date: Thu, 26 Mar 2026 11:12:47 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20migration=20040=20=E2=80=94=20run=20UPDA?= =?UTF-8?q?TE=20before=20ADD=20CONSTRAINT=20(correct=20order)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ALTER TABLE ADD CONSTRAINT validates existing rows immediately, so the UPDATE must run first to migrate 'standard' → 'bronze' before the new constraint is applied. Co-Authored-By: Claude Sonnet 4.6 --- backend/migrations/040_fixes.sql | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/backend/migrations/040_fixes.sql b/backend/migrations/040_fixes.sql index 73898db..4657077 100644 --- a/backend/migrations/040_fixes.sql +++ b/backend/migrations/040_fixes.sql @@ -15,15 +15,15 @@ CREATE UNIQUE INDEX IF NOT EXISTS uq_chat_rooms_general_per_hotel WHERE type = 'general'; -- Fix 2: replace 'standard' tier with 'bronze' (the UI uses bronze/silver/gold/platinum) --- Drop the old check constraint that only allows 'standard' instead of 'bronze' +-- Drop the old check constraint first ALTER TABLE guests DROP CONSTRAINT IF EXISTS guests_loyalty_tier_check; --- Add the correct constraint -ALTER TABLE guests - ADD CONSTRAINT guests_loyalty_tier_check - CHECK (loyalty_tier IN ('bronze', 'silver', 'gold', 'platinum')); - --- Migrate existing 'standard' rows to 'bronze' +-- Migrate existing 'standard' rows to 'bronze' (must happen BEFORE adding the new constraint) UPDATE guests SET loyalty_tier = 'bronze' WHERE loyalty_tier NOT IN ('bronze', 'silver', 'gold', 'platinum'); + +-- Now add the correct constraint +ALTER TABLE guests + ADD CONSTRAINT guests_loyalty_tier_check + CHECK (loyalty_tier IN ('bronze', 'silver', 'gold', 'platinum'));