-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_setup_usage.sql
More file actions
36 lines (31 loc) · 1.06 KB
/
supabase_setup_usage.sql
File metadata and controls
36 lines (31 loc) · 1.06 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
-- Create user_usage table for Free/Pro gating
CREATE TABLE IF NOT EXISTS user_usage (
user_id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
tier TEXT CHECK (tier IN ('free', 'pro')) DEFAULT 'free' NOT NULL,
questions_today INT DEFAULT 0 NOT NULL,
last_reset_date DATE DEFAULT CURRENT_DATE NOT NULL
);
-- Enable RLS
ALTER TABLE user_usage ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can read their own usage"
ON user_usage
FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can update their own usage"
ON user_usage
FOR UPDATE
USING (auth.uid() = user_id);
-- Create a trigger to auto-create user_usage row on signup
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.user_usage (user_id)
VALUES (new.id);
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Bind the trigger to Auth Users
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 PROCEDURE public.handle_new_user();