-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete-database-setup.sql
More file actions
344 lines (293 loc) · 11.3 KB
/
complete-database-setup.sql
File metadata and controls
344 lines (293 loc) · 11.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
-- COMPLETE DATABASE SETUP FOR RAJA TICKETING SYSTEM
-- Run this script in Supabase SQL Editor to create all necessary tables
-- ========================================
-- 1. CREATE UTILITY FUNCTIONS FIRST
-- ========================================
-- Function to update updated_at timestamps
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- ========================================
-- 2. CREATE USERS TABLE (references auth.users)
-- ========================================
CREATE TABLE IF NOT EXISTS users (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
full_name VARCHAR(255),
role VARCHAR(50) DEFAULT 'user',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable Row Level Security for users
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- Create policies for users table
DROP POLICY IF EXISTS "Users can view their own profile" ON users;
DROP POLICY IF EXISTS "Users can update their own profile" ON users;
DROP POLICY IF EXISTS "Admins can view all users" ON users;
CREATE POLICY "Users can view their own profile" ON users
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "Users can update their own profile" ON users
FOR UPDATE USING (auth.uid() = id);
CREATE POLICY "Admins can view all users" ON users
FOR ALL USING (auth.role() = 'authenticated');
-- Create indexes for users
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_role ON users(role);
-- Create trigger for users
DROP TRIGGER IF EXISTS update_users_updated_at ON users;
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ========================================
-- 3. CREATE EVENTS TABLE
-- ========================================
CREATE TABLE IF NOT EXISTS events (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
event_date DATE NOT NULL,
event_time TIME NOT NULL,
location VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
capacity INTEGER NOT NULL,
image_url TEXT,
start_registration TIMESTAMP WITH TIME ZONE,
end_registration TIMESTAMP WITH TIME ZONE,
ticket_photo_url TEXT,
additional_info TEXT,
category VARCHAR(100),
organizer_name VARCHAR(255),
organizer_contact VARCHAR(255),
venue_details TEXT,
dress_code VARCHAR(100),
age_restriction VARCHAR(50),
gallery_images TEXT[],
status VARCHAR(20) DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for events
CREATE INDEX IF NOT EXISTS idx_events_status ON events(status);
CREATE INDEX IF NOT EXISTS idx_events_date ON events(event_date);
CREATE INDEX IF NOT EXISTS idx_events_created_at ON events(created_at);
-- Enable Row Level Security for events
ALTER TABLE events ENABLE ROW LEVEL SECURITY;
-- Create policies for events
DROP POLICY IF EXISTS "Admins can manage all events" ON events;
DROP POLICY IF EXISTS "Public can view active events" ON events;
CREATE POLICY "Admins can manage all events" ON events
FOR ALL USING (auth.role() = 'authenticated');
CREATE POLICY "Public can view active events" ON events
FOR SELECT USING (status = 'active');
-- Create trigger for events
DROP TRIGGER IF EXISTS update_events_updated_at ON events;
CREATE TRIGGER update_events_updated_at
BEFORE UPDATE ON events
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ========================================
-- 4. CREATE REGISTRATIONS TABLE
-- ========================================
CREATE TABLE IF NOT EXISTS registrations (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
event_id UUID REFERENCES events(id) ON DELETE CASCADE NOT NULL,
status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected')),
payment_status TEXT DEFAULT 'pending' CHECK (payment_status IN ('pending', 'paid', 'refunded')),
payment_method TEXT DEFAULT 'manual',
notes TEXT,
-- Check-in tracking columns
checked_in BOOLEAN DEFAULT FALSE,
checked_in_at TIMESTAMP WITH TIME ZONE,
checked_in_by UUID REFERENCES auth.users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, event_id)
);
-- Create indexes for registrations
CREATE INDEX IF NOT EXISTS idx_registrations_user_id ON registrations(user_id);
CREATE INDEX IF NOT EXISTS idx_registrations_event_id ON registrations(event_id);
CREATE INDEX IF NOT EXISTS idx_registrations_status ON registrations(status);
CREATE INDEX IF NOT EXISTS idx_registrations_checked_in ON registrations(checked_in);
CREATE INDEX IF NOT EXISTS idx_registrations_checked_in_at ON registrations(checked_in_at);
-- Enable Row Level Security for registrations
ALTER TABLE registrations ENABLE ROW LEVEL SECURITY;
-- Create policies for registrations
DROP POLICY IF EXISTS "Users can view their own registrations" ON registrations;
DROP POLICY IF EXISTS "Users can create registrations" ON registrations;
DROP POLICY IF EXISTS "Admins can manage all registrations" ON registrations;
CREATE POLICY "Users can view their own registrations" ON registrations
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can create registrations" ON registrations
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Admins can manage all registrations" ON registrations
FOR ALL USING (auth.role() = 'authenticated');
-- Create trigger for registrations
DROP TRIGGER IF EXISTS update_registrations_updated_at ON registrations;
CREATE TRIGGER update_registrations_updated_at
BEFORE UPDATE ON registrations
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ========================================
-- 5. CREATE FAMILY MEMBERS TABLE
-- ========================================
CREATE TABLE IF NOT EXISTS family_members (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
full_name VARCHAR(255) NOT NULL,
age INTEGER,
relationship VARCHAR(100),
notes TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for family_members
CREATE INDEX IF NOT EXISTS idx_family_members_user_id ON family_members(user_id);
CREATE INDEX IF NOT EXISTS idx_family_members_created_at ON family_members(created_at);
-- Enable Row Level Security for family_members
ALTER TABLE family_members ENABLE ROW LEVEL SECURITY;
-- Create policies for family_members
DROP POLICY IF EXISTS "Users can manage their own family members" ON family_members;
DROP POLICY IF EXISTS "Admins can view all family members" ON family_members;
CREATE POLICY "Users can manage their own family members" ON family_members
FOR ALL USING (auth.uid() = user_id);
CREATE POLICY "Admins can view all family members" ON family_members
FOR SELECT USING (auth.role() = 'authenticated');
-- Create trigger for family_members
DROP TRIGGER IF EXISTS update_family_members_updated_at ON family_members;
CREATE TRIGGER update_family_members_updated_at
BEFORE UPDATE ON family_members
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ========================================
-- 6. CREATE SUPERUSERS TABLE
-- ========================================
CREATE TABLE IF NOT EXISTS superusers (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Insert default superuser (update email as needed)
INSERT INTO superusers (email)
VALUES ('superuser@example.com')
ON CONFLICT (email) DO NOTHING;
-- ========================================
-- 7. CREATE HELPER FUNCTIONS
-- ========================================
-- Function to handle new user signup
CREATE OR REPLACE FUNCTION handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.users (id, email, full_name, role)
VALUES (
NEW.id,
NEW.email,
COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.email),
COALESCE(NEW.raw_user_meta_data->>'role', 'user')
)
ON CONFLICT (id) DO NOTHING;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Create trigger for new user signup
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 handle_new_user();
-- Function to get admins
CREATE OR REPLACE FUNCTION get_admins()
RETURNS TABLE (
id UUID,
email VARCHAR,
created_at TIMESTAMP WITH TIME ZONE
)
SECURITY DEFINER
AS $$
BEGIN
RETURN QUERY
SELECT
auth.users.id,
auth.users.email,
auth.users.created_at
FROM auth.users
WHERE
(auth.users.raw_user_meta_data->>'role' = 'admin' OR auth.users.user_metadata->>'role' = 'admin')
AND auth.users.email != 'superuser@example.com';
END;
$$ LANGUAGE plpgsql;
-- Function to get all users
CREATE OR REPLACE FUNCTION get_all_users()
RETURNS TABLE (
id UUID,
email VARCHAR,
created_at TIMESTAMP WITH TIME ZONE
)
SECURITY DEFINER
AS $$
BEGIN
RETURN QUERY
SELECT
auth.users.id,
auth.users.email,
auth.users.created_at
FROM auth.users
WHERE auth.users.email != 'superuser@example.com';
END;
$$ LANGUAGE plpgsql;
-- Function to get family members for a user
CREATE OR REPLACE FUNCTION get_family_members_for_user(target_user_id UUID)
RETURNS TABLE (
id UUID,
full_name VARCHAR,
age INTEGER,
relationship VARCHAR,
notes TEXT,
created_at TIMESTAMP WITH TIME ZONE
)
SECURITY DEFINER
AS $$
BEGIN
RETURN QUERY
SELECT
family_members.id,
family_members.full_name,
family_members.age,
family_members.relationship,
family_members.notes,
family_members.created_at
FROM family_members
WHERE family_members.user_id = target_user_id
ORDER BY family_members.created_at ASC;
END;
$$ LANGUAGE plpgsql;
-- ========================================
-- 8. MIGRATE EXISTING AUTH USERS
-- ========================================
-- Migrate existing users from auth.users to public.users
INSERT INTO public.users (id, email, full_name, role)
SELECT
au.id,
au.email,
COALESCE(au.raw_user_meta_data->>'full_name', au.email) as full_name,
COALESCE(au.raw_user_meta_data->>'role', 'user') as role
FROM auth.users au
ON CONFLICT (id) DO UPDATE SET
email = EXCLUDED.email,
full_name = COALESCE(EXCLUDED.full_name, users.full_name),
role = COALESCE(EXCLUDED.role, users.role);
-- ========================================
-- 9. GRANT PERMISSIONS
-- ========================================
GRANT SELECT ON auth.users TO postgres;
GRANT EXECUTE ON FUNCTION get_admins() TO postgres;
GRANT EXECUTE ON FUNCTION get_all_users() TO postgres;
GRANT EXECUTE ON FUNCTION get_family_members_for_user(UUID) TO postgres;
-- ========================================
-- SUCCESS MESSAGE
-- ========================================
SELECT 'Complete database setup completed successfully! All tables created with check-in functionality.' as result;