-
Notifications
You must be signed in to change notification settings - Fork 9
/
schema.sql
80 lines (73 loc) · 2.61 KB
/
schema.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
-- While we could use a traditional migration tool, our use case for RealWorld is
-- simple enough that we won't reap the benefits of needing to evolve and maintain
-- We'll need a table for users, with username and email as an indexable key
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
-- With MySQL, we'll store UUIDs as bytes, using the UUID Go type to map them into structs
id BINARY(16) PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
image VARCHAR(4096) NOT NULL DEFAULT '',
bio VARCHAR(4096) NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY idx_users_username_email (username, email)
);
DROP TABLE IF EXISTS user_follows;
CREATE TABLE user_follows
(
id BINARY(16) PRIMARY KEY,
follower_id BINARY(16) NOT NULL,
followee_id BINARY(16) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
DROP TABLE IF EXISTS user_favorites;
CREATE TABLE user_favorites
(
id BINARY(16) PRIMARY KEY,
user_id BINARY(16) NOT NULL,
article_id BINARY(16) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
DROP TABLE IF EXISTS articles;
CREATE TABLE articles
(
id BINARY(16) PRIMARY KEY,
author_id BINARY(16) NOT NULL,
slug VARCHAR(255) NOT NULL,
title VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
body VARCHAR(4096) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY idx_articles_slug (slug)
);
DROP TABLE IF EXISTS comments;
CREATE TABLE comments
(
id BINARY(16) PRIMARY KEY,
author_id BINARY(16) NOT NULL,
article_id BINARY(16) NOT NULL,
body VARCHAR(4096) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
DROP TABLE IF EXISTS tags;
CREATE TABLE tags
(
id BINARY(16) PRIMARY KEY,
description VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY idx_tag_slug (description)
);
DROP TABLE IF EXISTS article_tags;
CREATE TABLE article_tags
(
id BINARY(16) PRIMARY KEY,
article_id BINARY(16) NOT NULL,
tag_id BINARY(16) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY idx_article_tags_article_tag (article_id, tag_id)
);