Repository navigation
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
94 lines (81 loc) · 3.42 KB
/
Copy pathsupabase-setup.sql
File metadata and controls
94 lines (81 loc) · 3.42 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
-- ============================================
-- MDEPLOY SUPABASE DATABASE SETUP
-- Run this entire file in Supabase SQL Editor
-- ============================================
-- ============================================
-- 1. CREATE PROJECTS TABLE
-- ============================================
CREATE TABLE IF NOT EXISTS projects (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('website', 'web_app', 'mobile_app', 'desktop_app')),
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'in_progress', 'completed', 'cancelled')),
client_name TEXT NOT NULL,
client_email TEXT NOT NULL,
price DECIMAL(10, 2) NOT NULL DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_projects_created_at ON projects(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Allow all operations for now" ON projects;
CREATE POLICY "Allow all operations for now" ON projects
FOR ALL
USING (true)
WITH CHECK (true);
-- ============================================
-- 2. CREATE PROFILES TABLE
-- ============================================
CREATE TABLE IF NOT EXISTS profiles (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
full_name TEXT,
role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('admin', 'user')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc', NOW()) NOT NULL
);
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
-- Drop existing policies if they exist
DROP POLICY IF EXISTS "Users can read their own profile" ON profiles;
DROP POLICY IF EXISTS "Users can update their own profile" ON profiles;
DROP POLICY IF EXISTS "Admins can read all profiles" ON profiles;
-- Allow users to read their own profile
CREATE POLICY "Users can read their own profile" ON profiles
FOR SELECT
USING (auth.uid() = id);
-- Allow authenticated users to update their own profile (except role)
CREATE POLICY "Users can update their own profile" ON profiles
FOR UPDATE
USING (auth.uid() = id)
WITH CHECK (auth.uid() = id);
-- Allow admins to read all profiles
CREATE POLICY "Admins can read all profiles" ON profiles
FOR SELECT
USING ((SELECT role FROM profiles WHERE id = auth.uid()) = 'admin');
-- ============================================
-- 3. CREATE PROFILE CREATION TRIGGER
-- ============================================
CREATE OR REPLACE FUNCTION create_profile_on_signup()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO profiles (id, email, full_name, role)
VALUES (NEW.id, NEW.email, NEW.raw_user_meta_data->>'full_name', 'user');
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
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 create_profile_on_signup();
-- ============================================
-- 4. DONE!
-- Next step: Update your email to admin role
-- Run this query after signing up with your email:
--
-- UPDATE profiles
-- SET role = 'admin'
-- WHERE email = 'your-email@example.com';
-- ============================================