-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
87 lines (70 loc) · 3.31 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
87 lines (70 loc) · 3.31 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
-- Enable RLS (Row Level Security)
alter table auth.users enable row level security;
-- Create wallets table
CREATE TABLE IF NOT EXISTS public.wallets (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
name TEXT NOT NULL,
balance DECIMAL NOT NULL DEFAULT 0,
type TEXT NOT NULL,
color TEXT NOT NULL
);
-- Create transactions table
CREATE TABLE IF NOT EXISTS public.transactions (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
amount DECIMAL NOT NULL,
category TEXT NOT NULL,
description TEXT,
date TEXT NOT NULL,
type TEXT NOT NULL,
wallet_id UUID REFERENCES public.wallets(id) ON DELETE CASCADE NOT NULL
);
-- Create budgets table
CREATE TABLE IF NOT EXISTS public.budgets (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
category TEXT NOT NULL,
amount DECIMAL NOT NULL,
spent DECIMAL NOT NULL DEFAULT 0,
period TEXT NOT NULL
);
-- Enable Row Level Security on all tables
ALTER TABLE public.wallets ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.transactions ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.budgets ENABLE ROW LEVEL SECURITY;
-- Create policies for wallets (only owner can access)
CREATE POLICY "Users can view their own wallets" ON public.wallets
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own wallets" ON public.wallets
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own wallets" ON public.wallets
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own wallets" ON public.wallets
FOR DELETE USING (auth.uid() = user_id);
-- Create policies for transactions (only owner can access)
CREATE POLICY "Users can view their own transactions" ON public.transactions
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own transactions" ON public.transactions
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own transactions" ON public.transactions
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own transactions" ON public.transactions
FOR DELETE USING (auth.uid() = user_id);
-- Create policies for budgets (only owner can access)
CREATE POLICY "Users can view their own budgets" ON public.budgets
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own budgets" ON public.budgets
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own budgets" ON public.budgets
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own budgets" ON public.budgets
FOR DELETE USING (auth.uid() = user_id);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS wallets_user_id_idx ON public.wallets (user_id);
CREATE INDEX IF NOT EXISTS transactions_user_id_idx ON public.transactions (user_id);
CREATE INDEX IF NOT EXISTS transactions_wallet_id_idx ON public.transactions (wallet_id);
CREATE INDEX IF NOT EXISTS budgets_user_id_idx ON public.budgets (user_id);