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 TEXT NOT NULL CHECK (valid_url_name(year)),
55
url_name TEXT NOT NULL CHECK (valid_url_name(url_name)),
57
display_name TEXT NOT NULL,
39
58
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
40
59
'current', 'future')) DEFAULT 'current',
41
UNIQUE (year, semester)
60
UNIQUE (year, url_name),
44
64
CREATE TABLE offering (
45
65
offeringid SERIAL PRIMARY KEY NOT NULL,
46
66
subject INT4 REFERENCES subject (subjectid) NOT NULL,
47
67
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
70
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
71
worksheet_cutoff TIMESTAMP,
48
72
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
49
73
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
50
74
UNIQUE (subject, semesterid)
62
86
CREATE TABLE project (
63
87
projectid SERIAL PRIMARY KEY NOT NULL,
64
short_name TEXT NOT NULL,
88
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
65
89
name TEXT NOT NULL,
68
92
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
93
deadline TIMESTAMP NOT NULL
72
96
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
96
120
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
98
122
CREATE TABLE project_group (
99
groupnm VARCHAR NOT NULL,
123
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
100
124
groupid SERIAL PRIMARY KEY NOT NULL,
101
125
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
162
186
OR (loginid IS NULL AND groupid IS NOT NULL))
189
-- We must use conditional constraints here -- NULL != NULL.
190
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
191
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
165
193
CREATE TABLE project_extension (
166
194
extensionid SERIAL PRIMARY KEY,
167
195
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
168
deadline TIMESTAMP NOT NULL,
169
197
approver INT4 REFERENCES login (loginid) NOT NULL,
194
222
CREATE TABLE exercise (
195
identifier TEXT PRIMARY KEY,
223
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
197
225
description TEXT,
226
description_xhtml_cache TEXT,
204
233
CREATE TABLE worksheet (
205
234
worksheetid SERIAL PRIMARY KEY,
206
235
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
207
identifier TEXT NOT NULL,
236
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
208
237
name TEXT NOT NULL,
209
238
data TEXT NOT NULL,
239
data_xhtml_cache TEXT,
210
240
assessable BOOLEAN NOT NULL,
241
published BOOLEAN NOT NULL DEFAULT true,
211
242
seq_no INT4 NOT NULL,
212
243
format TEXT NOT NUll,
213
244
UNIQUE (offeringid, identifier)