-- Script to drop old foreign key constraints
-- This is the fourth part of step 5

-- Drop foreign key constraints that reference the key tables first
DO $$ 
BEGIN
    -- Drop foreign key constraints in restaurant_items table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'restaurant_items' AND constraint_name = 'restaurant_items_restaurant_id_fkey') THEN
        ALTER TABLE restaurant_items DROP CONSTRAINT restaurant_items_restaurant_id_fkey;
    END IF;
    
    -- Drop foreign key constraints in markets table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'markets' AND constraint_name = 'markets_company_id_fkey') THEN
        ALTER TABLE markets DROP CONSTRAINT markets_company_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'markets' AND constraint_name = 'markets_beach_place_id_fkey') THEN
        ALTER TABLE markets DROP CONSTRAINT markets_beach_place_id_fkey;
    END IF;
    
    -- Drop foreign key constraints in restaurants table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'restaurants' AND constraint_name = 'restaurants_company_id_fkey') THEN
        ALTER TABLE restaurants DROP CONSTRAINT restaurants_company_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'restaurants' AND constraint_name = 'restaurants_beach_place_id_fkey') THEN
        ALTER TABLE restaurants DROP CONSTRAINT restaurants_beach_place_id_fkey;
    END IF;
    
    -- Drop foreign key constraints in adventures table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'adventures' AND constraint_name = 'adventures_company_id_fkey') THEN
        ALTER TABLE adventures DROP CONSTRAINT adventures_company_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'adventures' AND constraint_name = 'adventures_beach_place_id_fkey') THEN
        ALTER TABLE adventures DROP CONSTRAINT adventures_beach_place_id_fkey;
    END IF;
END $$;