1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
2
4
loginid SERIAL PRIMARY KEY NOT NULL,
3
5
login VARCHAR UNIQUE NOT NULL,
5
7
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
6
'enabled', 'disabled')),
8
'enabled', 'disabled'))
9
DEFAULT 'no_agreement',
7
10
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
9
12
'lecturer', 'admin')),
10
unixid INT UNIQUE NOT NULL, -- unix user id
13
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
11
14
nick VARCHAR NOT NULL,
12
15
pass_exp TIMESTAMP,
13
16
acct_exp TIMESTAMP,
22
28
CREATE TABLE subject (
23
29
subjectid SERIAL PRIMARY KEY NOT NULL,
24
30
subj_code VARCHAR UNIQUE NOT NULL,
25
31
subj_name VARCHAR NOT NULL,
26
subj_short_name VARCHAR, -- may be null
32
subj_short_name VARCHAR UNIQUE NOT NULL,
36
CREATE TABLE semester (
37
semesterid SERIAL PRIMARY KEY NOT NULL,
38
year CHAR(4) NOT NULL,
39
semester CHAR(1) NOT NULL,
41
UNIQUE (year, semester)
44
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
47
IF OLD.active = true AND NEW.active = false THEN
48
UPDATE enrolment SET active=false WHERE offeringid IN (
49
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
55
CREATE TRIGGER deactivate_semester_enrolments
56
AFTER UPDATE ON semester
57
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
30
59
CREATE TABLE offering (
31
60
offeringid SERIAL PRIMARY KEY NOT NULL,
32
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
33
year CHAR(4) NOT NULL,
34
semester CHAR(1) NOT NULL,
35
UNIQUE (subject, year, semester)
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
63
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
64
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
65
UNIQUE (subject, semesterid)
68
-- Projects and groups
69
-- -------------------
71
CREATE TABLE project_set (
72
projectsetid SERIAL PRIMARY KEY NOT NULL,
73
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
74
max_students_per_group INTEGER NOT NULL DEFAULT 4
38
77
CREATE TABLE project (
39
78
projectid SERIAL PRIMARY KEY NOT NULL,
42
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
81
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
46
85
CREATE TABLE project_group (
47
86
groupnm VARCHAR NOT NULL,
48
87
groupid SERIAL PRIMARY KEY NOT NULL,
49
offeringid INT4 REFERENCES offering (offeringid),
88
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
51
90
createdby INT4 REFERENCES login (loginid) NOT NULL,
52
91
epoch TIMESTAMP NOT NULL,
53
UNIQUE (offeringid, groupnm)
92
UNIQUE (projectsetid, groupnm)
95
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
100
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
101
PERFORM 1 FROM project_group, project_set WHERE project_set.offeringid = oid AND project_group.projectsetid = project_set.projectsetid AND project_group.groupnm = NEW.groupnm;
103
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
107
' LANGUAGE 'plpgsql';
109
CREATE TRIGGER check_group_namespacing
110
BEFORE INSERT OR UPDATE ON project_group
111
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
56
113
CREATE TABLE group_invitation (
57
114
loginid INT4 REFERENCES login (loginid) NOT NULL,
58
115
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
79
134
special_supp_result VARCHAR,
136
active BOOL NOT NULL DEFAULT true,
81
137
PRIMARY KEY (loginid,offeringid)
140
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
145
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
146
IF NOT active AND NEW.active = true THEN
147
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
151
' LANGUAGE 'plpgsql';
153
CREATE TRIGGER confirm_active_semester
154
BEFORE INSERT OR UPDATE ON enrolment
155
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
84
157
CREATE TABLE assessed (
85
158
assessedid SERIAL PRIMARY KEY NOT NULL,
86
159
loginid INT4 REFERENCES login (loginid),
87
160
groupid INT4 REFERENCES project_group (groupid),
161
projectid INT4 REFERENCES project (projectid) NOT NULL,
88
162
-- exactly one of loginid and groupid must be non-null
89
163
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
90
164
OR (loginid IS NULL AND groupid IS NOT NULL))