-- builds
DROP TABLE IF EXISTS builds CASCADE;
CREATE TABLE builds (
    id INTEGER PRIMARY KEY,
    url VARCHAR(50) NOT NULL,
    created_at REAL NOT NULL,
    finished_at REAL
);


CREATE INDEX IF NOT EXISTS builds_created_at
ON builds(created_at);

CREATE INDEX IF NOT EXISTS builds_finished_at
ON builds(finished_at);


-- build_taks_enum
DROP TABLE IF EXISTS build_task_status_enum CASCADE;
CREATE TABLE IF NOT EXISTS build_task_status_enum(
    id INTEGER PRIMARY KEY,
    value VARCHAR(15)
);

INSERT INTO build_task_status_enum (id, value)
VALUES
    (0, 'idle'),
    (1, 'started'),
    (2, 'completed'),
    (3, 'failed'),
    (4, 'excluded');


-- arch_enum
DROP TABLE IF EXISTS arch_enum CASCADE;
CREATE TABLE arch_enum(
    id INTEGER PRIMARY KEY,
    value VARCHAR(15)
);

INSERT INTO arch_enum(id, value)
VALUES
    (0, 'i686'),
    (1, 'x86_64'),
    (2, 'aarch64'),
    (3, 'ppc64le'),
    (4, 's390x');


-- build_tasks
DROP TABLE IF EXISTS build_tasks CASCADE;
CREATE TABLE build_tasks (
    id INTEGER PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    build_id INTEGER REFERENCES builds(id) ON DELETE CASCADE,
    arch_id INTEGER REFERENCES arch_enum(id) ON DELETE SET NULL,
    status_id INTEGER REFERENCES build_task_status_enum(id) ON DELETE SET NULL,
    started_at REAL,
    finished_at REAL
);

CREATE INDEX build_tasks_build_id
ON build_tasks(build_id);

CREATE INDEX build_tasks_started_at
ON build_tasks(started_at);

CREATE INDEX build_tasks_finished_at
ON build_tasks(finished_at);


-- sign_tasks
DROP TABLE IF EXISTS sign_tasks CASCADE;
CREATE TABLE sign_tasks (
    id INTEGER PRIMARY KEY,
    build_id INTEGER REFERENCES builds(id) ON DELETE CASCADE,
    buildtask_id INTEGER REFERENCES build_tasks(id) ON DELETE CASCADE,
    started_at REAL,
    finished_at REAL
);

CREATE INDEX sign_tasks_build_id
ON sign_tasks(build_id);

CREATE INDEX sign_tasks_buildtask_id
ON sign_tasks(buildtask_id);

CREATE INDEX sing_tasks_started_at
ON sign_tasks(started_at);

CREATE INDEX sign_tasks_finished_at
ON sign_tasks(finished_at);
