-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_database.sql
More file actions
112 lines (99 loc) · 3.43 KB
/
create_database.sql
File metadata and controls
112 lines (99 loc) · 3.43 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
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
publisher TEXT,
publish_year INTEGER,
isbn TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
page_count INTEGER,
cover_image_url TEXT,
description TEXT,
local_thumbnail TEXT,
subtitle TEXT,
genre TEXT,
added_by INTEGER,
FOREIGN KEY (added_by) REFERENCES users(id)
);
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
is_active INTEGER DEFAULT 1,
is_admin INTEGER DEFAULT 0
);
CREATE TABLE read_data (
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
rating INTEGER,
comment TEXT,
PRIMARY KEY (user_id, book_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE
);
CREATE TABLE reading_sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
date_started DATE,
date_completed DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE
);
CREATE INDEX idx_reading_sessions_user_book
ON reading_sessions(user_id, book_id);
CREATE TABLE IF NOT EXISTS "collections" (
collection_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
status TEXT CHECK(status IN ('read', 'want to read', 'currently reading', 'did not finish')) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE INDEX IF NOT EXISTS idx_collections_created_at ON collections(created_at);
CREATE TABLE book_tags (
tag_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
tag_name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE,
-- Ensure unique combinations of user, book, and tag
UNIQUE(user_id, book_id, tag_name)
);
CREATE INDEX idx_book_tags_user_book
ON book_tags(user_id, book_id);
CREATE TABLE user_collections (
collection_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
UNIQUE (user_id, name)
);
CREATE TABLE collection_books (
collection_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (collection_id, book_id),
FOREIGN KEY (collection_id) REFERENCES user_collections(collection_id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE TABLE wishlist (
wishlist_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
notes TEXT,
purchase_link TEXT,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE,
UNIQUE(user_id, book_id)
);
CREATE INDEX idx_wishlist_user
ON wishlist(user_id);