-- P5-5246 add designer teaser images

ALTER TABLE designers DROP CONSTRAINT IF EXISTS wallpaper_image_fk;
ALTER TABLE designers DROP CONSTRAINT IF EXISTS canvas_image_fk;
ALTER TABLE designers DROP CONSTRAINT IF EXISTS poster_image_fk;
ALTER TABLE designers DROP CONSTRAINT IF EXISTS framed_print_image_fk;

DROP TABLE IF EXISTS teaser_images;

ALTER TABLE designers
  DROP COLUMN IF EXISTS wallpaper_image_id,
  DROP COLUMN IF EXISTS canvas_image_id,
  DROP COLUMN IF EXISTS poster_image_id,
  DROP COLUMN IF EXISTS framed_print_image_id;

CREATE TABLE teaser_images
(
  id SERIAL PRIMARY KEY,
  key VARCHAR,
  UNIQUE(key)
);

ALTER TABLE designers ADD COLUMN wallpaper_image_id INTEGER,
    ADD COLUMN canvas_image_id INTEGER,
    ADD COLUMN poster_image_id INTEGER,
    ADD COLUMN framed_print_image_id INTEGER;

ALTER TABLE designers
    ADD CONSTRAINT wallpaper_image_fk
    FOREIGN KEY (wallpaper_image_id)
    REFERENCES teaser_images(id) MATCH SIMPLE
    ON DELETE SET NULL;

ALTER TABLE designers
    ADD CONSTRAINT canvas_image_fk
    FOREIGN KEY (canvas_image_id)
    REFERENCES teaser_images(id) MATCH SIMPLE
    ON DELETE SET NULL;

ALTER TABLE designers
    ADD CONSTRAINT poster_image_fk
    FOREIGN KEY (poster_image_id)
    REFERENCES teaser_images(id) MATCH SIMPLE
    ON DELETE SET NULL;

ALTER TABLE designers
    ADD CONSTRAINT framed_print_image_fk
    FOREIGN KEY (framed_print_image_id)
    REFERENCES teaser_images(id) MATCH SIMPLE
    ON DELETE SET NULL;
