-
Notifications
You must be signed in to change notification settings - Fork 12
/
notes.sql
264 lines (183 loc) · 10.3 KB
/
notes.sql
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
-- get replies count
drop view sce_comments_with_metadata;
create view sce_comments_with_metadata as select *, (select count(*) from sce_comments as c where c.parent_id = sce_comments.id) as replies_count from sce_comments;
-- unique constraint on comment_reactions
ALTER TABLE sce_comment_reactions ADD UNIQUE (user_id, comment_id, reaction_type);
-- aggregate metadata for comment reactions
create or replace view sce_comment_reactions_metadata as SELECT comment_id, reaction_type, COUNT(*) as reaction_count, BOOL_OR(user_id = auth.uid()) as active_for_user FROM sce_comment_reactions GROUP BY (comment_id, reaction_type) ORDER BY reaction_type;
-- display_users view for user avatars
create or replace view sce_display_users as select
id,
coalesce(raw_user_meta_data ->> 'name', raw_user_meta_data ->> 'full_name', raw_user_meta_data ->> 'user_name') as name,
coalesce(raw_user_meta_data ->> 'avatar_url', raw_user_meta_data ->> 'avatar') as avatar
from auth.users;
-- RELOADING SCHEMA CACHE
-- Create an event trigger function
CREATE OR REPLACE FUNCTION public.pgrst_watch() RETURNS event_trigger
LANGUAGE plpgsql
AS $$
BEGIN
NOTIFY pgrst, 'reload schema';
END;
$$;
-- This event trigger will fire after every ddl_command_end event
CREATE EVENT TRIGGER pgrst_watch
ON ddl_command_end
EXECUTE PROCEDURE public.pgrst_watch();
-- cascade deletes for comments to delete replies when parent deleted
alter table public.sce_comments
drop constraint sce_comments_parent_id_fkey;
alter table public.sce_comments
add constraint sce_comments_parent_id_fkey
foreign key (parent_id)
references public.sce_comments (id)
on delete cascade;
-- add some basic reactions
insert into sce_reactions(type, label, url) values ('heart', 'Heart', 'https://emojis.slackmojis.com/emojis/images/1596061862/9845/meow_heart.png?1596061862');
insert into sce_reactions(type, label, url) values ('like', 'Like', 'https://emojis.slackmojis.com/emojis/images/1588108689/8789/fb-like.png?1588108689');
insert into sce_reactions(type, label, url) values ('party-blob', 'Party Blob', 'https://emojis.slackmojis.com/emojis/images/1547582922/5197/party_blob.gif?1547582922');
-- GENERATED FROM MIGRA
create table "public"."comment_reactions" (
"id" uuid not null default uuid_generate_v4(),
"created_at" timestamp with time zone default now(),
"comment_id" uuid not null,
"user_id" uuid not null,
"reaction_type" character varying not null
);
create table "public"."comments" (
"id" uuid not null default uuid_generate_v4(),
"created_at" timestamp with time zone default now(),
"topic" character varying not null,
"comment" character varying not null,
"user_id" uuid not null,
"parent_id" uuid,
"mentioned_user_ids" uuid[] not null default '{}'::uuid[]
);
create table "public"."profiles" (
"id" uuid not null,
"created_at" timestamp with time zone default now(),
"name" character varying,
"avatar" character varying
);
create table "public"."reactions" (
"type" character varying not null,
"created_at" timestamp with time zone default now(),
"metadata" jsonb
);
CREATE UNIQUE INDEX comment_reactions_pkey ON public.comment_reactions USING btree (id);
CREATE UNIQUE INDEX comment_reactions_user_id_comment_id_reaction_type_key ON public.comment_reactions USING btree (user_id, comment_id, reaction_type);
CREATE UNIQUE INDEX comments_pkey ON public.comments USING btree (id);
CREATE UNIQUE INDEX profiles_pkey ON public.profiles USING btree (id);
CREATE UNIQUE INDEX reactions_pkey ON public.reactions USING btree (type);
alter table "public"."comment_reactions" add constraint "comment_reactions_pkey" PRIMARY KEY using index "comment_reactions_pkey";
alter table "public"."comments" add constraint "comments_pkey" PRIMARY KEY using index "comments_pkey";
alter table "public"."profiles" add constraint "profiles_pkey" PRIMARY KEY using index "profiles_pkey";
alter table "public"."reactions" add constraint "reactions_pkey" PRIMARY KEY using index "reactions_pkey";
alter table "public"."comment_reactions" add constraint "comment_reactions_comment_id_fkey" FOREIGN KEY (comment_id) REFERENCES comments(id);
alter table "public"."comment_reactions" add constraint "comment_reactions_reaction_type_fkey" FOREIGN KEY (reaction_type) REFERENCES reactions(type);
alter table "public"."comment_reactions" add constraint "comment_reactions_user_id_comment_id_reaction_type_key" UNIQUE using index "comment_reactions_user_id_comment_id_reaction_type_key";
alter table "public"."comment_reactions" add constraint "comment_reactions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id);
alter table "public"."comments" add constraint "comments_parent_id_fkey" FOREIGN KEY (parent_id) REFERENCES comments(id) ON DELETE CASCADE;
alter table "public"."comments" add constraint "comments_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id);
alter table "public"."profiles" add constraint "profiles_id_fkey" FOREIGN KEY (id) REFERENCES auth.users(id);
set check_function_bodies = off;
create or replace view "public"."comment_reactions_metadata" as SELECT comment_reactions.comment_id,
comment_reactions.reaction_type,
count(*) AS reaction_count,
bool_or((comment_reactions.user_id = auth.uid())) AS active_for_user
FROM comment_reactions
GROUP BY comment_reactions.comment_id, comment_reactions.reaction_type;
create or replace view "public"."comment_reactions_metadata_two" as SELECT comment_reactions.comment_id,
comment_reactions.reaction_type,
count(*) AS reaction_count
FROM comment_reactions
GROUP BY comment_reactions.comment_id, comment_reactions.reaction_type;
create or replace view "public"."comments_with_metadata" as SELECT comments.id,
comments.created_at,
comments.topic,
comments.comment,
comments.user_id,
comments.parent_id,
comments.mentioned_user_ids,
( SELECT count(*) AS count
FROM comments c
WHERE (c.parent_id = comments.id)) AS replies_count
FROM comments;
create or replace view "public"."display_users" as SELECT users.id,
COALESCE((users.raw_user_meta_data ->> 'name'::text), (users.raw_user_meta_data ->> 'full_name'::text), (users.raw_user_meta_data ->> 'user_name'::text)) AS name,
COALESCE((users.raw_user_meta_data ->> 'avatar_url'::text), (users.raw_user_meta_data ->> 'avatar'::text)) AS avatar
FROM auth.users;
-- MIGRA DIFF TWO
create table "public"."comment_reactions" (
"id" uuid not null default uuid_generate_v4(),
"created_at" timestamp with time zone default now(),
"comment_id" uuid not null,
"user_id" uuid not null,
"reaction_type" character varying not null
);
create table "public"."comments" (
"id" uuid not null default uuid_generate_v4(),
"created_at" timestamp with time zone default now(),
"topic" character varying not null,
"comment" character varying not null,
"user_id" uuid not null,
"parent_id" uuid,
"mentioned_user_ids" uuid[] not null default '{}'::uuid[]
);
create table "public"."reactions" (
"type" character varying not null,
"created_at" timestamp with time zone default now(),
"metadata" jsonb,
"label" character varying not null,
"url" character varying not null
);
CREATE UNIQUE INDEX comment_reactions_pkey ON public.comment_reactions USING btree (id);
CREATE UNIQUE INDEX comment_reactions_user_id_comment_id_reaction_type_key ON public.comment_reactions USING btree (user_id, comment_id, reaction_type);
CREATE UNIQUE INDEX comments_pkey ON public.comments USING btree (id);
CREATE UNIQUE INDEX reactions_pkey ON public.reactions USING btree (type);
alter table "public"."comment_reactions" add constraint "comment_reactions_pkey" PRIMARY KEY using index "comment_reactions_pkey";
alter table "public"."comments" add constraint "comments_pkey" PRIMARY KEY using index "comments_pkey";
alter table "public"."reactions" add constraint "reactions_pkey" PRIMARY KEY using index "reactions_pkey";
alter table "public"."comment_reactions" add constraint "comment_reactions_comment_id_fkey" FOREIGN KEY (comment_id) REFERENCES comments(id);
alter table "public"."comment_reactions" add constraint "comment_reactions_reaction_type_fkey" FOREIGN KEY (reaction_type) REFERENCES reactions(type);
alter table "public"."comment_reactions" add constraint "comment_reactions_user_id_comment_id_reaction_type_key" UNIQUE using index "comment_reactions_user_id_comment_id_reaction_type_key";
alter table "public"."comment_reactions" add constraint "comment_reactions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id);
alter table "public"."comments" add constraint "comments_parent_id_fkey" FOREIGN KEY (parent_id) REFERENCES comments(id) ON DELETE CASCADE;
alter table "public"."comments" add constraint "comments_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id);
set check_function_bodies = off;
create or replace view "public"."comment_reactions_metadata" as SELECT comment_reactions.comment_id,
comment_reactions.reaction_type,
count(*) AS reaction_count,
bool_or((comment_reactions.user_id = auth.uid())) AS active_for_user
FROM comment_reactions
GROUP BY comment_reactions.comment_id, comment_reactions.reaction_type
ORDER BY comment_reactions.reaction_type;
create or replace view "public"."comment_reactions_metadata_two" as SELECT comment_reactions.comment_id,
comment_reactions.reaction_type,
count(*) AS reaction_count
FROM comment_reactions
GROUP BY comment_reactions.comment_id, comment_reactions.reaction_type;
create or replace view "public"."comments_with_metadata" as SELECT comments.id,
comments.created_at,
comments.topic,
comments.comment,
comments.user_id,
comments.parent_id,
comments.mentioned_user_ids,
( SELECT count(*) AS count
FROM comments c
WHERE (c.parent_id = comments.id)) AS replies_count
FROM comments;
create or replace view "public"."display_users" as SELECT users.id,
COALESCE((users.raw_user_meta_data ->> 'name'::text), (users.raw_user_meta_data ->> 'full_name'::text), (users.raw_user_meta_data ->> 'user_name'::text)) AS name,
COALESCE((users.raw_user_meta_data ->> 'avatar_url'::text), (users.raw_user_meta_data ->> 'avatar'::text)) AS avatar
FROM auth.users;
CREATE OR REPLACE FUNCTION public.pgrst_watch()
RETURNS event_trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NOTIFY pgrst, 'reload schema';
END;
$function$
;