-- 930_add_missing_foreign_keys_down.sql
-- Rollback: Remove foreign key constraints added by 930

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'tracks' AND constraint_name = 'fk_tracks_user_id') THEN
        ALTER TABLE public.tracks DROP CONSTRAINT fk_tracks_user_id;
    END IF;
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'rooms' AND constraint_name = 'fk_rooms_owner_id') THEN
        ALTER TABLE public.rooms DROP CONSTRAINT fk_rooms_owner_id;
    END IF;
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'messages' AND constraint_name = 'fk_messages_user_id') THEN
        ALTER TABLE public.messages DROP CONSTRAINT fk_messages_user_id;
    END IF;
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'messages' AND constraint_name = 'fk_messages_parent_id') THEN
        ALTER TABLE public.messages DROP CONSTRAINT fk_messages_parent_id;
    END IF;
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'audit_logs' AND constraint_name = 'fk_audit_logs_user_id') THEN
        ALTER TABLE public.audit_logs DROP CONSTRAINT fk_audit_logs_user_id;
    END IF;
END $$;
