Repository navigation
Expand file tree
/
Copy pathsetup.sql
More file actions
194 lines (170 loc) · 7.5 KB
/
Copy pathsetup.sql
File metadata and controls
194 lines (170 loc) · 7.5 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
-- USE YOUR DATABASE --
USE `story-ai-db`;
-- story-ai-db for workflow testing
-- DROP UNNECESSARY TABLES --
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS shared_chats;
DROP TABLE IF EXISTS chats;
DROP TABLE IF EXISTS story_tags;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS read_history;
DROP TABLE IF EXISTS bookmarks;
DROP TABLE IF EXISTS likes;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS stories;
DROP TABLE IF EXISTS followers;
DROP TABLE IF EXISTS users;
-- CREATE TABLES ---
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(256) NOT NULL,
age INT,
register_time DATETIME DEFAULT CURRENT_TIMESTAMP,
active BOOLEAN DEFAULT TRUE,
last_login DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
is_creator BOOLEAN DEFAULT FALSE,
is_admin BOOLEAN DEFAULT FALSE,
image_name VARCHAR(256)
);
CREATE TABLE followers (
follower_id INT,
following_id INT,
PRIMARY KEY (follower_id, following_id),
FOREIGN KEY (follower_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (following_id) REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE TABLE stories (
story_id INT AUTO_INCREMENT PRIMARY KEY,
creator_id INT NOT NULL,
title VARCHAR(200) NOT NULL,
prompt TEXT,
description TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (creator_id) REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE TABLE posts (
post_id INT AUTO_INCREMENT PRIMARY KEY,
story_id INT NOT NULL,
image_name VARCHAR(256),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
like_count INT DEFAULT 0,
comment_count INT DEFAULT 0,
FOREIGN KEY (story_id) REFERENCES stories(story_id) ON DELETE CASCADE
);
CREATE TABLE comments (
comment_id INT AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
post_id INT NOT NULL,
comment TEXT NOT NULL,
like_count INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE
);
CREATE TABLE likes (
like_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
post_id INT,
comment_id INT,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE,
FOREIGN KEY (comment_id) REFERENCES comments(comment_id) ON DELETE CASCADE,
CHECK (
(post_id IS NOT NULL AND comment_id IS NULL) OR
(post_id IS NULL AND comment_id IS NOT NULL)
)
);
CREATE TABLE bookmarks (
user_id INT,
story_id INT,
PRIMARY KEY (user_id, story_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (story_id) REFERENCES stories(story_id) ON DELETE CASCADE
);
CREATE TABLE read_history (
user_id INT,
story_id INT,
last_read_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, story_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (story_id) REFERENCES stories(story_id) ON DELETE CASCADE
);
CREATE TABLE tags (
tag_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE story_tags (
story_id INT,
tag_id INT,
PRIMARY KEY (story_id, tag_id),
FOREIGN KEY (story_id) REFERENCES stories(story_id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(tag_id) ON DELETE CASCADE
);
CREATE TABLE chats (
chat_id INT AUTO_INCREMENT PRIMARY KEY,
story_id INT NOT NULL,
user_id INT NOT NULL,
FOREIGN KEY (story_id) REFERENCES stories(story_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE TABLE messages (
message_id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT NOT NULL,
chat_id INT NOT NULL,
is_user BOOLEAN DEFAULT TRUE,
FOREIGN KEY (chat_id) REFERENCES chats(chat_id) ON DELETE CASCADE
);
CREATE TABLE shared_chats (
share_id INT PRIMARY KEY AUTO_INCREMENT,
chat_id INT NOT NULL,
user_id INT NOT NULL,
shared_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(chat_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
UNIQUE (chat_id)
);
-- INSERT INTO TABLES --
-- Insert users
INSERT INTO users (username, password_hash, age, is_creator, active, image_name)
VALUES
('lsana', 'hash_tikaluka2', 21, TRUE, TRUE, 'image1.jpg'),
('chichia', 'hash_kitketa', 20, TRUE, TRUE, 'image2.jpg'),
('ffff','123',23,false,TRUE,'image2.jpg');
-- Insert stories for creators
INSERT INTO stories (creator_id, title, prompt, description)
VALUES
(1, 'The Mystery of the Lost Artifact', 'A suspenseful journey to uncover hidden secrets.',
'A really cool description for a really cool story'),
(2, 'Adventures in the Digital Age', 'Exploring the vast world of cyberspace.',
'A really uncool description for a really uncool story');
-- Insert posts for stories
INSERT INTO posts (story_id, image_name, like_count, comment_count)
VALUES
(1, 'image1.jpg', 2, 1),
(2, 'image2.jpg', 1, 1);
-- Insert comments from users
INSERT INTO comments (author_id, post_id, comment, like_count)
VALUES
(2, 1, 'Amazing post!', 0),
(1, 2, 'Great insights on the topic!', 1);
INSERT INTO likes (user_id, post_id, comment_id)
VALUES
(2, 1, NULL),
(1, NULL, 2);
-- Insert tags and associate them with stories
INSERT INTO tags (name)
VALUES
('Adventure'),
('Mystery'),
('Technology'),
('Sci-Fi'),
('Detective'),
('Drama');
INSERT INTO story_tags (story_id, tag_id)
VALUES
(1, 1),
(1, 2),
(2, 1),
(2, 3);