mentorenwahl/backend/db/migrations/20220414171336_create_users...

93 lines
2.4 KiB
SQL

/*
Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
Copyright (C) 2022 Dominic Grimm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-- +micrate Up
-- SQL in section ' Up ' is executed when this migration is applied
CREATE TYPE user_roles AS ENUM ('student', 'teacher');
CREATE TABLE users(
id serial PRIMARY KEY,
username text UNIQUE NOT NULL,
password_hash text NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
role user_roles NOT NULL,
admin boolean NOT NULL
);
CREATE TABLE tokens(
id uuid PRIMARY KEY,
iat timestamp NOT NULL,
exp timestamp NOT NULL,
active boolean NOT NULL,
user_id int NOT NULL REFERENCES users(id)
);
CREATE TABLE teachers(
id serial PRIMARY KEY,
user_id int NOT NULL UNIQUE REFERENCES users(id),
max_students int NOT NULL
);
CREATE TABLE classes(id serial PRIMARY KEY, name text UNIQUE NOT NULL);
CREATE TABLE students(
id serial PRIMARY KEY,
user_id int NOT NULL UNIQUE REFERENCES users(id),
class_id int NOT NULL REFERENCES classes(id)
);
CREATE TABLE votes(
id serial PRIMARY KEY,
student_id int NOT NULL UNIQUE REFERENCES students(id)
);
CREATE TABLE teacher_votes(
id serial PRIMARY KEY,
vote_id int NOT NULL REFERENCES votes(id),
teacher_id int NOT NULL REFERENCES teachers(id),
priority int NOT NULL,
UNIQUE (vote_id, teacher_id, priority)
);
CREATE TABLE assignments(
id serial PRIMARY KEY,
student_id int NOT NULL REFERENCES students(id) UNIQUE,
teacher_id int NOT NULL REFERENCES teachers(id)
);
-- +micrate Down
-- SQL section ' Down ' is executed when this migration is rolled back
DROP TABLE assignments;
DROP TABLE teacher_votes;
DROP TABLE votes;
DROP TABLE admins;
DROP TABLE teachers;
DROP TABLE students;
DROP TABLE classes;
DROP TABLE tokens;
DROP TABLE users;
DROP TYPE user_roles;