-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
91 lines (77 loc) · 3.09 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
91 lines (77 loc) · 3.09 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
-- Supabase Schema for Ilya's Top 30 Pro
-- Run this in your Supabase SQL Editor
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- User profiles table
CREATE TABLE user_profiles (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
clerk_id TEXT UNIQUE NOT NULL,
email TEXT NOT NULL,
lemonsqueezy_customer_id TEXT,
subscription_status TEXT DEFAULT 'free' CHECK (subscription_status IN ('free', 'active', 'cancelled', 'past_due')),
subscription_id TEXT,
daily_question_limit INTEGER DEFAULT 5,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Usage records table (tracks daily AI question usage)
CREATE TABLE usage_records (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES user_profiles(id) ON DELETE CASCADE,
date DATE NOT NULL DEFAULT CURRENT_DATE,
question_count INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, date)
);
-- Completed papers table (tracks user progress)
CREATE TABLE completed_papers (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES user_profiles(id) ON DELETE CASCADE,
paper_id INTEGER NOT NULL,
completed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, paper_id)
);
-- Study streaks table
CREATE TABLE study_streaks (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES user_profiles(id) ON DELETE CASCADE UNIQUE,
current_streak INTEGER DEFAULT 0,
longest_streak INTEGER DEFAULT 0,
last_study_date DATE,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes for performance
CREATE INDEX idx_user_profiles_clerk_id ON user_profiles(clerk_id);
CREATE INDEX idx_user_profiles_lemonsqueezy_customer_id ON user_profiles(lemonsqueezy_customer_id);
CREATE INDEX idx_usage_records_user_date ON usage_records(user_id, date);
CREATE INDEX idx_completed_papers_user ON completed_papers(user_id);
-- Row Level Security (RLS) policies
ALTER TABLE user_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE usage_records ENABLE ROW LEVEL SECURITY;
ALTER TABLE completed_papers ENABLE ROW LEVEL SECURITY;
ALTER TABLE study_streaks ENABLE ROW LEVEL SECURITY;
-- Policy: Users can read their own profile
CREATE POLICY "Users can read own profile" ON user_profiles
FOR SELECT USING (true); -- Service role handles auth
-- Policy: Service role can do everything
CREATE POLICY "Service role full access to profiles" ON user_profiles
FOR ALL USING (true);
CREATE POLICY "Service role full access to usage" ON usage_records
FOR ALL USING (true);
CREATE POLICY "Service role full access to papers" ON completed_papers
FOR ALL USING (true);
CREATE POLICY "Service role full access to streaks" ON study_streaks
FOR ALL USING (true);
-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Trigger for user_profiles
CREATE TRIGGER update_user_profiles_updated_at
BEFORE UPDATE ON user_profiles
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();