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