BEGIN;

-- test_task_status_enum
CREATE TABLE  test_task_status_enum(
    id INTEGER PRIMARY KEY,
    value VARCHAR(15)
);




INSERT INTO test_task_status_enum (id, value)
VALUES
    (0, 'created'),
    (1, 'started'),
    (2, 'completed'),
    (3, 'failed');


-- test_task
CREATE TABLE test_task (
    id INTEGER PRIMARY KEY,
    build_task_id INTEGER REFERENCES build_tasks(id) ON DELETE CASCADE,
    revision INTEGER,
    status_id INTEGER REFERENCES test_task_status_enum(id) ON DELETE SET NULL,
    package_fullname VARCHAR(100)
);

CREATE INDEX test_task_build_task_id
ON test_task(build_task_id);

CREATE INDEX test_task_build_status_id
ON test_task(status_id);

CREATE INDEX test_task_package_fullname
ON test_task(package_fullname);


-- test_step_enum
CREATE TABLE test_step_enum (
    id INTEGER PRIMARY KEY,
    value VARCHAR(50)
);

INSERT INTO test_step_enum (id, value)
VALUES
    (1, 'install_package'),
    (2, 'stop_environment'),
    (3, 'initial_provision'),
    (4, 'start_environment'),
    (5, 'uninstall_package'),
    (6, 'initialize_terraform'),
    (7, 'package_integrity_tests');



-- test_step
CREATE TABLE test_step_stats(
    test_task_id INTEGER,
    stat_name_id  INTEGER REFERENCES test_step_enum(id) ON DELETE SET NULL,
    start_ts DOUBLE PRECISION,
    end_ts DOUBLE PRECISION
);

ALTER TABLE test_step_stats
ADD CONSTRAINT test_step_stats_unique UNIQUE (test_task_id, stat_name_id);

CREATE INDEX test_step_stats_start_ts
ON test_step_stats(start_ts);

CREATE INDEX test_step_stats_end_ts
ON test_step_stats(end_ts);


UPDATE schema_version
SET version = 3;

COMMIT;