-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseStructure.ts
More file actions
433 lines (378 loc) · 15.6 KB
/
Copy pathDatabaseStructure.ts
File metadata and controls
433 lines (378 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import {PermissionEnum} from "../../../../models/enums/PermissionEnum";
import {isPostgresError} from "../helpers/DatabaseErrorHelper";
import {end, pool, permissionBits, getClient, pgDB, toBin} from "../HelperDB";
import {usersView, CourseUsersView, CoursesView, submissionsView, filesView, snippetsView, commentsView, commentThreadView, MentionsView, TagsView, CourseUsersViewAll} from "../ViewsDB";
import {databaseSamples} from "./DatabaseSamples";
/**
* Contains Script to generate database structure
*/
//////////////////////////////////////////////////////////////////////////////////////////////
// ___ __ __ ____ ___ ____ _____ _ _ _ _____ //
// |_ _| \/ | _ \ / _ \| _ |_ _|/ \ | \ | |_ __ //
// | || |\/| | |_) | | | | |_) || | / _ \ | \| | | |(_) //
// | || | | | __/| |_| | _ < | |/ ___ \| |\ | | | _ //
// |___|_| |_|_| \___/|_| \_\|_/_/ \_|_| \_| |_|(_) //
// //
// If you make any changes to the database schema, please upgrade the version number //
// and create a migration in the DatabaseMigrations file to update running Atelier //
// applications. Please make sure that this schema results in the same database structure //
// as applying the migration on the previous version does. //
// This does not apply to views, as those are recreated on every start of Atelier anyway, //
// whether they are changed or not. If a view changes because of a schema change, you don't //
// have to include those views in the migration. //
// //
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/ const VERSION = 6; /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
////////////////////////////////////|--------------------|////////////////////////////////////
if (require.main === module) {
//args without node & path name
const args = process.argv.splice(2);
//check if the 'insert' option is specified
const insertToo = args.find(el => el === "-i");
//create the database
(async() => {
const client = await getClient();
try {
await client.query("BEGIN");
await makeDB(client);
//if inserting too, then do insert as well
if (insertToo) {
await databaseSamples(client);
}
await client.query("COMMIT");
} catch (e) {
await client.query("ROLLBACK");
throw e;
} finally {
client.release();
end();
}
})();
} else {
// makeDB(()=>{console.log("made the database")}, console.error)
}
async function makeDB(client: pgDB = pool) {
const query = dropViewQueries + dropTableQueries + createTableQueries + createViewQueries + createIndexQueries;
// const singles = query.split('CREATE').map(s=>'CREATE'+s).slice(1)
// console.log(singles)
// for (const item of singles){
// await client.query(item).then(()=>
// console.log('database creation successful')
// ).catch((e : Error) =>{
// if (isPostgresError(e) && e.position!==undefined){
// console.log(item.substring(Number(e.position)-60, Number(e.position)+60))
// }
// throw e
// });
// };
// return;
return client.query(query
).then(() =>
console.log("database creation successful")
).catch((e: Error) => {
if (isPostgresError(e) && e.position !== undefined) {
console.log(query.substring(Number(e.position) - 60, Number(e.position) + 60));
}
throw e;
});
}
export const dropViewQueries = `
DROP VIEW IF EXISTS
"SubmissionsRefs", "CommentThreadRefs",
"UsersView", "CoursesView",
"SubmissionsView", "FilesView",
"SnippetsView", "CommentsView",
"CommentThreadView", "CourseRegistrationView",
"MentionsView", "TagsView", "CourseUsersView", "CourseUsersViewAll",
"CourseRegistrationViewAll";
`;
const dropTableQueries = `
DROP TABLE IF EXISTS
"Metadata",
"GlobalRolePermissions",
"CourseRolePermissions", "Users",
"Courses", "CourseRegistration",
"Submissions", "Files", "Snippets",
"CommentThread", "Comments",
"Mentions", "Tags", "CourseInvites",
"Plugins", "PluginHooks";
`;
const createIndexQueries = `
-- We need to get submissions by course for the feed, which is sorted by date
CREATE INDEX submission_courseid_sorted_idx ON "Submissions" (courseid, date);
-- Threads are often filtered on visibility, then sometimes on automatedness (for the feeds)
-- Joining on the submission is then done in a hash join, so that doesn't require an index
CREATE INDEX commentthread_visibility_idx ON "CommentThread" (visibilitystate, automated);
-- Threads are also queried by submissionid, then per visibility
CREATE INDEX commentthread_submissionid_visibility_idx ON "CommentThread" (submissionid, visibilitystate);
-- Comments are almost always grouped by the thread they're in, then sorted by date (and id, for some reason)
CREATE INDEX comments_threadid_sorted_idx ON "Comments" (commentthreadid, created, commentid);
-- Files are often queried by submissionid
CREATE INDEX files_submissionid_idx ON "Files" (submissionid);
-- Mentions are queried by userid or usergroup
CREATE INDEX mentions_userid_idx ON "Mentions" (userid);
CREATE INDEX mentions_usergroup_idx ON "Mentions" (usergroup);
-- Course registrations may occasionaly be filtered by role, after the courseid
CREATE INDEX courseregistration_courseid_role_userid_idx ON "CourseRegistration" (courseid, courserole, userid);
-- Users are queried by samlid, username and email
CREATE INDEX users_samlid_idx ON "Users" (samlid);
CREATE INDEX users_username_idx ON "Users" (username);
CREATE INDEX users_email_idx ON "Users" (email);
-- Invites are in the settings queried by course and creator
CREATE INDEX courseinvites_courseid_creatorid_idx ON "CourseInvites" (courseid, creatorid);
-- Course registrations are also queried for a user
CREATE INDEX courseregistration_userid_idx ON "CourseRegistration" (userid);
-- Submissions can also be queried by user in a course
CREATE INDEX submission_courseid_user_sorted_idx ON "Submissions" (courseid, userid, date);
`;
const createTableQueries = `
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TABLE "Metadata" (
key text PRIMARY KEY,
value text NOT NULL
);
INSERT INTO "Metadata" VALUES
('version', '${VERSION}');
CREATE TABLE "CourseRolePermissions" (
courseRoleID text PRIMARY KEY,
permission bit(${permissionBits}) NOT NULL
);
INSERT INTO "CourseRolePermissions" VALUES
('moduleCoordinator', 64898867406::bit(${permissionBits})),
('student', 25769803776::bit(${permissionBits})),
('TA', 64898465858::bit(${permissionBits})),
('teacher', 64898859078::bit(${permissionBits})),
('unregistered', 0::bit(${permissionBits})),
('plugin', 0::bit(${permissionBits}));
CREATE TABLE "GlobalRolePermissions" (
globalRoleID text PRIMARY KEY,
permission bit(${permissionBits}) NOT NULL
);
INSERT INTO "GlobalRolePermissions" VALUES
('admin', 64907262175::bit(${permissionBits})),
('staff', 14528::bit(${permissionBits})),
('user', 0::bit(${permissionBits})),
('unregistered', 0::bit(${permissionBits})),
('plugin', 0::bit(${permissionBits}));
CREATE TABLE "Users" (
userID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
samlID text UNIQUE, --can be null
userName text NOT NULL CHECK (userName <> ''),
email text NOT NULL UNIQUE CHECK (email <> ''),
globalRole text NOT NULL REFERENCES "GlobalRolePermissions"(globalRoleID) DEFAULT 'unregistered',
permission bit(${permissionBits}) NOT NULL,
hash char(60) NOT NULL,
canvasrefresh text UNIQUE,
researchAllowed boolean -- can be null, if the user has not responded yet
);
CREATE TABLE "Plugins" (
pluginID uuid PRIMARY KEY REFERENCES "Users"(userID) ON DELETE CASCADE,
webhookURL text NOT NULL,
webhookSecret text NOT NULL,
publicKey text NOT NULL
);
CREATE TABLE "PluginHooks" (
pluginID uuid NOT NULL REFERENCES "Plugins"(pluginID) ON DELETE CASCADE,
hook text NOT NULL,
UNIQUE (pluginID, hook)
);
CREATE TABLE "Courses" (
courseID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
courseName text NOT NULL CHECK (courseName <> ''),
state text NOT NULL DEFAULT 'open',
creator uuid REFERENCES "Users" (userID),
canvasCourseID text
);
CREATE TABLE "CourseRegistration" (
courseID uuid NOT NULL REFERENCES "Courses"(courseID) ON DELETE CASCADE,
userID uuid NOT NULL REFERENCES "Users"(userID) ON DELETE CASCADE,
courseRole text NOT NULL REFERENCES "CourseRolePermissions"(courseRoleID) DEFAULT 'unregistered',
permission bit(${permissionBits}) NOT NULL,
PRIMARY KEY (courseID, userID)
);
CREATE TABLE "CourseInvites" (
inviteID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
creatorID uuid NOT NULL REFERENCES "Users"(userID) ON DELETE CASCADE,
courseID uuid NOT NULL REFERENCES "Courses"(courseID) ON DELETE CASCADE,
type text NOT NULL,
joinRole text NOT NULL REFERENCES "CourseRolePermissions"(courseRoleID)
);
CREATE TABLE "Submissions" (
submissionID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
courseID uuid NOT NULL REFERENCES "Courses"(courseID) ON DELETE CASCADE,
userID uuid NOT NULL REFERENCES "Users"(userID),
title text NOT NULL CHECK (title <> ''),
date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
state text NOT NULL DEFAULT 'new'
);
CREATE TABLE "Files" (
fileID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
submissionID uuid NOT NULL REFERENCES "Submissions"(submissionID) ON DELETE CASCADE,
pathname text NOT NULL,
type text NOT NULL DEFAULT 'unsupported'
);
CREATE TABLE "Snippets" (
snippetID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
lineStart integer NOT NULL,
lineEnd integer NOT NULL,
charStart integer NOT NULL,
charEnd integer NOT NULL,
body text NOT NULL,
contextBefore text NOT NULL,
contextAfter text NOT NULL
);
CREATE TABLE "CommentThread" (
commentThreadID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
submissionID uuid NOT NULL REFERENCES "Submissions"(submissionID) ON DELETE CASCADE,
fileID uuid NOT NULL REFERENCES "Files"(fileID),
snippetID uuid NOT NULL UNIQUE REFERENCES "Snippets"(snippetID),
visibilityState text NOT NULL DEFAULT 'public',
automated boolean NOT NULL DEFAULT FALSE, -- indicator that this comment was created by an automated system
sharedBy uuid REFERENCES "Users"(userID) ON DELETE SET NULL
-- if the user that shared this comment, i.e. made it public to students, is deleted
-- the comment should still exist, so we set the value back to null (which is also the
-- value for comments that are not shared)
);
CREATE TABLE "Comments" (
commentID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
commentThreadID uuid NOT NULL REFERENCES "CommentThread"(commentThreadID) ON DELETE CASCADE,
userID uuid NOT NULL REFERENCES "Users"(userID),
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
edited timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
body text NOT NULL
);
CREATE TABLE "Mentions" (
mentionID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
commentID uuid NOT NULL REFERENCES "Comments"(commentID) ON DELETE CASCADE,
userID uuid REFERENCES "Users"(userID) ON DELETE CASCADE,
userGroup text REFERENCES "CourseRolePermissions"(courseRoleID) ON DELETE CASCADE,
CONSTRAINT either_or CHECK ((userID IS NULL) != (userGroup IS NULL))
);
CREATE TABLE "Tags" (
tagID uuid PRIMARY KEY DEFAULT gen_random_uuid(),
commentID uuid NOT NULL REFERENCES "Comments"(commentID) ON DELETE CASCADE,
tagbody text NOT NULL
);
CREATE OR REPLACE FUNCTION delSnippet()
returns trigger AS
$$
begin
DELETE FROM "Snippets"
WHERE snippetID = old.snippetID;
return null;
end;
$$
LANGUAGE plpgsql;
CREATE TRIGGER DEL_SNIPPET
AFTER DELETE ON "CommentThread"
FOR EACH ROW
EXECUTE PROCEDURE delSnippet();
CREATE OR REPLACE FUNCTION defaultFile()
returns TRIGGER AS
$$
BEGIN
INSERT INTO "Files" VALUES (
DEFAULT,
(SELECT new.submissionID),
'',
'undefined/undefined'
);
return null;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER DEFAULT_FILE
AFTER INSERT ON "Submissions"
FOR EACH ROW
EXECUTE PROCEDURE defaultFile();
`;
export const createViewQueries = `
CREATE VIEW "SubmissionsRefs" AS (
SELECT courseID, submissionID
FROM "Submissions"
);
CREATE VIEW "CommentThreadRefs" AS (
SELECT sr.*, ct.commentThreadID, ct.snippetID, ct.fileID, ct.automated, ct.visibilityState
FROM "CommentThread" as ct, "SubmissionsRefs" as sr
WHERE sr.submissionID = ct.submissionID
);
CREATE VIEW "UsersView" AS (
${usersView()}
);
CREATE VIEW "CourseUsersView" AS (
${CourseUsersView()}
);
CREATE VIEW "CoursesView" AS (
${CoursesView()}
);
CREATE VIEW "CourseUsersViewAll" AS (
${CourseUsersViewAll()}
);
CREATE VIEW "SubmissionsView" AS (
${submissionsView()}
);
CREATE VIEW "FilesView" as (
${filesView()}
);
CREATE VIEW "SnippetsView" as (
${snippetsView()}
);
CREATE VIEW "CommentsView" AS (
${commentsView()}
);
CREATE VIEW "CommentThreadView" as (
${commentThreadView()}
);
CREATE VIEW "MentionsView" as (
${MentionsView()}
);
CREATE VIEW "TagsView" as (
${TagsView()}
);
CREATE OR REPLACE FUNCTION _viewableSubmissions (userID uuid, courseID uuid)
RETURNS TABLE (submissionID uuid) AS $$
DECLARE
course_user "CourseUsersViewAll"%ROWTYPE;
BEGIN
SELECT * INTO course_user -- get extra info on the current user
FROM "CourseUsersViewAll" as c
WHERE c.userID = $1
AND c.courseID = $2;
-- if a user can see all, return all
IF ((b'${toBin(2 ** PermissionEnum.viewAllSubmissions)}' & course_user.permission) > 0::bit(${permissionBits})) THEN
RETURN QUERY (
SELECT s.submissionID
FROM "SubmissionsView" as s
WHERE s.courseID=$2
);
ELSE
RETURN QUERY ( -- your submission
SELECT s.submissionID
FROM "SubmissionsView" as s
WHERE s.courseID=$2
AND s.userID = $1
);
RETURN QUERY ( -- you commented on it
SELECT c.submissionID
FROM "CommentsView" as c
WHERE c.courseID=$2
AND c.userID=$1
);
RETURN QUERY ( -- you are mentioned in a comment
SELECT m.submissionID
FROM "MentionsView" as m
WHERE m.courseID = $2
AND ( m.userID = $1
OR m.courseRole = course_user.courseRole
)
);
END IF;
END; $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION viewableSubmissions(userID uuid, courseID uuid)
RETURNS TABLE (submissionID uuid) AS $$
BEGIN
RETURN QUERY (
SELECT DISTINCT *
FROM _viewableSubmissions($1, $2)
);
END; $$ LANGUAGE plpgsql;
`;