-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sql
More file actions
585 lines (425 loc) · 16.7 KB
/
setup.sql
File metadata and controls
585 lines (425 loc) · 16.7 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
--
-- Name: get_comment_count(uuid[]); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.get_comment_count(post_ids uuid[]) RETURNS TABLE(post_id uuid, "countComments" bigint)
LANGUAGE plpgsql
SET search_path TO 'public'
AS $$
DECLARE
item_id uuid;
begin
for item_id in select unnest(post_ids)
LOOP
RETURN QUERY
SELECT c.post_id, COUNT(*) as "countComments"
FROM comments c
WHERE c.post_id = item_id
GROUP BY c.post_id;
end LOOP;
end;
$$;
ALTER FUNCTION public.get_comment_count(post_ids uuid[]) OWNER TO postgres;
--
-- Name: get_stats_by_topic(uuid); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.get_stats_by_topic(topic_id_param uuid) RETURNS TABLE(post_count bigint, comment_count bigint, created_at timestamp with time zone)
LANGUAGE plpgsql
SET search_path TO 'public'
AS $$
DECLARE
post_ids uuid[];
BEGIN
SELECT ARRAY_AGG(p.id) INTO post_ids
FROM posts p
WHERE p.topic_id = topic_id_param;
post_count := (SELECT COUNT(*) FROM unnest(post_ids));
comment_count := (
SELECT COUNT(*)
FROM comments c
WHERE c.post_id = ANY(post_ids)
);
SELECT t.created_at INTO created_at
FROM topics t
WHERE t.id = topic_id_param;
RETURN NEXT;
END;
$$;
ALTER FUNCTION public.get_stats_by_topic(topic_id_param uuid) OWNER TO postgres;
--
-- Name: get_stats_by_user(uuid); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.get_stats_by_user(user_id_param uuid) RETURNS TABLE(post_count bigint, comment_count bigint, created_at timestamp with time zone)
LANGUAGE plpgsql
SET search_path TO 'public'
AS $$
BEGIN
SELECT COUNT(*) INTO post_count
FROM posts p
WHERE p.user_id = user_id_param;
SELECT COUNT(*) INTO comment_count
FROM comments c
WHERE c.user_id = user_id_param;
SELECT u.created_at INTO created_at
FROM users u
WHERE u.id = user_id_param;
RETURN NEXT;
END;
$$;
ALTER FUNCTION public.get_stats_by_user(user_id_param uuid) OWNER TO postgres;
--
-- Name: handle_new_user(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.handle_new_user() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
insert into public.users (id, name, email, photo_url, is_blocked, created_at)
values(
new.id,
coalesce(new.raw_user_meta_data->>'full_name', new.email),
new.email,
new.raw_user_meta_data->>'avatar_url',
false,
new.created_at
);
return new;
end;
$$;
ALTER FUNCTION public.handle_new_user() OWNER TO postgres;
--
-- Name: comment_with_vote; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW public.comment_with_vote AS
SELECT
NULL::bigint AS id,
NULL::uuid AS user_id,
NULL::uuid AS post_id,
NULL::bigint AS parent_comment_id,
NULL::jsonb AS users,
NULL::text AS content,
NULL::bigint AS "voteCount",
NULL::integer AS "userVote",
NULL::timestamp with time zone AS created_at;
ALTER VIEW public.comment_with_vote OWNER TO postgres;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: posts; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.posts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
title text NOT NULL,
content text,
image_url character varying,
user_id uuid NOT NULL,
topic_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
is_nsfw boolean DEFAULT false NOT NULL
);
ALTER TABLE public.posts OWNER TO postgres;
--
-- Name: topics; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.topics (
id uuid DEFAULT gen_random_uuid() NOT NULL,
name character varying NOT NULL,
description text,
user_id uuid NOT NULL,
photo_url character varying,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.topics OWNER TO postgres;
--
-- Name: users; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.users (
id uuid DEFAULT auth.uid() NOT NULL,
name character varying NOT NULL,
email character varying NOT NULL,
photo_url character varying,
description character varying,
is_blocked boolean DEFAULT false NOT NULL,
social_networks jsonb[],
created_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT users_description_check CHECK ((length((description)::text) <= 300))
);
ALTER TABLE public.users OWNER TO postgres;
--
-- Name: comment_linear; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW public.comment_linear AS
SELECT cv.id,
cv.user_id,
cv.post_id,
cv.parent_comment_id,
cv.users,
cv.content,
cv."voteCount",
cv."userVote",
cv.created_at,
((to_jsonb(p.*) || jsonb_build_object('users', to_jsonb(u.*))) || jsonb_build_object('topics', to_jsonb(t.*))) AS posts
FROM (((public.comment_with_vote cv
JOIN public.users u ON ((cv.user_id = u.id)))
JOIN public.posts p ON ((cv.post_id = p.id)))
JOIN public.topics t ON ((p.topic_id = t.id)))
GROUP BY cv.id, cv.user_id, cv.post_id, cv.parent_comment_id, cv.users, cv.content, cv."voteCount", cv."userVote", cv.created_at, ((to_jsonb(p.*) || jsonb_build_object('users', to_jsonb(u.*))) || jsonb_build_object('topics', to_jsonb(t.*)));
ALTER VIEW public.comment_linear OWNER TO postgres;
--
-- Name: comment_tree; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW public.comment_tree AS
WITH RECURSIVE comment_tree(id, user_id, post_id, parent_comment_id, users, content, "voteCount", "userVote", created_at, path, "pathSortOld", "pathSortNew", "pathSortBest", "pathSortControversial") AS (
SELECT comment_with_vote.id,
comment_with_vote.user_id,
comment_with_vote.post_id,
comment_with_vote.parent_comment_id,
comment_with_vote.users,
comment_with_vote.content,
comment_with_vote."voteCount",
comment_with_vote."userVote",
comment_with_vote.created_at,
ARRAY[comment_with_vote.id] AS path,
ARRAY[(0)::bigint, (EXTRACT(epoch FROM comment_with_vote.created_at))::bigint, comment_with_vote.id] AS "pathSortOld",
ARRAY[(0)::bigint, (- (EXTRACT(epoch FROM comment_with_vote.created_at))::bigint), comment_with_vote.id] AS "pathSortNew",
ARRAY[(0)::bigint, (- comment_with_vote."voteCount"), comment_with_vote.id] AS "pathSortBest",
ARRAY[(0)::bigint, comment_with_vote."voteCount", comment_with_vote.id] AS "pathSortControversial"
FROM public.comment_with_vote
WHERE (comment_with_vote.parent_comment_id IS NULL)
UNION
SELECT cv.id,
cv.user_id,
cv.post_id,
cv.parent_comment_id,
cv.users,
cv.content,
cv."voteCount",
cv."userVote",
cv.created_at,
(ct.path || cv.id) AS path,
((ct."pathSortOld" || (EXTRACT(epoch FROM cv.created_at))::bigint) || cv.id) AS "pathSortOld",
((ct."pathSortNew" || (- (EXTRACT(epoch FROM cv.created_at))::bigint)) || cv.id) AS "pathSortNew",
((ct."pathSortBest" || (- cv."voteCount")) || cv.id) AS "pathSortBest",
((ct."pathSortControversial" || cv."voteCount") || cv.id) AS "pathSortControversial"
FROM (public.comment_with_vote cv
JOIN comment_tree ct ON ((ct.id = cv.parent_comment_id)))
)
SELECT comment_tree.id,
comment_tree.user_id,
comment_tree.post_id,
comment_tree.parent_comment_id,
comment_tree.users,
comment_tree.content,
comment_tree."voteCount",
comment_tree."userVote",
comment_tree.created_at,
comment_tree.path,
comment_tree."pathSortOld",
comment_tree."pathSortNew",
comment_tree."pathSortBest",
comment_tree."pathSortControversial"
FROM comment_tree;
ALTER VIEW public.comment_tree OWNER TO postgres;
--
-- Name: comment_votes; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.comment_votes (
comment_id bigint NOT NULL,
user_id uuid NOT NULL,
vote smallint DEFAULT '0'::smallint,
CONSTRAINT comment_votes_vote_check CHECK ((vote = ANY (ARRAY[0, 1, '-1'::integer])))
);
ALTER TABLE public.comment_votes OWNER TO postgres;
--
-- Name: comments; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.comments (
id bigint NOT NULL,
user_id uuid NOT NULL,
post_id uuid NOT NULL,
parent_comment_id bigint,
content text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.comments OWNER TO postgres;
--
-- Name: comments_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
ALTER TABLE public.comments ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.comments_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: comment_votes comment_votes_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comment_votes
ADD CONSTRAINT comment_votes_pkey PRIMARY KEY (comment_id, user_id);
--
-- Name: comments comments_id_key; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comments
ADD CONSTRAINT comments_id_key UNIQUE (id);
--
-- Name: comments comments_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comments
ADD CONSTRAINT comments_pkey PRIMARY KEY (id);
--
-- Name: posts posts_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.posts
ADD CONSTRAINT posts_pkey PRIMARY KEY (id);
--
-- Name: topics topic_name_key; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.topics
ADD CONSTRAINT topic_name_key UNIQUE (name);
--
-- Name: topics topic_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.topics
ADD CONSTRAINT topic_pkey PRIMARY KEY (id);
--
-- Name: users users_id_key; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_id_key UNIQUE (id);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
--
-- Name: comment_with_vote _RETURN; Type: RULE; Schema: public; Owner: postgres
--
CREATE OR REPLACE VIEW public.comment_with_vote AS
SELECT c.id,
c.user_id,
c.post_id,
c.parent_comment_id,
to_jsonb(u.*) AS users,
c.content,
COALESCE(sum(cv.vote), (0)::bigint) AS "voteCount",
COALESCE((( SELECT v.vote
FROM public.comment_votes v
WHERE ((auth.uid() = v.user_id) AND (c.id = v.comment_id))))::integer, 0) AS "userVote",
c.created_at
FROM ((public.users u
JOIN public.comments c ON ((u.id = c.user_id)))
LEFT JOIN public.comment_votes cv ON ((c.id = cv.comment_id)))
GROUP BY c.id, (to_jsonb(u.*));
--
-- Name: comment_votes comment_votes_comment_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comment_votes
ADD CONSTRAINT comment_votes_comment_id_fkey FOREIGN KEY (comment_id) REFERENCES public.comments(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: comment_votes comment_votes_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comment_votes
ADD CONSTRAINT comment_votes_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: comments comments_post_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comments
ADD CONSTRAINT comments_post_id_fkey FOREIGN KEY (post_id) REFERENCES public.posts(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: comments comments_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.comments
ADD CONSTRAINT comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: posts posts_topic_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.posts
ADD CONSTRAINT posts_topic_id_fkey FOREIGN KEY (topic_id) REFERENCES public.topics(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: posts posts_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.posts
ADD CONSTRAINT posts_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: topics topics_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.topics
ADD CONSTRAINT topics_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: comment_votes Enable insert for authenticated users only; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable insert for authenticated users only" ON public.comment_votes FOR INSERT TO authenticated WITH CHECK (true);
--
-- Name: comments Enable insert for authenticated users only; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable insert for authenticated users only" ON public.comments FOR INSERT TO authenticated WITH CHECK (true);
--
-- Name: posts Enable insert for authenticated users only; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable insert for authenticated users only" ON public.posts FOR INSERT TO authenticated WITH CHECK (true);
--
-- Name: topics Enable insert for authenticated users only; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable insert for authenticated users only" ON public.topics FOR INSERT TO authenticated WITH CHECK (true);
--
-- Name: comment_votes Enable read access for all users; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable read access for all users" ON public.comment_votes FOR SELECT USING (true);
--
-- Name: comments Enable read access for all users; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable read access for all users" ON public.comments FOR SELECT USING (true);
--
-- Name: posts Enable read access for all users; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable read access for all users" ON public.posts FOR SELECT USING (true);
--
-- Name: topics Enable read access for all users; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable read access for all users" ON public.topics FOR SELECT USING (true);
--
-- Name: users Enable read access for all users; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable read access for all users" ON public.users FOR SELECT USING (true);
--
-- Name: comment_votes Enable update for users based on user_id; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable update for users based on user_id" ON public.comment_votes FOR UPDATE TO authenticated USING ((auth.uid() = user_id)) WITH CHECK ((auth.uid() = user_id));
--
-- Name: comments Enable update for users based on user_id; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable update for users based on user_id" ON public.comments FOR UPDATE TO authenticated USING ((auth.uid() = user_id)) WITH CHECK ((auth.uid() = user_id));
--
-- Name: posts Enable update for users based on user_id; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable update for users based on user_id" ON public.posts FOR UPDATE TO authenticated USING ((auth.uid() = user_id)) WITH CHECK ((auth.uid() = user_id));
--
-- Name: topics Enable update for users based on user_id; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable update for users based on user_id" ON public.topics FOR UPDATE TO authenticated USING ((auth.uid() = user_id)) WITH CHECK ((auth.uid() = user_id));
--
-- Name: users Enable update for users based on user_id; Type: POLICY; Schema: public; Owner: postgres
--
CREATE POLICY "Enable update for users based on user_id" ON public.users FOR UPDATE TO authenticated USING ((auth.uid() = id)) WITH CHECK ((auth.uid() = id));
--
-- Name: comment_votes; Type: ROW SECURITY; Schema: public; Owner: postgres
--
ALTER TABLE public.comment_votes ENABLE ROW LEVEL SECURITY;
--
-- Name: comments; Type: ROW SECURITY; Schema: public; Owner: postgres
--
ALTER TABLE public.comments ENABLE ROW LEVEL SECURITY;
--
-- Name: posts; Type: ROW SECURITY; Schema: public; Owner: postgres
--
ALTER TABLE public.posts ENABLE ROW LEVEL SECURITY;
--
-- Name: topics; Type: ROW SECURITY; Schema: public; Owner: postgres
--
ALTER TABLE public.topics ENABLE ROW LEVEL SECURITY;
--
-- Name: users; Type: ROW SECURITY; Schema: public; Owner: postgres
--
ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;