-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from SELab-2/db_update
Update selab_script.sql
- Loading branch information
Showing
1 changed file
with
101 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,142 @@ | ||
DROP TABLE IF EXISTS Indiening CASCADE; | ||
DROP TABLE IF EXISTS Status CASCADE; | ||
DROP TABLE IF EXISTS Project CASCADE; | ||
DROP TABLE IF EXISTS StudentGroep CASCADE; | ||
DROP TABLE IF EXISTS Groep CASCADE; | ||
DROP TABLE IF EXISTS StudentVak CASCADE; | ||
DROP TABLE IF EXISTS LesgeverVak CASCADE; | ||
DROP TABLE IF EXISTS Vak CASCADE; | ||
DROP TABLE IF EXISTS WebsiteUser CASCADE; | ||
DROP TABLE IF EXISTS Bestand CASCADE; | ||
DROP TABLE IF EXISTS Status CASCADE; | ||
|
||
|
||
CREATE TABLE WebsiteUser( | ||
azureObjectId text PRIMARY KEY, | ||
is_admin boolean | ||
DROP TRIGGER IF EXISTS trg_check_submission_before_deadline ON submission; | ||
DROP FUNCTION IF EXISTS check_submission_before_deadline(); | ||
DROP FUNCTION IF EXISTS check_team_size_before_joining(); | ||
|
||
DROP TABLE IF EXISTS submission CASCADE; | ||
DROP TABLE IF EXISTS status CASCADE; | ||
DROP TABLE IF EXISTS file CASCADE; | ||
DROP TABLE IF EXISTS student_group CASCADE; | ||
DROP TABLE IF EXISTS team CASCADE; | ||
DROP TABLE IF EXISTS project CASCADE; | ||
DROP TABLE IF EXISTS teacher_subject CASCADE; | ||
DROP TABLE IF EXISTS student_subject CASCADE; | ||
DROP TABLE IF EXISTS subject CASCADE; | ||
DROP TABLE IF EXISTS website_user CASCADE; | ||
|
||
CREATE TABLE website_user ( | ||
uid TEXT PRIMARY KEY, | ||
is_admin BOOLEAN NOT NULL DEFAULT FALSE, | ||
given_name TEXT NOT NULL, | ||
mail TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE Vak ( | ||
vak_id TEXT PRIMARY KEY, | ||
Naam TEXT NOT NULL | ||
CREATE TABLE subject ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE StudentVak ( | ||
azureObjectId TEXT NOT NULL, | ||
vak_id TEXT NOT NULL, | ||
PRIMARY KEY (azureObjectId, vak_id), | ||
FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, | ||
FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE CASCADE | ||
CREATE TABLE student_subject ( | ||
uid TEXT NOT NULL, | ||
subject_id BIGSERIAL NOT NULL, | ||
PRIMARY KEY (uid, subject_id), | ||
FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, | ||
FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE LesgeverVak ( | ||
azureObjectId TEXT NOT NULL, | ||
vak_id TEXT NOT NULL, | ||
PRIMARY KEY (azureObjectId, vak_id), | ||
FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, | ||
FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE CASCADE | ||
CREATE TABLE teacher_subject ( | ||
uid TEXT NOT NULL, | ||
subject_id BIGSERIAL NOT NULL, | ||
PRIMARY KEY (uid, subject_id), | ||
FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, | ||
FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE CASCADE | ||
); | ||
|
||
|
||
CREATE TABLE Project ( | ||
CREATE TABLE project ( | ||
id BIGSERIAL PRIMARY KEY, | ||
deadline DATE, | ||
naam TEXT NOT NULL, | ||
vak_id TEXT NOT NULL, | ||
FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE SET NULL | ||
deadline DATE NOT NULL, | ||
name TEXT NOT NULL, | ||
subject_id BIGSERIAL NOT NULL, | ||
description TEXT, | ||
max_team_size INT NOT NULL DEFAULT 4, -- Added column for maximum team size | ||
FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE SET NULL, | ||
CONSTRAINT deadline_check CHECK (deadline >= CURRENT_DATE) | ||
); | ||
|
||
CREATE TABLE Groep ( | ||
groep_id BIGSERIAL PRIMARY KEY, | ||
groep TEXT NOT NULL, | ||
score BIGINT, | ||
project_id BIGINT, | ||
FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE CASCADE | ||
CREATE TABLE team ( | ||
id BIGSERIAL PRIMARY KEY, | ||
team_name TEXT NOT NULL, | ||
score BIGINT NOT NULL, | ||
project_id BIGINT NOT NULL, | ||
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE, | ||
CONSTRAINT score_check CHECK (score BETWEEN 0 AND 20) | ||
); | ||
|
||
|
||
CREATE TABLE StudentGroep ( | ||
azureObjectId TEXT NOT NULL, | ||
groep_id BIGINT NOT NULL, | ||
PRIMARY KEY (azureObjectId, groep_id), | ||
FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, | ||
FOREIGN KEY (groep_id) REFERENCES Groep(groep_id) ON DELETE CASCADE | ||
CREATE TABLE student_group ( | ||
uid TEXT NOT NULL, | ||
team_id BIGINT NOT NULL, | ||
PRIMARY KEY (uid, team_id), | ||
FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, | ||
FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE Status ( | ||
status_id BIGSERIAL PRIMARY KEY, | ||
CREATE TABLE status ( | ||
id BIGSERIAL PRIMARY KEY, | ||
status_name TEXT NOT NULL UNIQUE | ||
); | ||
|
||
INSERT INTO Status (status_name) VALUES ('inProgress'); | ||
INSERT INTO Status (status_name) VALUES ('Accepted'); | ||
INSERT INTO Status (status_name) VALUES ('Denied'); | ||
INSERT INTO status (status_name) VALUES ('InProgress'); | ||
INSERT INTO status (status_name) VALUES ('Accepted'); | ||
INSERT INTO status (status_name) VALUES ('Denied'); | ||
|
||
|
||
CREATE TABLE Indiening ( | ||
indiening_id BIGSERIAL PRIMARY KEY, | ||
datum TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
groep_id BIGINT NOT NULL, | ||
CREATE TABLE submission ( | ||
id BIGSERIAL PRIMARY KEY, | ||
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
team_id BIGINT NOT NULL, | ||
project_id BIGINT NOT NULL, | ||
status_id BIGINT NOT NULL DEFAULT 1, -- Default to 'inProgress' | ||
FOREIGN KEY (groep_id) REFERENCES Groep(groep_id) ON DELETE CASCADE, | ||
FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE CASCADE, | ||
FOREIGN KEY (status_id) REFERENCES Status(status_id) ON DELETE RESTRICT | ||
status_id BIGINT NOT NULL DEFAULT 1, -- Default to 'InProgress' | ||
FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE, | ||
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE, | ||
FOREIGN KEY (status_id) REFERENCES status(id) ON DELETE RESTRICT | ||
); | ||
|
||
CREATE TABLE Bestand ( | ||
bestand_id BIGSERIAL PRIMARY KEY, | ||
indiening_id BIGINT, -- mag null zijn -> kunnen linken aan zowel indiening of project of standalone | ||
project_id BIGINT, -- same | ||
-- TODO nog effectief bestand columns toevoegen ie file type etc.. not sure about exact implementatie rn | ||
FOREIGN KEY (indiening_id) REFERENCES Indiening(indiening_id) ON DELETE SET NULL, | ||
FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE SET NULL | ||
CREATE TABLE file ( | ||
id BIGSERIAL PRIMARY KEY, | ||
submission_id BIGINT, -- Optional, can be linked to a submission, project, or standalone | ||
project_id BIGINT, -- Optional, same as above | ||
FOREIGN KEY (submission_id) REFERENCES submission(id) ON DELETE SET NULL, | ||
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE SET NULL | ||
); | ||
|
||
|
||
--checks | ||
|
||
ALTER TABLE Project | ||
ADD CONSTRAINT deadline_check CHECK (deadline >= CURRENT_DATE); | ||
|
||
ALTER TABLE Groep ADD CONSTRAINT score_check CHECK (score BETWEEN 0 AND 20); | ||
|
||
ALTER TABLE WebsiteUser ALTER COLUMN is_admin SET DEFAULT FALSE; | ||
|
||
--zorgt ervoor dat er geen indieningen na deadline kunnen gebeuren | ||
CREATE OR REPLACE FUNCTION check_indiening_voor_deadline() | ||
-- Function to check submission deadline | ||
CREATE OR REPLACE FUNCTION check_submission_before_deadline() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
project_deadline DATE; | ||
BEGIN | ||
SELECT deadline INTO project_deadline | ||
FROM Project | ||
WHERE id = NEW.project_id; | ||
SELECT deadline INTO project_deadline FROM project WHERE id = NEW.project_id; | ||
|
||
IF CURRENT_TIMESTAMP > project_deadline THEN | ||
RAISE EXCEPTION 'Indiening is niet meer toegelaten na deadline'; | ||
RAISE EXCEPTION 'Submissions are not allowed after the deadline.'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER trg_check_indiening_voor_deadline | ||
BEFORE INSERT ON Indiening | ||
CREATE TRIGGER trg_check_submission_before_deadline | ||
BEFORE INSERT ON submission | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_indiening_voor_deadline(); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
EXECUTE FUNCTION check_submission_before_deadline(); | ||
|
||
-- Function to check team size before adding a new member | ||
CREATE OR REPLACE FUNCTION check_team_size_before_joining() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
current_team_size INT; | ||
max_size INT; | ||
BEGIN | ||
SELECT COUNT(*) INTO current_team_size FROM student_group WHERE team_id = NEW.team_id; | ||
SELECT max_team_size INTO max_size FROM project JOIN team ON team.project_id = project.id WHERE team.id = NEW.team_id; | ||
|
||
IF current_team_size >= max_size THEN | ||
RAISE EXCEPTION 'The team is already at its maximum capacity of % members.', max_size; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER trg_check_team_size_before_joining | ||
BEFORE INSERT ON student_group | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_team_size_before_joining(); |