-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26-medium-database.sql
97 lines (74 loc) · 2.65 KB
/
26-medium-database.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
89
90
91
92
93
94
95
96
97
-- Drop existing resources
DROP TABLE countries CASCADE;
DROP TABLE users CASCADE;
DROP SEQUENCE user_sequence;
-- Create resource
CREATE TABLE "users" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"username" varchar UNIQUE NOT NULL,
"password" varchar NOT NULL,
"name" varchar NOT NULL,
"role" varchar NOT NULL,
"gender" varchar(10) NOT NULL,
"avatar" varchar,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp ON UPDATE
);
CREATE TABLE "posts" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"title" varchar(200) DEFAULT '',
"body" text DEFAULT '',
"og_image" varchar,
"slug" varchar UNIQUE NOT NULL,
"published" boolean,
"created_by" integer,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp
);
CREATE TABLE "claps" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"post_id" integer,
"user_id" integer,
"counter" integer DEFAULT 0,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp
);
CREATE TABLE "comments" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"post_id" integer,
"user_id" integer,
"content" text,
"parent_id" integer,
"visible" boolean,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp
);
CREATE TABLE "user_lists" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_id" integer,
"title" varchar(100),
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp
);
CREATE TABLE "user_list_entry" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_list_id" integer,
"post_id" integer,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp
);
CREATE UNIQUE INDEX ON "claps" ("post_id", "user_id");
CREATE INDEX ON "claps" ("post_id");
CREATE INDEX ON "comments" ("post_id");
CREATE INDEX ON "comments" ("visible");
CREATE UNIQUE INDEX ON "user_lists" ("user_id", "title");
CREATE INDEX ON "user_lists" ("user_id");
ALTER TABLE "posts" ADD FOREIGN KEY ("created_by") REFERENCES "users" ("id");
ALTER TABLE "claps" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "claps" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("parent_id") REFERENCES "comments" ("id");
ALTER TABLE "user_lists" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "user_list_entry" ADD FOREIGN KEY ("user_list_id") REFERENCES "user_lists" ("id");
ALTER TABLE "user_list_entry" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");