begin transaction;

create table newsgroup (
    id          INTEGER PRIMARY KEY NOT NULL,
    created_on  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_by  TEXT NOT NULL,
    name        TEXT NOT NULL,
    description TEXT NOT NULL
);

create table newsgroup_message (
    id            INTEGER PRIMARY KEY NOT NULL,
    created_on    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    newsgroup_id  INTEGER NOT NULL,
    message_id    TEXT NOT NULL UNIQUE,
    reference_ids TEXT,
    sender        TEXT NOT NULL,
    subject       TEXT NOT NULL,
    content       TEXT NOT NULL,

    FOREIGN KEY(newsgroup_id) REFERENCES newsgroup(id)
);

create index newsgroup_message_newsgroup_id_idx on newsgroup_message (
    newsgroup_id, id
);

create index newsgroup_message_newsgroup_date_idx on newsgroup_message (
    newsgroup_id, created_on
);

commit;
