
CREATE TABLE releases
(
  release_id TEXT PRIMARY KEY,
  aid INTEGER NOT NULL,
  release_type INTEGER NOT NULL,
  release_data jsonb,
  release_date INTEGER
);
CREATE INDEX releases_aid_key ON releases(aid);

CREATE TABLE episodes_releases
(
  aid INTEGER NOT NULL,
  episode INTEGER NOT NULL,
  release_id TEXT NOT NULL REFERENCES releases(release_id) ON DELETE CASCADE ON UPDATE CASCADE,
  release_type INTEGER NOT NULL,
  release_data jsonb NOT NULL

);
CREATE INDEX episodes_releases_aid_key ON episodes_releases(aid);
CREATE INDEX episodes_releases_release_id_key ON episodes_releases(release_id);
ALTER TABLE ONLY episodes_releases ADD CONSTRAINT episodes_releases_pkey PRIMARY KEY (release_id, aid, episode, release_type);

CREATE TABLE tracked_releases
(
  aid INTEGER NOT NULL,
  episode INTEGER NOT NULL,
  release_id TEXT NOT NULL REFERENCES releases(release_id) ON DELETE CASCADE ON UPDATE CASCADE,
  release_type INTEGER NOT NULL,
  release_data jsonb NOT NULL
);
CREATE INDEX tracked_releases_aid_key ON tracked_releases(aid);
CREATE INDEX tracked_releases_release_id_key ON tracked_releases(release_id);
ALTER TABLE ONLY tracked_releases ADD CONSTRAINT tracked_releases_pkey PRIMARY KEY (release_id, aid, episode, release_type);

CREATE TABLE tracked
(
  aid INTEGER PRIMARY KEY,
  episode_count INTEGER,
  prefer_groups TEXT,
  download_folder TEXT NOT NULL,
  flags INTEGER
);

CREATE TABLE alternate_titles
(
  aid INTEGER,
  title TEXT NOT NULL,
  type INTEGER,
  language TEXT NOT NULL
);

ALTER TABLE ONLY alternate_titles ADD CONSTRAINT alternate_titles_pkey PRIMARY KEY (aid, type, language, title);
