Files
Sauci/apps/supabase/migrations/20231227000006_couple_packs.sql
T
2025-12-27 19:10:11 +00:00

42 lines
1.3 KiB
SQL

-- Create couple_packs table to track enabled packs per couple
CREATE TABLE public.couple_packs (
couple_id UUID NOT NULL REFERENCES public.couples(id) ON DELETE CASCADE,
pack_id UUID NOT NULL REFERENCES public.question_packs(id) ON DELETE CASCADE,
enabled BOOLEAN DEFAULT TRUE,
created_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (couple_id, pack_id)
);
-- RLS Policies
ALTER TABLE public.couple_packs ENABLE ROW LEVEL SECURITY;
-- Couples can view their own pack settings
CREATE POLICY "Couples can view their own pack settings"
ON public.couple_packs
FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.profiles
WHERE profiles.id = auth.uid()
AND profiles.couple_id = couple_packs.couple_id
)
);
-- Couples can insert/update their own pack settings
CREATE POLICY "Couples can modify their own pack settings"
ON public.couple_packs
FOR ALL
USING (
EXISTS (
SELECT 1 FROM public.profiles
WHERE profiles.id = auth.uid()
AND profiles.couple_id = couple_packs.couple_id
)
);
-- Function to handle default packs for new couples (Optional, but good UX)
-- For now, we'll just assume if no record exists, it's not enabled?
-- Or maybe better: if no record exists, it IS enabled if it's not premium?
-- Actually, the user wants to "select which packs are active".
-- Let's stick to explicit records.