-- Flyway Migration: V2.3.0.3
-- Purpose: Create COLLECTION and COLLECTION_GAMES tables for Collections feature.

CREATE SEQUENCE COLLECTION_SEQ INCREMENT BY 50;

CREATE TABLE COLLECTION
(
    ID                  BIGINT                   NOT NULL PRIMARY KEY,
    CREATED_AT          TIMESTAMP WITH TIME ZONE NOT NULL,
    UPDATED_AT          TIMESTAMP WITH TIME ZONE NOT NULL,
    NAME                CHARACTER VARYING(255)   NOT NULL UNIQUE,
    DESCRIPTION         CHARACTER LARGE OBJECT,
    DISPLAY_ON_HOMEPAGE BOOLEAN DEFAULT TRUE,
    DISPLAY_ORDER       INT     DEFAULT -1
);

CREATE TABLE COLLECTION_GAMES
(
    COLLECTIONS_ID BIGINT NOT NULL,
    GAMES_ID       BIGINT NOT NULL,
    PRIMARY KEY (COLLECTIONS_ID, GAMES_ID),
    CONSTRAINT FK_COLLECTION_GAMES_COLLECTION FOREIGN KEY (COLLECTIONS_ID) REFERENCES COLLECTION ON DELETE CASCADE,
    CONSTRAINT FK_COLLECTION_GAMES_GAME FOREIGN KEY (GAMES_ID) REFERENCES GAME ON DELETE CASCADE
);

-- Indexes to speed up lookup by GAMES_ID and COLLECTIONS_ID
CREATE INDEX IDX_COLLECTION_GAMES_GAME_ID ON COLLECTION_GAMES (GAMES_ID);
CREATE INDEX IDX_COLLECTION_GAMES_COLLECTION_ID ON COLLECTION_GAMES (COLLECTIONS_ID);

