-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.sql
88 lines (77 loc) · 2.81 KB
/
setup.sql
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
-------------------------
-- connect-pg-simple --
-------------------------
CREATE TABLE "session" (
"sid" varchar NOT NULL COLLATE "default",
"sess" json NOT NULL,
"expire" timestamp(6) NOT NULL
)
WITH (OIDS=FALSE);
ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX "IDX_session_expire" ON "session" ("expire");
--------------
-- willow --
--------------
CREATE TABLE "users" (
"email" TEXT PRIMARY KEY,
"password" TEXT NOT NULL,
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"rcs_id" TEXT,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
"last_login_at" TIMESTAMP WITH TIME ZONE,
"reset_token" UUID UNIQUE,
"reset_token_created_at" TIMESTAMP WITH TIME ZONE
);
CREATE OR REPLACE FUNCTION "on_update_reset_token"()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW."reset_token_created_at" = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
DROP TRIGGER IF EXISTS "on_update_reset_token" ON "users";
CREATE TRIGGER "on_update_reset_token"
BEFORE INSERT OR UPDATE
ON "users"
FOR EACH ROW
WHEN (NEW."reset_token" IS NOT NULL)
EXECUTE PROCEDURE "on_update_reset_token"();
CREATE TABLE "courses" (
"name" TEXT PRIMARY KEY,
"display_name" TEXT,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "instructors" (
"course_name" TEXT REFERENCES "courses" ("name") ON DELETE CASCADE ON UPDATE CASCADE,
"instructor_email" TEXT /* REFERENCES "users" ("email") ON DELETE CASCADE ON UPDATE CASCADE */,
PRIMARY KEY ("course_name", "instructor_email")
);
CREATE TABLE "students" (
"course_name" TEXT REFERENCES "courses" ("name") ON DELETE CASCADE ON UPDATE CASCADE,
"student_email" TEXT /* REFERENCES "users" ("email") ON DELETE CASCADE ON UPDATE CASCADE */,
PRIMARY KEY ("course_name", "student_email")
);
CREATE TABLE "assignments" (
"name" TEXT,
"course_name" TEXT REFERENCES "courses" ("name") ON DELETE CASCADE ON UPDATE CASCADE,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
"due_date" TIMESTAMP WITH TIME ZONE,
"tree" JSONB NOT NULL,
PRIMARY KEY ("name", "course_name")
);
CREATE TABLE "submissions" (
"student_email" TEXT REFERENCES "users" ("email"),
"assignment_name" TEXT,
"course_name" TEXT,
"submitted_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
"tree" JSONB,
"correct" BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY ("assignment_name", "course_name")
REFERENCES "assignments" ("name", "course_name") ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY ("course_name", "student_email")
REFERENCES "students" ("course_name", "student_email") ON DELETE CASCADE ON UPDATE CASCADE
);