3
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
6
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
10
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
13
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
15
$$ LANGUAGE 'plpgsql';
17
2
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
4
CREATE TABLE login (
20
5
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
6
login VARCHAR UNIQUE NOT NULL,
23
8
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
9
'enabled', 'disabled'))
46
28
subjectid SERIAL PRIMARY KEY NOT NULL,
47
29
subj_code VARCHAR UNIQUE NOT NULL,
48
30
subj_name VARCHAR NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
31
subj_short_name VARCHAR UNIQUE NOT NULL,
52
35
CREATE TABLE semester (
53
36
semesterid SERIAL PRIMARY KEY NOT NULL,
54
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
55
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
37
year CHAR(4) NOT NULL,
38
semester CHAR(1) NOT NULL,
56
39
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
57
40
'current', 'future')) DEFAULT 'current',
58
41
UNIQUE (year, semester)
62
45
offeringid SERIAL PRIMARY KEY NOT NULL,
63
46
subject INT4 REFERENCES subject (subjectid) NOT NULL,
64
47
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
48
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
68
49
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
69
50
UNIQUE (subject, semesterid)
75
56
CREATE TABLE project_set (
76
57
projectsetid SERIAL PRIMARY KEY NOT NULL,
77
58
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
78
max_students_per_group INTEGER
59
max_students_per_group INTEGER NOT NULL DEFAULT 4
81
62
CREATE TABLE project (
82
63
projectid SERIAL PRIMARY KEY NOT NULL,
83
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
87
66
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
88
deadline TIMESTAMP NOT NULL
91
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
96
IF TG_OP = ''UPDATE'' THEN
97
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
101
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
102
PERFORM 1 FROM project, project_set
103
WHERE project_set.offeringid = oid AND
104
project.projectsetid = project_set.projectsetid AND
105
project.short_name = NEW.short_name;
107
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
111
' LANGUAGE 'plpgsql';
113
CREATE TRIGGER check_project_namespacing
114
BEFORE INSERT OR UPDATE ON project
115
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
117
70
CREATE TABLE project_group (
118
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
71
groupnm VARCHAR NOT NULL,
119
72
groupid SERIAL PRIMARY KEY NOT NULL,
120
73
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
184
137
CREATE TABLE project_extension (
185
extensionid SERIAL PRIMARY KEY,
186
138
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
187
139
deadline TIMESTAMP NOT NULL,
188
140
approver INT4 REFERENCES login (loginid) NOT NULL,
192
144
CREATE TABLE project_submission (
193
submissionid SERIAL PRIMARY KEY,
194
145
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
195
146
path VARCHAR NOT NULL,
196
revision INT4 NOT NULL,
197
date_submitted TIMESTAMP NOT NULL,
198
submitter INT4 REFERENCES login (loginid) NOT NULL
147
revision INT4 NOT NULL
201
150
CREATE TABLE project_mark (
223
172
CREATE TABLE worksheet (
224
173
worksheetid SERIAL PRIMARY KEY,
225
174
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
226
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
175
identifier TEXT NOT NULL,
227
176
name TEXT NOT NULL,
228
177
data TEXT NOT NULL,
229
178
assessable BOOLEAN NOT NULL,