schema initialisation

This commit is contained in:
dragonmuffin 2024-07-27 14:27:14 +05:00
parent 2fa110e760
commit 70dbcd1bc0

View file

@ -0,0 +1,69 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS tests
(
id serial NOT NULL,
problem_id INT NOT NULL,
PRIMARY KEY (id),
);
CREATE INDEX ON users USING B-TREE (id);
CREATE INDEX ON users USING B-TREE (problem_id);
CREATE TABLE IF NOT EXISTS languages
(
id serial NOT NULL,
name VARCHAR(60) NOT NULL,
build_file_hash CHAR(128) NOT NULL,
execute_file_hash CHAR(128) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (id),
);
CREATE FUNCTION lng_upd_trg_fn() RETURNS TRIGGER
LANGUAGE plpgsql AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
CREATE TRIGGER lng_upd_trg
BEFORE UPDATE
ON languages
FOR EACH ROW
EXECUTE PROCEDURE lng_upd_trg_fn();
CREATE TABLE IF NOT EXISTS solutions
(
id serial NOT NULL,
user_id INT REFERENCES users ON DELETE CASCADE,
problem_id INT REFERENCES problems ON DELETE CASCADE,
language_id INT REFERENCES languages ON DELETE CASCADE,
contest_id INT NOT NULL,
solution_hash CHAR(128) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (id),
CHECK (length(name) != 0),
CHECK (length(description) != 0)
);
CREATE INDEX ON users USING B-TREE (id);
CREATE INDEX ON users USING B-TREE (id,user_id,problem_id,language_id);
-- +goose StatementEnd
-- +goose Down
DROP TABLE IF EXISTS tests;
DROP TABLE IF EXISTS solutions;