-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupabase_setup.sql
More file actions
142 lines (122 loc) · 4.73 KB
/
supabase_setup.sql
File metadata and controls
142 lines (122 loc) · 4.73 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
-- Supabase SQL Setup for Reviews Feature
-- Run this in your Supabase SQL Editor
-- First, ensure the reviews table exists with the correct schema
CREATE TABLE IF NOT EXISTS public.reviews (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
space_id UUID NOT NULL,
user_id UUID NOT NULL,
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
comment TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL
);
-- Delete legacy spaces that don't have a host_id
DELETE FROM public.spaces WHERE host_id IS NULL;
-- Create a table for public profiles
CREATE TABLE IF NOT EXISTS public.profiles (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
name TEXT,
role TEXT DEFAULT 'CLIENT',
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL
);
-- Enable RLS on profiles
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
-- Allow public read access to profiles
CREATE POLICY "Public profiles are viewable by everyone"
ON public.profiles FOR SELECT
USING (true);
-- Allow users to insert their own profile
CREATE POLICY "Users can insert their own profile"
ON public.profiles FOR INSERT
WITH CHECK (auth.uid() = id);
-- Allow users to update their own profile
CREATE POLICY "Users can update own profile"
ON public.profiles FOR UPDATE
USING (auth.uid() = id);
-- Function to handle new user signup
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, name, role)
VALUES (new.id, new.raw_user_meta_data->>'name', new.raw_user_meta_data->>'role');
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Trigger to call the function on new user creation
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- Backfill existing users into profiles (Run this manually if needed, or include here)
-- Note: This might fail if run multiple times due to PK constraint, so we use ON CONFLICT DO NOTHING
INSERT INTO public.profiles (id, name, role)
SELECT id, raw_user_meta_data->>'name', raw_user_meta_data->>'role'
FROM auth.users
ON CONFLICT (id) DO NOTHING;
-- Ensure spaces table has host_id
ALTER TABLE public.spaces ADD COLUMN IF NOT EXISTS host_id UUID REFERENCES auth.users(id);
-- Add foreign key constraints if they don't exist
-- This assumes you have a 'spaces' table and an 'auth.users' table or 'users' table
DO $$
BEGIN
-- Add foreign key to spaces table (if not exists)
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'reviews_space_id_fkey'
AND table_name = 'reviews'
) THEN
ALTER TABLE public.reviews
ADD CONSTRAINT reviews_space_id_fkey
FOREIGN KEY (space_id)
REFERENCES public.spaces(id)
ON DELETE CASCADE;
END IF;
-- Add foreign key to auth.users table (if not exists)
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'reviews_user_id_fkey'
AND table_name = 'reviews'
) THEN
ALTER TABLE public.reviews
ADD CONSTRAINT reviews_user_id_fkey
FOREIGN KEY (user_id)
REFERENCES auth.users(id)
ON DELETE CASCADE;
END IF;
END $$;
-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS reviews_space_id_idx ON public.reviews(space_id);
CREATE INDEX IF NOT EXISTS reviews_user_id_idx ON public.reviews(user_id);
CREATE INDEX IF NOT EXISTS reviews_created_at_idx ON public.reviews(created_at DESC);
-- Enable Row Level Security
ALTER TABLE public.reviews ENABLE ROW LEVEL SECURITY;
-- Drop existing policies if they exist
DROP POLICY IF EXISTS "Anyone can view reviews" ON public.reviews;
DROP POLICY IF EXISTS "Authenticated users can create reviews" ON public.reviews;
DROP POLICY IF EXISTS "Users can update their own reviews" ON public.reviews;
DROP POLICY IF EXISTS "Users can delete their own reviews" ON public.reviews;
-- RLS Policies
-- Allow anyone to read reviews (including anonymous users)
CREATE POLICY "Anyone can view reviews"
ON public.reviews
FOR SELECT
TO public
USING (true);
-- Allow authenticated users to insert reviews
CREATE POLICY "Authenticated users can create reviews"
ON public.reviews
FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);
-- Allow users to update their own reviews
CREATE POLICY "Users can update their own reviews"
ON public.reviews
FOR UPDATE
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Allow users to delete their own reviews
CREATE POLICY "Users can delete their own reviews"
ON public.reviews
FOR DELETE
TO authenticated
USING (auth.uid() = user_id);