-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
116 lines (98 loc) · 3.97 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
116 lines (98 loc) · 3.97 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
-- ============================================================
-- Croissant – Supabase SQL Schema
-- ============================================================
-- 1. Profiles table (extends Supabase auth.users)
CREATE TABLE public.profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email TEXT,
full_name TEXT,
avatar_url TEXT,
credits INTEGER NOT NULL DEFAULT 3,
subscription_tier TEXT NOT NULL DEFAULT 'free' CHECK (subscription_tier IN ('free', 'pro', 'enterprise')),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Auto-create profile on signup
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, email, full_name, avatar_url)
VALUES (
NEW.id,
NEW.email,
COALESCE(NEW.raw_user_meta_data->>'full_name', ''),
COALESCE(NEW.raw_user_meta_data->>'avatar_url', '')
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- 2. Generations history table
CREATE TABLE public.generations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES public.profiles(id) ON DELETE SET NULL,
image_url TEXT NOT NULL,
heatmap_base64 TEXT,
raw_metrics JSONB,
gemini_analysis TEXT,
is_anonymous BOOLEAN NOT NULL DEFAULT false,
gemini_latency_ms INTEGER,
session_id TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_generations_user_id ON public.generations(user_id);
CREATE INDEX idx_generations_created_at ON public.generations(created_at DESC);
CREATE INDEX idx_generations_session_id ON public.generations(session_id);
-- 3. RLS Policies
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.generations ENABLE ROW LEVEL SECURITY;
-- Profiles: users can read/update their own profile
CREATE POLICY "Users can view own profile" ON public.profiles
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "Users can update own profile" ON public.profiles
FOR UPDATE USING (auth.uid() = id);
-- Generations: users can view their own generations
CREATE POLICY "Users can view own generations" ON public.generations
FOR SELECT USING (auth.uid() = user_id);
-- Generations: insert policy (allow authenticated and anonymous inserts via service role)
CREATE POLICY "Service role can insert generations" ON public.generations
FOR INSERT WITH CHECK (true);
-- Generations: anonymous generations are publicly readable
CREATE POLICY "Anonymous generations are public" ON public.generations
FOR SELECT USING (is_anonymous = true);
-- 4. Storage bucket for thumbnails
INSERT INTO storage.buckets (id, name, public) VALUES ('thumbnails', 'thumbnails', true);
-- Storage policies: allow public uploads (max 5MB images)
CREATE POLICY "Allow public thumbnail uploads" ON storage.objects
FOR INSERT WITH CHECK (
bucket_id = 'thumbnails'
AND (LOWER(storage.extension(name)) IN ('jpg', 'jpeg', 'png', 'webp', 'gif'))
);
CREATE POLICY "Allow public thumbnail reads" ON storage.objects
FOR SELECT USING (bucket_id = 'thumbnails');
-- 5. Updated_at trigger for profiles
CREATE OR REPLACE FUNCTION public.update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER profiles_updated_at
BEFORE UPDATE ON public.profiles
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at();
-- 6. Trial usage tracking
CREATE TABLE IF NOT EXISTS public.trial_usage (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ip_address TEXT NOT NULL,
device_fingerprint TEXT,
analysis_count INTEGER NOT NULL DEFAULT 0,
last_used_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(ip_address, device_fingerprint)
);
CREATE INDEX IF NOT EXISTS idx_trial_usage_ip ON public.trial_usage(ip_address);
ALTER TABLE public.trial_usage ENABLE ROW LEVEL SECURITY;