3
-- Check that the provided name is sane for use in URLs.
4
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
7
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
11
-- Just like valid_url_name, except that @ is permitted (so we can use a
12
-- reasonable subset of email addresses as usernames).
13
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
16
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
18
$$ LANGUAGE 'plpgsql';
2
20
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
4
22
CREATE TABLE login (
5
23
loginid SERIAL PRIMARY KEY NOT NULL,
6
login VARCHAR UNIQUE NOT NULL,
24
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
8
26
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
9
27
'enabled', 'disabled'))
28
46
subjectid SERIAL PRIMARY KEY NOT NULL,
29
47
subj_code VARCHAR UNIQUE NOT NULL,
30
48
subj_name VARCHAR NOT NULL,
31
subj_short_name VARCHAR UNIQUE NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
35
52
CREATE TABLE semester (
36
53
semesterid SERIAL PRIMARY KEY NOT NULL,
37
year CHAR(4) NOT NULL,
38
semester CHAR(1) NOT NULL,
54
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
55
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
39
56
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
40
57
'current', 'future')) DEFAULT 'current',
41
58
UNIQUE (year, semester)
45
62
offeringid SERIAL PRIMARY KEY NOT NULL,
46
63
subject INT4 REFERENCES subject (subjectid) NOT NULL,
47
64
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
68
worksheet_cutoff TIMESTAMP,
48
69
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
49
70
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
50
71
UNIQUE (subject, semesterid)
56
77
CREATE TABLE project_set (
57
78
projectsetid SERIAL PRIMARY KEY NOT NULL,
58
79
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
59
max_students_per_group INTEGER NOT NULL DEFAULT 4
80
max_students_per_group INTEGER
62
83
CREATE TABLE project (
63
84
projectid SERIAL PRIMARY KEY NOT NULL,
85
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
66
89
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
90
deadline TIMESTAMP NOT NULL
93
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
98
IF TG_OP = ''UPDATE'' THEN
99
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
103
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
104
PERFORM 1 FROM project, project_set
105
WHERE project_set.offeringid = oid AND
106
project.projectsetid = project_set.projectsetid AND
107
project.short_name = NEW.short_name;
109
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
113
' LANGUAGE 'plpgsql';
115
CREATE TRIGGER check_project_namespacing
116
BEFORE INSERT OR UPDATE ON project
117
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
70
119
CREATE TABLE project_group (
71
groupnm VARCHAR NOT NULL,
120
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
72
121
groupid SERIAL PRIMARY KEY NOT NULL,
73
122
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
134
183
OR (loginid IS NULL AND groupid IS NOT NULL))
186
-- We must use conditional constraints here -- NULL != NULL.
187
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
188
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
137
190
CREATE TABLE project_extension (
191
extensionid SERIAL PRIMARY KEY,
138
192
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
139
193
deadline TIMESTAMP NOT NULL,
140
194
approver INT4 REFERENCES login (loginid) NOT NULL,
144
198
CREATE TABLE project_submission (
199
submissionid SERIAL PRIMARY KEY,
145
200
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
146
201
path VARCHAR NOT NULL,
147
revision INT4 NOT NULL
202
revision INT4 NOT NULL,
203
date_submitted TIMESTAMP NOT NULL,
204
submitter INT4 REFERENCES login (loginid) NOT NULL
150
207
CREATE TABLE project_mark (
162
219
CREATE TABLE exercise (
163
identifier TEXT PRIMARY KEY,
220
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
165
222
description TEXT,
223
description_xhtml_cache TEXT,
172
230
CREATE TABLE worksheet (
173
231
worksheetid SERIAL PRIMARY KEY,
174
232
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
175
identifier TEXT NOT NULL,
233
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
176
234
name TEXT NOT NULL,
177
235
data TEXT NOT NULL,
236
data_xhtml_cache TEXT,
178
237
assessable BOOLEAN NOT NULL,
238
published BOOLEAN NOT NULL DEFAULT true,
179
239
seq_no INT4 NOT NULL,
180
240
format TEXT NOT NUll,
181
241
UNIQUE (offeringid, identifier)