41 lines
1.7 KiB
SQL
41 lines
1.7 KiB
SQL
-- User Follows Migration for BroadcastBeat
|
|
-- Adds follow/unfollow functionality between users
|
|
|
|
-- ============================================================
|
|
-- 1. USER FOLLOWS TABLE
|
|
-- ============================================================
|
|
CREATE TABLE IF NOT EXISTS public.user_follows (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
follower_id UUID NOT NULL REFERENCES public.user_profiles(id) ON DELETE CASCADE,
|
|
following_id UUID NOT NULL REFERENCES public.user_profiles(id) ON DELETE CASCADE,
|
|
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
CONSTRAINT user_follows_no_self_follow CHECK (follower_id != following_id)
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_follows_unique
|
|
ON public.user_follows (follower_id, following_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_follows_follower_id
|
|
ON public.user_follows (follower_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_follows_following_id
|
|
ON public.user_follows (following_id);
|
|
|
|
-- ============================================================
|
|
-- 2. ENABLE RLS
|
|
-- ============================================================
|
|
ALTER TABLE public.user_follows ENABLE ROW LEVEL SECURITY;
|
|
|
|
-- ============================================================
|
|
-- 3. RLS POLICIES
|
|
-- ============================================================
|
|
DROP POLICY IF EXISTS "public_read_user_follows" ON public.user_follows;
|
|
CREATE POLICY "public_read_user_follows"
|
|
ON public.user_follows FOR SELECT TO public USING (true);
|
|
|
|
DROP POLICY IF EXISTS "users_manage_own_follows" ON public.user_follows;
|
|
CREATE POLICY "users_manage_own_follows"
|
|
ON public.user_follows FOR ALL TO authenticated
|
|
USING (follower_id = auth.uid())
|
|
WITH CHECK (follower_id = auth.uid());
|