-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
412 lines (354 loc) · 13.2 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
412 lines (354 loc) · 13.2 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
-- Run this in Supabase Dashboard -> SQL Editor
-- Sets up tables, indexes, and row-level security for the blog
-- USERS (Supabase already has auth.users; this is your public profile table)
create table public.profiles (
id uuid references auth.users on delete cascade primary key,
username text unique not null,
avatar_url text,
bio text,
created_at timestamp with time zone default now()
);
-- POSTS
create table public.posts (
id uuid default gen_random_uuid() primary key,
author_id uuid references public.profiles(id) on delete cascade not null,
title text not null,
slug text unique not null,
content_md text not null,
cover_image text,
status text default 'draft' check (status in ('draft', 'published')),
published_at timestamp with time zone,
created_at timestamp with time zone default now()
);
create index posts_slug_idx on public.posts (slug);
create index posts_status_idx on public.posts (status, published_at desc);
-- TAGS
create table public.tags (
id uuid default gen_random_uuid() primary key,
name text unique not null,
slug text unique not null
);
create table public.post_tags (
post_id uuid references public.posts(id) on delete cascade,
tag_id uuid references public.tags(id) on delete cascade,
primary key (post_id, tag_id)
);
-- COMMENTS
create table public.comments (
id uuid default gen_random_uuid() primary key,
post_id uuid references public.posts(id) on delete cascade not null,
author_id uuid references public.profiles(id) on delete cascade not null,
parent_id uuid references public.comments(id) on delete cascade,
content text not null,
created_at timestamp with time zone default now()
);
-- REACTIONS (likes)
create table public.reactions (
post_id uuid references public.posts(id) on delete cascade,
user_id uuid references public.profiles(id) on delete cascade,
type text default 'like',
primary key (post_id, user_id)
);
-- ROW LEVEL SECURITY
alter table public.profiles enable row level security;
alter table public.posts enable row level security;
alter table public.comments enable row level security;
alter table public.reactions enable row level security;
-- Anyone can read published posts; only the author can read/write their drafts
create policy "Public posts are viewable by everyone"
on public.posts for select
using (status = 'published' or auth.uid() = author_id);
create policy "Authors can insert their own posts"
on public.posts for insert
with check (auth.uid() = author_id);
create policy "Authors can update their own posts"
on public.posts for update
using (auth.uid() = author_id);
create policy "Authors can delete their own posts"
on public.posts for delete
using (auth.uid() = author_id);
-- Profiles: anyone can read, only owner can update
create policy "Profiles are viewable by everyone"
on public.profiles for select
using (true);
create policy "Users can update their own profile"
on public.profiles for update
using (auth.uid() = id);
-- Comments: anyone can read, logged-in users can write
create policy "Comments are viewable by everyone"
on public.comments for select
using (true);
create policy "Logged-in users can comment"
on public.comments for insert
with check (auth.uid() = author_id);
-- Reactions: anyone can read, logged-in users can react
create policy "Reactions are viewable by everyone"
on public.reactions for select
using (true);
create policy "Logged-in users can react"
on public.reactions for insert
with check (auth.uid() = user_id);
create policy "Users can remove their own reaction"
on public.reactions for delete
using (auth.uid() = user_id);
-- Trigger to create a public profile when a user signs up
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.profiles (id, username, avatar_url)
values (
new.id,
coalesce(new.raw_user_meta_data->>'user_name', split_part(new.email, '@', 1)),
new.raw_user_meta_data->>'avatar_url'
);
return new;
end;
$$ language plpgsql security definer;
create or replace trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
-- Full-Text search indexer RPC function with ranking
create or replace function public.search_posts(search_query text)
returns table (
id uuid,
title text,
slug text,
content_md text,
published_at timestamp with time zone,
cover_image text,
author_username text,
author_avatar_url text
) as $$
begin
return query
select
p.id,
p.title,
p.slug,
p.content_md,
p.published_at,
p.cover_image,
pr.username as author_username,
pr.avatar_url as author_avatar_url
from public.posts p
join public.profiles pr on p.author_id = pr.id
where p.status = 'published'
and (
to_tsvector('english', p.title || ' ' || p.content_md) @@ websearch_to_tsquery('english', search_query)
or p.title ilike '%' || search_query || '%'
or pr.username ilike '%' || search_query || '%'
)
order by ts_rank(to_tsvector('english', p.title || ' ' || p.content_md), websearch_to_tsquery('english', search_query)) desc, p.published_at desc;
end;
$$ language plpgsql security definer;
-- =============================================================================
-- PHASE 4 — Community graph, notifications, view counts, live discussions
-- Safe to run on existing databases (IF NOT EXISTS / exception guards)
-- =============================================================================
alter table public.posts
add column if not exists view_count integer not null default 0;
drop function if exists public.search_posts(text);
create function public.search_posts(search_query text)
returns table (
id uuid,
title text,
slug text,
content_md text,
published_at timestamp with time zone,
cover_image text,
view_count integer,
author_username text,
author_avatar_url text
) as $$
begin
return query
select
p.id,
p.title,
p.slug,
p.content_md,
p.published_at,
p.cover_image,
coalesce(p.view_count, 0) as view_count,
pr.username as author_username,
pr.avatar_url as author_avatar_url
from public.posts p
join public.profiles pr on p.author_id = pr.id
where p.status = 'published'
and (
to_tsvector('english', p.title || ' ' || p.content_md) @@ websearch_to_tsquery('english', search_query)
or p.title ilike '%' || search_query || '%'
or pr.username ilike '%' || search_query || '%'
)
order by ts_rank(to_tsvector('english', p.title || ' ' || p.content_md), websearch_to_tsquery('english', search_query)) desc, p.published_at desc;
end;
$$ language plpgsql security definer;
create table if not exists public.follows (
follower_id uuid references public.profiles(id) on delete cascade not null,
following_id uuid references public.profiles(id) on delete cascade not null,
created_at timestamp with time zone default now(),
primary key (follower_id, following_id),
check (follower_id <> following_id)
);
create index if not exists follows_following_id_idx on public.follows (following_id);
create index if not exists follows_follower_id_idx on public.follows (follower_id);
create table if not exists public.bookmarks (
user_id uuid references public.profiles(id) on delete cascade not null,
post_id uuid references public.posts(id) on delete cascade not null,
created_at timestamp with time zone default now(),
primary key (user_id, post_id)
);
create index if not exists bookmarks_user_id_idx on public.bookmarks (user_id, created_at desc);
create table if not exists public.notifications (
id uuid default gen_random_uuid() primary key,
user_id uuid references public.profiles(id) on delete cascade not null,
actor_id uuid references public.profiles(id) on delete cascade,
type text not null check (type in ('like', 'comment', 'reply', 'follow')),
post_id uuid references public.posts(id) on delete cascade,
comment_id uuid references public.comments(id) on delete cascade,
read boolean not null default false,
created_at timestamp with time zone default now()
);
create index if not exists notifications_user_id_idx on public.notifications (user_id, read, created_at desc);
alter table public.follows enable row level security;
alter table public.bookmarks enable row level security;
alter table public.notifications enable row level security;
drop policy if exists "Follows are viewable by everyone" on public.follows;
create policy "Follows are viewable by everyone"
on public.follows for select
using (true);
drop policy if exists "Users can follow others" on public.follows;
create policy "Users can follow others"
on public.follows for insert
with check (auth.uid() = follower_id);
drop policy if exists "Users can unfollow" on public.follows;
create policy "Users can unfollow"
on public.follows for delete
using (auth.uid() = follower_id);
drop policy if exists "Users can view their own bookmarks" on public.bookmarks;
create policy "Users can view their own bookmarks"
on public.bookmarks for select
using (auth.uid() = user_id);
drop policy if exists "Users can bookmark posts" on public.bookmarks;
create policy "Users can bookmark posts"
on public.bookmarks for insert
with check (auth.uid() = user_id);
drop policy if exists "Users can remove bookmarks" on public.bookmarks;
create policy "Users can remove bookmarks"
on public.bookmarks for delete
using (auth.uid() = user_id);
drop policy if exists "Users can view their notifications" on public.notifications;
create policy "Users can view their notifications"
on public.notifications for select
using (auth.uid() = user_id);
drop policy if exists "Users can update their notifications" on public.notifications;
create policy "Users can update their notifications"
on public.notifications for update
using (auth.uid() = user_id);
drop policy if exists "Users can delete their notifications" on public.notifications;
create policy "Users can delete their notifications"
on public.notifications for delete
using (auth.uid() = user_id);
drop policy if exists "Users can delete their own comments" on public.comments;
create policy "Users can delete their own comments"
on public.comments for delete
using (auth.uid() = author_id);
drop policy if exists "Users can insert their own profile" on public.profiles;
create policy "Users can insert their own profile"
on public.profiles for insert
with check (auth.uid() = id);
create or replace function public.increment_post_views(target_post_id uuid)
returns void
language plpgsql
security definer
set search_path = public
as $$
begin
update public.posts
set view_count = coalesce(view_count, 0) + 1
where id = target_post_id
and status = 'published';
end;
$$;
grant execute on function public.increment_post_views(uuid) to anon, authenticated;
grant execute on function public.search_posts(text) to anon, authenticated;
create or replace function public.notify_on_like()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
declare
post_author uuid;
begin
select author_id into post_author from public.posts where id = new.post_id;
if post_author is not null and post_author <> new.user_id then
insert into public.notifications (user_id, actor_id, type, post_id)
values (post_author, new.user_id, 'like', new.post_id);
end if;
return new;
end;
$$;
drop trigger if exists on_reaction_created on public.reactions;
create trigger on_reaction_created
after insert on public.reactions
for each row execute procedure public.notify_on_like();
create or replace function public.notify_on_comment()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
declare
post_author uuid;
parent_author uuid;
begin
select author_id into post_author from public.posts where id = new.post_id;
if new.parent_id is not null then
select author_id into parent_author from public.comments where id = new.parent_id;
if parent_author is not null and parent_author <> new.author_id then
insert into public.notifications (user_id, actor_id, type, post_id, comment_id)
values (parent_author, new.author_id, 'reply', new.post_id, new.id);
end if;
end if;
if post_author is not null
and post_author <> new.author_id
and post_author is distinct from parent_author then
insert into public.notifications (user_id, actor_id, type, post_id, comment_id)
values (post_author, new.author_id, 'comment', new.post_id, new.id);
end if;
return new;
end;
$$;
drop trigger if exists on_comment_created on public.comments;
create trigger on_comment_created
after insert on public.comments
for each row execute procedure public.notify_on_comment();
create or replace function public.notify_on_follow()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
begin
insert into public.notifications (user_id, actor_id, type)
values (new.following_id, new.follower_id, 'follow');
return new;
end;
$$;
drop trigger if exists on_follow_created on public.follows;
create trigger on_follow_created
after insert on public.follows
for each row execute procedure public.notify_on_follow();
do $$
begin
alter publication supabase_realtime add table public.notifications;
exception
when duplicate_object then null;
end $$;
do $$
begin
alter publication supabase_realtime add table public.comments;
exception
when duplicate_object then null;
end $$;