ms-tester/migrations/20240727123308_initial.sql
Vyacheslav1557 be25404852 feat:
2024-10-13 19:01:36 +05:00

151 lines
4.6 KiB
PL/PgSQL

-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS problems
(
id serial NOT NULL,
title varchar(255) NOT NULL,
content varchar(65536) NOT NULL DEFAULT '',
time_limit integer NOT NULL DEFAULT 1000,
memory_limit integer NOT NULL DEFAULT 65536,
testing_strategy integer NOT NULL DEFAULT 1,
testing_order varchar(1024) NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
CHECK (length(title) != 0),
CHECK (memory_limit > 0),
CHECK (time_limit > 0),
CHECK (testing_strategy > 0)
);
CREATE TRIGGER on_problems_update
BEFORE UPDATE
ON problems
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE TABLE IF NOT EXISTS contests
(
id serial NOT NULL,
title varchar(255) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
CHECK (length(title) != 0)
);
CREATE TRIGGER on_contests_update
BEFORE UPDATE
ON contests
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE TABLE IF NOT EXISTS tasks
(
id serial NOT NULL,
problem_id integer NOT NULL REFERENCES problems (id),
contest_id integer NOT NULL REFERENCES contests (id),
position integer NOT NULL,
prefix varchar(10) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
UNIQUE (problem_id, contest_id),
CHECK (position >= 0)
);
CREATE TRIGGER on_tasks_update
BEFORE UPDATE
ON tasks
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE TABLE IF NOT EXISTS participants
(
id serial NOT NULL,
user_id integer NOT NULL,
contest_id integer NOT NULL REFERENCES contests (id),
name varchar(255) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
UNIQUE (user_id, contest_id),
CHECK (length(name) != 0)
);
CREATE TRIGGER on_participants_update
BEFORE UPDATE
ON participants
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE TABLE IF NOT EXISTS solutions
(
id serial NOT NULL,
task_id integer NOT NULL REFERENCES tasks (id),
participant_id integer NOT NULL REFERENCES participants (id),
solution varchar(1048576) NOT NULL,
state integer NOT NULL DEFAULT 1,
results varchar(1000) NOT NULL,
score integer NOT NULL,
penalty integer NOT NULL,
total_score integer NOT NULL,
language integer NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id)
);
CREATE TRIGGER on_solutions_update
BEFORE UPDATE
ON solutions
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE TABLE IF NOT EXISTS best_solutions
(
id serial NOT NULL,
participant_id integer NOT NULL REFERENCES participants (id),
task_id integer NOT NULL REFERENCES tasks (id),
solution_id integer NOT NULL REFERENCES solutions (id),
best_total_score integer NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
UNIQUE (participant_id, task_id)
);
CREATE TRIGGER on_best_solutions_update
BEFORE UPDATE
ON best_solutions
FOR EACH ROW
EXECUTE PROCEDURE updated_at_update();
CREATE FUNCTION updated_at_update() RETURNS TRIGGER
LANGUAGE plpgsql AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TRIGGER IF EXISTS on_problems_update ON problems;
DROP TABLE IF EXISTS problems;
DROP TRIGGER IF EXISTS on_contests_update ON contests;
DROP TABLE IF EXISTS contests;
DROP TRIGGER IF EXISTS on_tasks_update ON tasks;
DROP TABLE IF EXISTS tasks;
DROP TRIGGER IF EXISTS on_participants_update ON participants;
DROP TABLE IF EXISTS participants;
DROP TRIGGER IF EXISTS on_solutions_update ON solutions;
DROP TABLE IF EXISTS solutions;
DROP TRIGGER IF EXISTS on_best_solutions_update ON best_solutions;
DROP TABLE IF EXISTS best_solutions;
DROP FUNCTION updated_at_update();
-- +goose StatementEnd