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

93 lines
2.4 KiB
MySQL
Raw Normal View History

2022-04-14 16:22:07 +00:00
/*
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
2022-10-31 08:47:26 +00:00
CREATE TYPE user_roles AS ENUM ('student', 'teacher');
2022-04-14 16:22:07 +00:00
CREATE TABLE users(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
username text UNIQUE NOT NULL,
2023-01-29 10:49:13 +00:00
password_hash text NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
2022-04-14 16:22:07 +00:00
role user_roles NOT NULL,
2022-11-13 17:41:53 +00:00
admin boolean NOT NULL
);
CREATE TABLE tokens(
id uuid PRIMARY KEY,
iat timestamp NOT NULL,
exp timestamp NOT NULL,
2022-11-21 18:48:53 +00:00
active boolean NOT NULL,
2022-11-13 17:41:53 +00:00
user_id int NOT NULL REFERENCES users(id)
2022-04-14 16:22:07 +00:00
);
CREATE TABLE teachers(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
user_id int NOT NULL UNIQUE REFERENCES users(id),
max_students int NOT NULL
2022-04-14 16:22:07 +00:00
);
2023-01-29 10:49:13 +00:00
CREATE TABLE classes(id serial PRIMARY KEY, name text UNIQUE NOT NULL);
2022-04-14 16:22:07 +00:00
CREATE TABLE students(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
2023-01-29 10:49:13 +00:00
user_id int NOT NULL UNIQUE REFERENCES users(id),
class_id int NOT NULL REFERENCES classes(id)
2022-04-14 16:22:07 +00:00
);
CREATE TABLE votes(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
student_id int NOT NULL UNIQUE REFERENCES students(id)
2022-04-14 16:22:07 +00:00
);
CREATE TABLE teacher_votes(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
vote_id int NOT NULL REFERENCES votes(id),
teacher_id int NOT NULL REFERENCES teachers(id),
2023-01-17 05:56:19 +00:00
priority int NOT NULL,
UNIQUE (vote_id, teacher_id, priority)
2022-04-14 16:22:07 +00:00
);
2022-07-28 12:05:10 +00:00
CREATE TABLE assignments(
2022-11-13 17:41:53 +00:00
id serial PRIMARY KEY,
2023-01-17 05:56:19 +00:00
student_id int NOT NULL REFERENCES students(id) UNIQUE,
2022-11-13 17:41:53 +00:00
teacher_id int NOT NULL REFERENCES teachers(id)
2022-07-28 12:05:10 +00:00
);
2022-04-14 16:22:07 +00:00
-- +micrate Down
-- SQL section ' Down ' is executed when this migration is rolled back
2022-11-13 17:41:53 +00:00
DROP TABLE assignments;
2022-04-14 16:22:07 +00:00
DROP TABLE teacher_votes;
DROP TABLE votes;
DROP TABLE admins;
DROP TABLE teachers;
DROP TABLE students;
2023-01-29 10:49:13 +00:00
DROP TABLE classes;
2022-11-13 17:41:53 +00:00
DROP TABLE tokens;
2022-04-14 16:22:07 +00:00
DROP TABLE users;
DROP TYPE user_roles;