-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
76 lines (68 loc) · 2.56 KB
/
init.sql
File metadata and controls
76 lines (68 loc) · 2.56 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
CREATE TABLE users(
id SERIAL NOT NULL PRIMARY KEY,
username varchar(50) NOT NULL UNIQUE,
email varchar(255) NOT NULL UNIQUE,
password varchar(50) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
follower_count INT NOT NULL DEFAULT 0
);
ALTER TABLE users ADD COLUMN search_vector tsvector;
UPDATE users SET search_vector = to_tsvector('english', username || ' ' || first_name || ' ' ||last_name);
CREATE INDEX users_search_vector_idx ON users USING gin(search_vector);
CREATE TABLE followers(
user_id INT NOT NULL,
follower_id INT NOT NULL,
PRIMARY KEY (user_id, follower_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (follower_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE likes(
liker_id INT NOT NULL,
post_id INT NOT NULL,
PRIMARY KEY (post_id, liker_id),
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
FOREIGN KEY (liker_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE retweetLikes(
liker_id INT NOT NULL,
retweet_id INT NOT NULL,
PRIMARY KEY (retweet_id, liker_id),
FOREIGN KEY (retweet_id) REFERENCES retweets(id) ON DELETE CASCADE,
FOREIGN KEY (liker_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE posts(
id SERIAL NOT NULL PRIMARY KEY,
author_id INT NOT NULL,
title varchar(50) NOT NULL,
body varchar(255),
likes INT NOT NULL DEFAULT 0,
comment_count INT NOT NULL DEFAULT 0,
post_date varchar(255) NOT NULL,
FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE,
);
CREATE TABLE retweets(
id SERIAL NOT NULL PRIMARY KEY,
author_id INT NOT NULL,
post_id INT NOT NULL,
title varchar(50) NOT NULL,
body varchar(255),
retweeter_id INT NOT NULL,
likes INT NOT NULL DEFAULT 0,
comment_count INT NOT NULL DEFAULT 0,
post_date varchar(255) NOT NULL,
FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (retweeter_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
);
ALTER TABLE posts ADD COLUMN search_vector tsvector;
UPDATE posts SET search_vector = to_tsvector('english', title || ' ' || body);
CREATE INDEX posts_search_vector_idx ON posts USING gin(search_vector);
CREATE TABLE comments(
id SERIAL NOT NULL PRIMARY KEY,
author_id INT NOT NULL,
body varchar(255) NOT NULL,
likes INT NOT NULL DEFAULT 0,
comment_date varchar(255) NOT NULL,
FOREIGN KEY (author_id) REFERENCES users(id)
);