-CREATE TABLE users (
+CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
passphrase TEXT, -- NOT NULL,
admin BOOLEAN NOT NULL DEFAULT FALSE,
since BIGINT DEFAULT CAST(EXTRACT(epoch from now()) AS bigint)
);
-CREATE TABLE contests (
+CREATE TABLE IF NOT EXISTS contests (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
editorial TEXT,
CONSTRAINT positive_duration CHECK (stop > start)
);
-CREATE TABLE contest_status (
+CREATE TABLE IF NOT EXISTS contest_status (
contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE,
owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
score INT NOT NULL,
PRIMARY KEY (owner, contest)
);
-CREATE TABLE problems (
+CREATE TABLE IF NOT EXISTS problems (
id TEXT PRIMARY KEY,
author TEXT,
writer TEXT,
versource TEXT
);
-CREATE TABLE contest_problems (
+CREATE TABLE IF NOT EXISTS contest_problems (
contest TEXT REFERENCES contests ON DELETE CASCADE,
problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
PRIMARY KEY (contest, problem)
);
-CREATE TABLE jobs (
+CREATE TABLE IF NOT EXISTS jobs (
id SERIAL PRIMARY KEY,
contest TEXT REFERENCES contests ON DELETE CASCADE,
daemon TEXT,
owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE
);
-CREATE TABLE problem_status (
+CREATE TABLE IF NOT EXISTS problem_status (
problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
job SERIAL NOT NULL REFERENCES jobs ON DELETE CASCADE,
PRIMARY KEY (owner, problem)
);
-CREATE TABLE opens (
+CREATE TABLE IF NOT EXISTS opens (
contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE,
problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
PRIMARY KEY (contest, problem, owner)
);
-CREATE TABLE limits (
+CREATE TABLE IF NOT EXISTS limits (
problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
format TEXT NOT NULL,
timeout REAL NOT NULL,
BEGIN {
eval {
Gruntmaster::Data::init 'dbi:Pg:dbname=gmtest'; 1;
- } or plan skip_all => 'Cannot connect to test database. Create it by running make_test_db.sh before running this test. '. "Error: $@";
+ } or plan skip_all => 'Cannot connect to test database. Create it by running createdb gmtest before running this test. '. "Error: $@";
plan tests => 37;
}
+note 'Setting up test database';
+$ENV{PGOPTIONS} = '-c client_min_messages=WARNING';
+system 'psql', 'gmtest', '-qf', 'db.sql';
+system 'psql', 'gmtest', '-qf', 'testdata.sql';
+
note 'Running update_status';
update_status;
-CREATE FUNCTION tm() RETURNS BIGINT IMMUTABLE LANGUAGE SQL AS $$
+CREATE OR REPLACE FUNCTION tm() RETURNS BIGINT IMMUTABLE LANGUAGE SQL AS $$
SELECT EXTRACT(epoch FROM NOW())::bigint
$$;
+TRUNCATE users, contests, problems, jobs RESTART IDENTITY CASCADE;
+
-- USERS
INSERT INTO users (id, admin) VALUES ('MGV', TRUE);