Repository navigation
Expand file tree
/
Copy pathshema2.sql
More file actions
290 lines (289 loc) · 10.8 KB
/
Copy pathshema2.sql
File metadata and controls
290 lines (289 loc) · 10.8 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
-- WARNING: This schema is for context only and is not meant to be run.
-- Table order and constraints may not be valid for execution.
CREATE TABLE public.accounts (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text,
type text NOT NULL,
provider text NOT NULL,
provider_account_id text NOT NULL,
refresh_token text,
access_token text,
expires_at bigint,
token_type text,
scope text,
id_token text,
session_state text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT accounts_pkey PRIMARY KEY (id),
CONSTRAINT accounts_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.addresses (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
full_name text NOT NULL,
street text NOT NULL,
city text NOT NULL,
state text,
postal_code text,
country text NOT NULL,
phone text,
is_default boolean DEFAULT false,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT addresses_pkey PRIMARY KEY (id),
CONSTRAINT addresses_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.audit_logs (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text,
action text NOT NULL,
entity text,
entity_id uuid,
metadata jsonb,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT audit_logs_pkey PRIMARY KEY (id),
CONSTRAINT audit_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.authors (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
bio text,
payout_account jsonb,
earnings numeric DEFAULT 0,
CONSTRAINT authors_pkey PRIMARY KEY (id),
CONSTRAINT authors_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.book_files (
id uuid NOT NULL DEFAULT gen_random_uuid(),
book_id uuid,
format text NOT NULL CHECK (format = ANY (ARRAY['epub'::text, 'pdf'::text, 'mobi'::text, 'txt'::text, 'doc'::text, 'docx'::text])),
file_name text NOT NULL,
file_path text,
download_link text,
file_size bigint,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT book_files_pkey PRIMARY KEY (id),
CONSTRAINT book_files_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id)
);
CREATE TABLE public.book_ratings (
id uuid NOT NULL DEFAULT gen_random_uuid(),
book_id uuid NOT NULL,
user_id text NOT NULL,
rating integer NOT NULL CHECK (rating >= 1 AND rating <= 5),
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT book_ratings_pkey PRIMARY KEY (id),
CONSTRAINT book_ratings_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id),
CONSTRAINT book_ratings_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id)
);
CREATE TABLE public.book_reviews (
id uuid NOT NULL DEFAULT gen_random_uuid(),
book_id uuid NOT NULL,
user_id text NOT NULL,
rating_id uuid,
title text,
content text NOT NULL CHECK (length(content) >= 10),
is_spoiler boolean DEFAULT false,
is_published boolean DEFAULT true,
helpful_count integer DEFAULT 0,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT book_reviews_pkey PRIMARY KEY (id),
CONSTRAINT book_reviews_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id),
CONSTRAINT book_reviews_rating_id_fkey FOREIGN KEY (rating_id) REFERENCES public.book_ratings(id),
CONSTRAINT book_reviews_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.books (
id uuid NOT NULL DEFAULT gen_random_uuid(),
google_books_id text UNIQUE,
title text NOT NULL,
authors ARRAY DEFAULT '{}'::text[],
description text,
thumbnail text,
publisher text,
published_date text,
page_count integer,
categories ARRAY DEFAULT '{}'::text[],
language text DEFAULT 'en'::text,
isbn_10 text,
isbn_13 text,
google_books_data jsonb,
price numeric DEFAULT 0,
currency text DEFAULT 'USD'::text,
source text DEFAULT 'telegram'::text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
published boolean DEFAULT true,
featured boolean DEFAULT false,
average_rating numeric DEFAULT 0,
total_ratings integer DEFAULT 0,
total_reviews integer DEFAULT 0,
available_formats ARRAY DEFAULT ARRAY['pdf'::text],
original_filename text,
image text,
CONSTRAINT books_pkey PRIMARY KEY (id)
);
CREATE TABLE public.cart_items (
id uuid NOT NULL DEFAULT gen_random_uuid(),
cart_id uuid NOT NULL,
book_id uuid NOT NULL,
quantity integer DEFAULT 1 CHECK (quantity > 0),
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT cart_items_pkey PRIMARY KEY (id),
CONSTRAINT cart_items_cart_id_fkey FOREIGN KEY (cart_id) REFERENCES public.carts(id),
CONSTRAINT cart_items_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id)
);
CREATE TABLE public.carts (
id uuid NOT NULL DEFAULT gen_random_uuid(),
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
user_id text UNIQUE,
CONSTRAINT carts_pkey PRIMARY KEY (id),
CONSTRAINT carts_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.contact_messages (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text,
name text NOT NULL,
email text NOT NULL,
subject text,
message text NOT NULL,
status text DEFAULT 'new'::text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT contact_messages_pkey PRIMARY KEY (id),
CONSTRAINT contact_messages_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.exchanges (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
book_title text,
book_metadata jsonb,
credit_amount numeric,
status text DEFAULT 'submitted'::text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT exchanges_pkey PRIMARY KEY (id),
CONSTRAINT exchanges_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.order_items (
id uuid NOT NULL DEFAULT gen_random_uuid(),
order_id uuid,
book_id uuid,
qty integer DEFAULT 1 CHECK (qty > 0),
unit_price numeric,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT order_items_pkey PRIMARY KEY (id),
CONSTRAINT order_items_order_id_fkey FOREIGN KEY (order_id) REFERENCES public.orders(id),
CONSTRAINT order_items_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id)
);
CREATE TABLE public.orders (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
total numeric,
currency text DEFAULT 'KES'::text,
payment_method text,
payment_provider text,
provider_session_id text,
status text DEFAULT 'pending'::text,
metadata jsonb,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT orders_pkey PRIMARY KEY (id),
CONSTRAINT orders_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.review_votes (
id uuid NOT NULL DEFAULT gen_random_uuid(),
review_id uuid NOT NULL,
user_id text NOT NULL,
is_helpful boolean NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT review_votes_pkey PRIMARY KEY (id),
CONSTRAINT review_votes_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id),
CONSTRAINT review_votes_review_id_fkey FOREIGN KEY (review_id) REFERENCES public.book_reviews(id)
);
CREATE TABLE public.sessions (
id uuid NOT NULL DEFAULT gen_random_uuid(),
session_token text NOT NULL UNIQUE,
user_id text,
expires timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT sessions_pkey PRIMARY KEY (id),
CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.subscriptions (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
user_id text,
stripe_customer_id text,
stripe_subscription_id text,
plan text,
status text,
current_period_end timestamp with time zone,
created_at timestamp with time zone DEFAULT now(),
books_accessed_count integer DEFAULT 0,
books_limit integer NOT NULL,
can_download boolean DEFAULT false,
premium_expires_at timestamp with time zone,
CONSTRAINT subscriptions_pkey PRIMARY KEY (id),
CONSTRAINT subscriptions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.user_book_access (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
book_id uuid NOT NULL,
access_type text NOT NULL CHECK (access_type = ANY (ARRAY['purchase'::text, 'subscription'::text])),
granted_at timestamp with time zone DEFAULT now(),
expires_at timestamp with time zone,
download_count integer DEFAULT 0,
last_accessed timestamp with time zone,
CONSTRAINT user_book_access_pkey PRIMARY KEY (id),
CONSTRAINT user_book_access_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id),
CONSTRAINT user_book_access_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id)
);
CREATE TABLE public.user_sessions (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text,
session_start timestamp with time zone DEFAULT now(),
session_end timestamp with time zone,
ip_address inet,
user_agent text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT user_sessions_pkey PRIMARY KEY (id),
CONSTRAINT user_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.user_settings (
user_id text NOT NULL,
theme text DEFAULT 'light'::text,
language text DEFAULT 'en'::text,
notifications_enabled boolean DEFAULT true,
email_notifications boolean DEFAULT true,
marketing_emails boolean DEFAULT false,
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT user_settings_pkey PRIMARY KEY (user_id),
CONSTRAINT user_settings_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);
CREATE TABLE public.users (
email text,
full_name text,
avatar_url text,
username text,
subscription_tier text DEFAULT 'free'::text,
onboarding_completed boolean DEFAULT false,
preferences jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
id text NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (id)
);
CREATE TABLE public.verification_tokens (
identifier text NOT NULL,
token text NOT NULL UNIQUE,
expires timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT verification_tokens_pkey PRIMARY KEY (identifier, token)
);
CREATE TABLE public.wishlists (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id text NOT NULL,
book_id uuid,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT wishlists_pkey PRIMARY KEY (id),
CONSTRAINT wishlists_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id)
);