-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathinit.sql
78 lines (65 loc) · 2.7 KB
/
init.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
BEGIN;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS users (
"id" uuid DEFAULT uuid_generate_v4 () PRIMARY KEY,
"name" varchar NOT NULL,
"screen_name" varchar NOT NULL,
"password_hash" varchar NOT NULL,
"email" varchar NOT NULL,
"bio" varchar NOT NULL,
"location" varchar NOT NULL,
"website" varchar NOT NULL,
"birth_date" date NOT NULL,
"profile_image_url" text NOT NULL,
"profile_banner_url" text NOT NULL,
"followers_count" int NOT NULL,
"followings_count" int NOT NULL,
"created_at" timestamp(0) without time zone NOT NULL,
"updated_at" timestamp(0) without time zone NOT NULL
);
CREATE TABLE IF NOT EXISTS followers (
"followee_id" uuid NOT NULL REFERENCES users ("id") ON DELETE CASCADE,
"follower_id" uuid NOT NULL REFERENCES users ("id") ON DELETE CASCADE,
"created_at" timestamp(0) without time zone NOT NULL,
PRIMARY KEY ("followee_id", "follower_id")
);
CREATE TABLE IF NOT EXISTS tweets (
"id" uuid DEFAULT uuid_generate_v4 () PRIMARY KEY,
"user_id" uuid NOT NULL REFERENCES users ("id") ON DELETE CASCADE,
"content" varchar(280) CHECK (char_length("content") <= 280),
"favorites_count" int,
"replies_count" int,
"created_at" timestamp(0) without time zone NOT NULL
);
CREATE INDEX IF NOT EXISTS tweets_created_at_idx ON tweets ("created_at");
CREATE TABLE IF NOT EXISTS tweet_entities (
"tweet_id" uuid REFERENCES tweets ON DELETE CASCADE,
"media_links" text[] CHECK (array_length("media_links", 1) <= 4),
"created_at" timestamp(0) without time zone NOT NULL
);
CREATE TABLE IF NOT EXISTS replies (
"tweet_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
"reply_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
PRIMARY KEY ("tweet_id", "reply_id")
);
CREATE TABLE IF NOT EXISTS retweets (
"tweet_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
"retweet_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
PRIMARY KEY ("tweet_id", "retweet_id")
);
CREATE TABLE IF NOT EXISTS favorites (
"user_id" uuid NOT NULL REFERENCES users ("id") ON DELETE CASCADE,
"tweet_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
PRIMARY KEY ("tweet_id", "user_id")
);
CREATE TABLE IF NOT EXISTS feeds (
"id" uuid DEFAULT uuid_generate_v4 () PRIMARY KEY,
"user_id" uuid NOT NULL REFERENCES users ("id") ON DELETE CASCADE,
"created_at" timestamp(0) without time zone NOT NULL
);
CREATE TABLE IF NOT EXISTS feed_tweets (
"tweet_id" uuid NOT NULL REFERENCES tweets ("id") ON DELETE CASCADE,
"feed_id" uuid NOT NULL REFERENCES feeds ("id") ON DELETE CASCADE,
PRIMARY KEY ("tweet_id", "feed_id")
);
COMMIT;