-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
58 lines (50 loc) · 1.74 KB
/
supabase-setup.sql
File metadata and controls
58 lines (50 loc) · 1.74 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
-- CODE CRISPIES - Supabase Database Setup
-- Run this in Supabase Dashboard → SQL Editor → New Query
-- Drop existing objects first
DROP FUNCTION IF EXISTS delete_own_account();
DROP TABLE IF EXISTS user_progress;
DROP TABLE IF EXISTS newsletter_subscribers;
-- User progress table
CREATE TABLE user_progress (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
progress JSONB NOT NULL DEFAULT '{}',
user_code JSONB NOT NULL DEFAULT '{}',
settings JSONB NOT NULL DEFAULT '{}',
language TEXT DEFAULT 'en',
updated_at TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id)
);
-- Newsletter subscribers table
CREATE TABLE newsletter_subscribers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT UNIQUE NOT NULL,
subscribed_at TIMESTAMPTZ DEFAULT NOW()
);
-- Row Level Security
ALTER TABLE user_progress ENABLE ROW LEVEL SECURITY;
ALTER TABLE newsletter_subscribers ENABLE ROW LEVEL SECURITY;
-- Users can only access their own progress
CREATE POLICY "Users can CRUD own progress"
ON user_progress FOR ALL
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Anyone can subscribe to newsletter (public insert)
CREATE POLICY "Anyone can subscribe to newsletter"
ON newsletter_subscribers FOR INSERT
WITH CHECK (true);
-- Function to delete own account (called via RPC)
CREATE OR REPLACE FUNCTION delete_own_account()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
-- Delete user's progress (CASCADE should handle this, but be explicit)
DELETE FROM user_progress WHERE user_id = auth.uid();
-- Delete the user from auth.users
DELETE FROM auth.users WHERE id = auth.uid();
END;
$$;