-- Ensure marketing_products FK is CASCADE (it should already be, but let's make sure)
DO $$
BEGIN
    -- Drop existing FK if exists
    IF EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'fk_marketing_products_marketing_id'
    ) THEN
        ALTER TABLE marketing_products DROP CONSTRAINT fk_marketing_products_marketing_id;
    END IF;

    -- Recreate with ON DELETE CASCADE
    ALTER TABLE marketing_products
        ADD CONSTRAINT fk_marketing_products_marketing_id
        FOREIGN KEY (marketing_id) REFERENCES marketings(id)
        ON DELETE CASCADE;
END $$;

-- Ensure marketing_delivery_products FK is CASCADE
DO $$
BEGIN
    -- Drop existing FK if exists
    IF EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'fk_marketing_delivery_products_marketing_product_id'
    ) THEN
        ALTER TABLE marketing_delivery_products DROP CONSTRAINT fk_marketing_delivery_products_marketing_product_id;
    END IF;

    -- Recreate with ON DELETE CASCADE
    ALTER TABLE marketing_delivery_products
        ADD CONSTRAINT fk_marketing_delivery_products_marketing_product_id
        FOREIGN KEY (marketing_product_id) REFERENCES marketing_products(id)
        ON DELETE CASCADE;
END $$;
