-- Add hero_image_id column to globals that got heroImage upload field
ALTER TABLE "group_visits_page" ADD COLUMN IF NOT EXISTS "hero_image_id" integer;
ALTER TABLE "birthday_page"     ADD COLUMN IF NOT EXISTS "hero_image_id" integer;
ALTER TABLE "dyvolis_page"      ADD COLUMN IF NOT EXISTS "hero_image_id" integer;

-- Foreign keys (media table stores all uploaded files)
DO $$ BEGIN
  ALTER TABLE "group_visits_page"
    ADD CONSTRAINT "group_visits_page_hero_image_id_media_id_fk"
    FOREIGN KEY ("hero_image_id") REFERENCES "media"("id") ON DELETE SET NULL;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

DO $$ BEGIN
  ALTER TABLE "birthday_page"
    ADD CONSTRAINT "birthday_page_hero_image_id_media_id_fk"
    FOREIGN KEY ("hero_image_id") REFERENCES "media"("id") ON DELETE SET NULL;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

DO $$ BEGIN
  ALTER TABLE "dyvolis_page"
    ADD CONSTRAINT "dyvolis_page_hero_image_id_media_id_fk"
    FOREIGN KEY ("hero_image_id") REFERENCES "media"("id") ON DELETE SET NULL;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
