Add community post likes and comments tables with RLS
cgen-9b605b8c72754979ac44468155a1f075
This commit is contained in:
parent
aef065275e
commit
63f7d30070
1 changed files with 46 additions and 0 deletions
46
supabase/migrations/20251018_community_likes_comments.sql
Normal file
46
supabase/migrations/20251018_community_likes_comments.sql
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
-- Community post likes and comments
|
||||
begin;
|
||||
|
||||
-- likes table for community_posts
|
||||
create table if not exists public.community_post_likes (
|
||||
post_id uuid not null references public.community_posts(id) on delete cascade,
|
||||
user_id uuid not null references public.user_profiles(id) on delete cascade,
|
||||
created_at timestamptz not null default now(),
|
||||
primary key (post_id, user_id)
|
||||
);
|
||||
|
||||
-- comments table for community_posts
|
||||
create table if not exists public.community_comments (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
post_id uuid not null references public.community_posts(id) on delete cascade,
|
||||
user_id uuid not null references public.user_profiles(id) on delete cascade,
|
||||
content text not null,
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
alter table public.community_post_likes enable row level security;
|
||||
alter table public.community_comments enable row level security;
|
||||
|
||||
-- policies: users can read all published post likes/comments
|
||||
DO $$ BEGIN
|
||||
CREATE POLICY community_post_likes_read ON public.community_post_likes
|
||||
FOR SELECT TO authenticated USING (true);
|
||||
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE POLICY community_comments_read ON public.community_comments
|
||||
FOR SELECT TO authenticated USING (true);
|
||||
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
||||
|
||||
-- users manage their own likes/comments
|
||||
DO $$ BEGIN
|
||||
CREATE POLICY community_post_likes_manage_self ON public.community_post_likes
|
||||
FOR ALL TO authenticated USING (user_id = auth.uid()) WITH CHECK (user_id = auth.uid());
|
||||
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE POLICY community_comments_manage_self ON public.community_comments
|
||||
FOR ALL TO authenticated USING (user_id = auth.uid()) WITH CHECK (user_id = auth.uid());
|
||||
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
||||
|
||||
commit;
|
||||
Loading…
Reference in a new issue