Files
hotelsync/backend/migrations/040_fixes.sql
HotelSync 214a11ec9d fix: migration 040 — drop old loyalty_tier check constraint before renaming standard→bronze
The guests table had CHECK (loyalty_tier IN ('standard','silver','gold','platinum'))
but the UI expects 'bronze' not 'standard'. Drop the old constraint, migrate
'standard' rows to 'bronze', then add the correct constraint.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-26 11:06:44 +03:00

30 lines
1.0 KiB
SQL

-- Fix 1: unique general chat room per hotel (prevent duplicates on every open)
-- First deduplicate: keep the oldest general room per hotel, delete the rest
DELETE FROM chat_rooms
WHERE type = 'general'
AND id NOT IN (
SELECT DISTINCT ON (hotel_id) id
FROM chat_rooms
WHERE type = 'general'
ORDER BY hotel_id, created_at ASC
);
-- Now add the unique constraint
CREATE UNIQUE INDEX IF NOT EXISTS uq_chat_rooms_general_per_hotel
ON chat_rooms (hotel_id)
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'
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'
UPDATE guests
SET loyalty_tier = 'bronze'
WHERE loyalty_tier NOT IN ('bronze', 'silver', 'gold', 'platinum');