7
'Comment this line out to acknowledge the warnings.'
10
DELETE FROM group_member;
11
DELETE FROM group_invitation;
12
DELETE FROM project_group;
13
DELETE FROM enrolment;
16
CREATE TABLE semester (
17
semesterid SERIAL PRIMARY KEY NOT NULL,
18
year CHAR(4) NOT NULL,
19
semester CHAR(1) NOT NULL,
21
UNIQUE (year, semester)
24
CREATE TABLE project_set (
25
projectsetid SERIAL PRIMARY KEY NOT NULL,
26
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
27
max_students_per_group INTEGER NOT NULL DEFAULT 4
30
ALTER TABLE offering DROP COLUMN year;
31
ALTER TABLE offering DROP COLUMN semester;
32
ALTER TABLE offering DROP COLUMN max_groups_per_student;
33
ALTER TABLE offering DROP COLUMN max_students_per_group;
34
ALTER TABLE offering ADD COLUMN semesterid
35
INTEGER REFERENCES semester (semesterid) NOT NULL;
36
ALTER TABLE offering ADD CONSTRAINT offering_subject_key
37
UNIQUE (subject, semesterid);
39
ALTER TABLE project DROP COLUMN offeringid;
40
ALTER TABLE project ADD COLUMN projectsetid
41
INTEGER REFERENCES project_set (projectsetid) NOT NULL;
43
ALTER TABLE project_group DROP COLUMN offeringid;
44
ALTER TABLE project_group ADD COLUMN projectsetid
45
INTEGER REFERENCES project_set (projectsetid) NOT NULL;
46
ALTER TABLE project_group ADD CONSTRAINT project_group_projectsetid_key
47
UNIQUE (projectsetid, groupnm);
50
ALTER TABLE group_member DROP COLUMN projectid;
52
ALTER TABLE enrolment ADD COLUMN active BOOL NOT NULL DEFAULT true;
55
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
58
IF OLD.active = true AND NEW.active = false THEN
59
UPDATE enrolment SET active=false WHERE offeringid IN (
60
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
66
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
71
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
72
PERFORM 1 FROM project_group, project_set WHERE project_group.projectsetid = project_set.projectsetid AND project_group.groupnm = NEW.groupnm;
74
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
80
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
85
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
86
IF NOT active AND NEW.active = true THEN
87
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
93
CREATE TRIGGER deactivate_semester_enrolments
94
AFTER UPDATE ON semester
95
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
97
CREATE TRIGGER check_group_namespacing
98
BEFORE INSERT OR UPDATE ON project_group
99
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
101
CREATE TRIGGER confirm_active_semester
102
BEFORE INSERT OR UPDATE ON enrolment
103
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();