-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
150 lines (138 loc) · 6.95 KB
/
schema.sql
File metadata and controls
150 lines (138 loc) · 6.95 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- blog backend basic schema
-- Charset and engine chosen for MySQL 8+ (utf8mb4 for emoji support)
CREATE DATABASE IF NOT EXISTS `blog` DEFAULT CHARACTER SET = 'utf8mb4' DEFAULT COLLATE 'utf8mb4_general_ci';
USE `blog`;
-- Users
CREATE TABLE IF NOT EXISTS `users` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`avatar` VARCHAR(512) DEFAULT NULL,
`bio` VARCHAR(512) DEFAULT NULL,
`roles` VARCHAR(255) DEFAULT 'user',
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=active,0=disabled',
`last_login` DATETIME DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_users_username` (`username`),
UNIQUE KEY `uk_users_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Posts
CREATE TABLE IF NOT EXISTS `posts` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`author_id` BIGINT NOT NULL,
`title` VARCHAR(255) NOT NULL,
`summary` VARCHAR(512) DEFAULT NULL,
`content` LONGTEXT NOT NULL,
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=published,0=draft,2=deleted',
`view_count` INT NOT NULL DEFAULT 0,
`like_count` INT NOT NULL DEFAULT 0,
`comment_count` INT NOT NULL DEFAULT 0,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_posts_author` (`author_id`),
KEY `idx_posts_created_at` (`created_at`),
CONSTRAINT `fk_posts_author` FOREIGN KEY (`author_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Comments
CREATE TABLE IF NOT EXISTS `comments` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`post_id` BIGINT NOT NULL,
`user_id` BIGINT NOT NULL,
`parent_id` BIGINT DEFAULT NULL,
`content` TEXT NOT NULL,
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=visible,0=hidden',
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_comments_post` (`post_id`),
KEY `idx_comments_user` (`user_id`),
CONSTRAINT `fk_comments_post` FOREIGN KEY (`post_id`) REFERENCES `posts`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_comments_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Likes (supports liking posts or comments)
CREATE TABLE IF NOT EXISTS `likes` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`post_id` BIGINT DEFAULT NULL,
`comment_id` BIGINT DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_likes_user_post_comment` (`user_id`,`post_id`,`comment_id`),
KEY `idx_likes_user` (`user_id`),
CONSTRAINT `fk_likes_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_likes_post` FOREIGN KEY (`post_id`) REFERENCES `posts`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_likes_comment` FOREIGN KEY (`comment_id`) REFERENCES `comments`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Friends / follows
CREATE TABLE IF NOT EXISTS `friends` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`friend_id` BIGINT NOT NULL,
`status` TINYINT NOT NULL DEFAULT 0 COMMENT '0=pending,1=accepted,2=blocked',
`remark` VARCHAR(255) DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_friends_pair` (`user_id`,`friend_id`),
KEY `idx_friends_user` (`user_id`),
CONSTRAINT `fk_friends_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_friends_friend` FOREIGN KEY (`friend_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Messages (private messages / system notifications)
CREATE TABLE IF NOT EXISTS `messages` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`from_user_id` BIGINT DEFAULT NULL,
`to_user_id` BIGINT NOT NULL,
`type` VARCHAR(50) DEFAULT 'chat' COMMENT 'chat/system/notice',
`content` TEXT NOT NULL,
`is_read` TINYINT NOT NULL DEFAULT 0,
`delivered` TINYINT NOT NULL DEFAULT 0,
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=normal,0=deleted',
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_messages_to` (`to_user_id`),
KEY `idx_messages_from` (`from_user_id`),
CONSTRAINT `fk_messages_from_user` FOREIGN KEY (`from_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL,
CONSTRAINT `fk_messages_to_user` FOREIGN KEY (`to_user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Notifications
CREATE TABLE IF NOT EXISTS `blog_notification` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL COMMENT '接收通知的用户ID',
`from_user_id` BIGINT DEFAULT NULL COMMENT '发送通知的用户ID',
`type` VARCHAR(50) NOT NULL COMMENT 'LIKE/COMMENT/REPLY/FRIEND_REQUEST/FRIEND_ACCEPT',
`target_id` BIGINT DEFAULT NULL COMMENT '目标ID(如动态ID、评论ID等)',
`content` VARCHAR(500) DEFAULT NULL COMMENT '通知内容摘要',
`is_read` TINYINT NOT NULL DEFAULT 0 COMMENT '0-未读,1-已读',
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '1-正常,0-删除',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_notification_user` (`user_id`),
KEY `idx_notification_from_user` (`from_user_id`),
KEY `idx_notification_type` (`type`),
CONSTRAINT `fk_notification_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_notification_from_user` FOREIGN KEY (`from_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Token blacklist (optional persistence for logout tokens)
CREATE TABLE IF NOT EXISTS `token_blacklist` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`token` VARCHAR(1024) NOT NULL,
`expired_at` DATETIME DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_token_blacklist_token` (`token`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Optional: admin / migration table
CREATE TABLE IF NOT EXISTS `schema_version` (
`name` VARCHAR(128) NOT NULL,
`version` VARCHAR(64) NOT NULL,
`applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- Sample data (optional) -- uncomment to seed
-- INSERT INTO `users` (`username`,`password`,`email`) VALUES ('admin','<hashed_password>','admin@example.com');
-- End of schema