CREATE TABLE torrents_abstract (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    imdb_id text NOT NULL,
    url text NOT NULL,
    source text NOT NULL,
    quality text NOT NULL,
    upload_user text NOT NULL,
    seeders integer NOT NULL,
    leechers integer NOT NULL,
    LIKE base INCLUDING DEFAULTS
);

CREATE TABLE movie_torrents (
)
INHERITS (torrents_abstract);

CREATE TABLE episode_torrents (
    season integer NOT NULL,
    episode integer NOT NULL
)
INHERITS (torrents_abstract);

CREATE INDEX ON movie_torrents (imdb_id);
CREATE UNIQUE INDEX ON movie_torrents (imdb_id, source, quality);
CREATE INDEX ON episode_torrents (imdb_id);
CREATE INDEX ON episode_torrents (imdb_id, season, episode);
CREATE INDEX ON torrents_abstract (imdb_id);

CREATE TRIGGER update_movie_torrents_updated_at BEFORE UPDATE ON movie_torrents FOR EACH ROW EXECUTE PROCEDURE  update_updated_at_column();
CREATE TRIGGER update_episode_torrents_updated_at BEFORE UPDATE ON episode_torrents FOR EACH ROW EXECUTE PROCEDURE  update_updated_at_column();
