-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsupabase_migration_clean.sql
More file actions
247 lines (213 loc) · 10.1 KB
/
supabase_migration_clean.sql
File metadata and controls
247 lines (213 loc) · 10.1 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
-- BroCode Supabase Database Migration (Idempotent Version)
-- This version can be run multiple times safely
-- Run this SQL in your Supabase SQL Editor
-- ============================================================================
-- 1. PROFILES TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
username TEXT UNIQUE NOT NULL,
email TEXT,
phone TEXT,
password TEXT,
role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('admin', 'user', 'guest')),
profile_pic_url TEXT,
location TEXT,
date_of_birth TEXT,
is_verified BOOLEAN DEFAULT false,
latitude NUMERIC,
longitude NUMERIC,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE UNIQUE INDEX IF NOT EXISTS profiles_username_unique ON profiles(username);
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
-- Drop all possible profile policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Users can read all profiles" ON profiles;
DROP POLICY IF EXISTS "Users can update own profile" ON profiles;
DROP POLICY IF EXISTS "Anyone can insert profiles" ON profiles;
END $$;
CREATE POLICY "Users can read all profiles" ON profiles FOR SELECT USING (true);
CREATE POLICY "Users can update own profile" ON profiles FOR UPDATE USING (true);
CREATE POLICY "Anyone can insert profiles" ON profiles FOR INSERT WITH CHECK (true);
-- ============================================================================
-- 2. SPOTS TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS spots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
date TIMESTAMP WITH TIME ZONE NOT NULL,
day TEXT NOT NULL,
timing TEXT NOT NULL,
budget NUMERIC NOT NULL DEFAULT 0,
location TEXT NOT NULL,
created_by UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
description TEXT,
feedback TEXT,
latitude NUMERIC,
longitude NUMERIC,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS spots_date_idx ON spots(date);
CREATE INDEX IF NOT EXISTS spots_created_by_idx ON spots(created_by);
ALTER TABLE spots ENABLE ROW LEVEL SECURITY;
-- Drop all possible spot policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Everyone can read spots" ON spots;
DROP POLICY IF EXISTS "Admins can create spots" ON spots;
DROP POLICY IF EXISTS "Anyone can create spots" ON spots;
DROP POLICY IF EXISTS "Admins can update spots" ON spots;
DROP POLICY IF EXISTS "Anyone can update spots" ON spots;
DROP POLICY IF EXISTS "Admins can delete spots" ON spots;
DROP POLICY IF EXISTS "Anyone can delete spots" ON spots;
END $$;
CREATE POLICY "Everyone can read spots" ON spots FOR SELECT USING (true);
CREATE POLICY "Anyone can create spots" ON spots FOR INSERT WITH CHECK (true);
CREATE POLICY "Anyone can update spots" ON spots FOR UPDATE USING (true);
CREATE POLICY "Anyone can delete spots" ON spots FOR DELETE USING (true);
-- ============================================================================
-- 3. INVITATIONS TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS invitations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
spot_id UUID NOT NULL REFERENCES spots(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('confirmed', 'pending', 'declined')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(spot_id, user_id)
);
CREATE INDEX IF NOT EXISTS invitations_spot_id_idx ON invitations(spot_id);
CREATE INDEX IF NOT EXISTS invitations_user_id_idx ON invitations(user_id);
ALTER TABLE invitations ENABLE ROW LEVEL SECURITY;
-- Drop all possible invitation policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Everyone can read invitations" ON invitations;
DROP POLICY IF EXISTS "Users can manage own invitations" ON invitations;
DROP POLICY IF EXISTS "Anyone can create invitations" ON invitations;
END $$;
CREATE POLICY "Everyone can read invitations" ON invitations FOR SELECT USING (true);
CREATE POLICY "Users can manage own invitations" ON invitations FOR ALL USING (true);
CREATE POLICY "Anyone can create invitations" ON invitations FOR INSERT WITH CHECK (true);
-- ============================================================================
-- 4. PAYMENTS TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS payments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
spot_id UUID NOT NULL REFERENCES spots(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
status TEXT NOT NULL DEFAULT 'not_paid' CHECK (status IN ('paid', 'not_paid')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(spot_id, user_id)
);
CREATE INDEX IF NOT EXISTS payments_spot_id_idx ON payments(spot_id);
CREATE INDEX IF NOT EXISTS payments_user_id_idx ON payments(user_id);
ALTER TABLE payments ENABLE ROW LEVEL SECURITY;
-- Drop all possible payment policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Everyone can read payments" ON payments;
DROP POLICY IF EXISTS "Admins can update payments" ON payments;
DROP POLICY IF EXISTS "Anyone can update payments" ON payments;
DROP POLICY IF EXISTS "System can create payments" ON payments;
END $$;
CREATE POLICY "Everyone can read payments" ON payments FOR SELECT USING (true);
CREATE POLICY "Anyone can update payments" ON payments FOR UPDATE USING (true);
CREATE POLICY "System can create payments" ON payments FOR INSERT WITH CHECK (true);
-- ============================================================================
-- 5. CHAT_MESSAGES TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS chat_messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
content_text TEXT,
content_image_urls TEXT[],
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
reactions JSONB DEFAULT '{}'::jsonb
);
CREATE INDEX IF NOT EXISTS chat_messages_created_at_idx ON chat_messages(created_at);
ALTER TABLE chat_messages ENABLE ROW LEVEL SECURITY;
-- Drop all possible chat message policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Everyone can read messages" ON chat_messages;
DROP POLICY IF EXISTS "Users can create own messages" ON chat_messages;
DROP POLICY IF EXISTS "Users can delete own messages" ON chat_messages;
END $$;
CREATE POLICY "Everyone can read messages" ON chat_messages FOR SELECT USING (true);
CREATE POLICY "Users can create own messages" ON chat_messages FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can delete own messages" ON chat_messages FOR DELETE USING (true);
-- ============================================================================
-- 6. MOMENTS TABLE
-- ============================================================================
CREATE TABLE IF NOT EXISTS moments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
image_url TEXT NOT NULL,
caption TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS moments_user_id_idx ON moments(user_id);
CREATE INDEX IF NOT EXISTS moments_created_at_idx ON moments(created_at DESC);
ALTER TABLE moments ENABLE ROW LEVEL SECURITY;
-- Drop all possible moment policies
DO $$
BEGIN
DROP POLICY IF EXISTS "Everyone can read moments" ON moments;
DROP POLICY IF EXISTS "Users can create own moments" ON moments;
DROP POLICY IF EXISTS "Users can delete own moments" ON moments;
END $$;
CREATE POLICY "Everyone can read moments" ON moments FOR SELECT USING (true);
CREATE POLICY "Users can create own moments" ON moments FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can delete own moments" ON moments FOR DELETE USING (true);
-- ============================================================================
-- 7. INITIAL DATA
-- ============================================================================
-- Insert admin user
INSERT INTO profiles (id, name, username, email, phone, password, role, location, is_verified)
VALUES (
'00000000-0000-0000-0000-000000000001',
'Ram',
'brocode',
'brocode@gmail.com',
'7826821130',
'admin@brocode',
'admin',
'Attibele',
true
)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
username = EXCLUDED.username,
email = EXCLUDED.email,
phone = EXCLUDED.phone,
password = EXCLUDED.password,
role = EXCLUDED.role,
location = EXCLUDED.location,
is_verified = EXCLUDED.is_verified;
-- Insert other users
INSERT INTO profiles (id, name, username, phone, password, role, location, is_verified)
VALUES
('00000000-0000-0000-0000-000000000002', 'Dhanush', 'dhanush', '9994323520', 'dhanush123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000003', 'Godwin', 'godwin', '8903955341', 'godwin123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000004', 'Tharun', 'tharun', '9345624112', 'tharun123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000005', 'Sanjay', 'sanjay', '9865703667', 'sanjay123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000006', 'Soundar', 'soundar', '9566686921', 'soundar123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000007', 'Jagadeesh', 'jagadeesh', '6381038172', 'jagadeesh123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000008', 'Ram', 'ram', '7826821130', 'ram123', 'user', 'Attibele', true),
('00000000-0000-0000-0000-000000000009', 'Lingesh', 'lingesh', '', 'lingesh123', 'user', 'Attibele', true)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
username = EXCLUDED.username,
phone = EXCLUDED.phone,
role = EXCLUDED.role,
location = EXCLUDED.location,
is_verified = EXCLUDED.is_verified;
-- Verify tables were created
SELECT 'Migration completed successfully! All tables and policies are set up.' as status;