-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-migration.sql
More file actions
162 lines (135 loc) · 5.8 KB
/
supabase-migration.sql
File metadata and controls
162 lines (135 loc) · 5.8 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Supabase Migration for cashflow.pilot
-- Run this in your Supabase SQL Editor (https://supabase.com/dashboard/project/_/sql)
--
-- This creates all necessary tables for cloud storage:
-- - accounts: User bank accounts
-- - transactions: Income and expense records
-- - budgets: Spending limits by category
-- - goals: Financial goals with progress tracking
-- - recurring_patterns: Automated recurring transactions
--
-- All tables include Row Level Security (RLS) policies to ensure
-- users can only access their own data.
-- Create accounts table
CREATE TABLE accounts (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('checking', 'savings', 'credit', 'investment')),
initial_balance DECIMAL(15, 2) NOT NULL,
currency TEXT NOT NULL DEFAULT 'USD',
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
-- Create transactions table
CREATE TABLE transactions (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
account_id UUID REFERENCES accounts(id) ON DELETE CASCADE NOT NULL,
amount DECIMAL(15, 2) NOT NULL,
description TEXT NOT NULL,
category TEXT NOT NULL,
date TIMESTAMPTZ NOT NULL,
type TEXT NOT NULL CHECK (type IN ('income', 'expense')),
is_recurring BOOLEAN NOT NULL DEFAULT FALSE,
recurring_id UUID,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
-- Create budgets table
CREATE TABLE budgets (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
category TEXT NOT NULL,
amount DECIMAL(15, 2) NOT NULL,
period TEXT NOT NULL CHECK (period IN ('weekly', 'monthly', 'yearly')),
start_date TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
-- Create goals table
CREATE TABLE goals (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
name TEXT NOT NULL,
target_amount DECIMAL(15, 2) NOT NULL,
current_amount DECIMAL(15, 2) NOT NULL,
deadline TIMESTAMPTZ NOT NULL,
status TEXT NOT NULL CHECK (status IN ('active', 'achieved', 'overdue')),
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
-- Create recurring_patterns table
CREATE TABLE recurring_patterns (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
account_id UUID REFERENCES accounts(id) ON DELETE CASCADE NOT NULL,
amount DECIMAL(15, 2) NOT NULL,
description TEXT NOT NULL,
category TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('income', 'expense')),
frequency TEXT NOT NULL CHECK (frequency IN ('daily', 'weekly', 'monthly', 'yearly')),
start_date TIMESTAMPTZ NOT NULL,
last_occurrence TIMESTAMPTZ,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL
);
-- Create indexes for better query performance
CREATE INDEX idx_accounts_user_id ON accounts(user_id);
CREATE INDEX idx_transactions_user_id ON transactions(user_id);
CREATE INDEX idx_transactions_account_id ON transactions(account_id);
CREATE INDEX idx_transactions_date ON transactions(date);
CREATE INDEX idx_budgets_user_id ON budgets(user_id);
CREATE INDEX idx_goals_user_id ON goals(user_id);
CREATE INDEX idx_recurring_patterns_user_id ON recurring_patterns(user_id);
-- Enable Row Level Security
ALTER TABLE accounts ENABLE ROW LEVEL SECURITY;
ALTER TABLE transactions ENABLE ROW LEVEL SECURITY;
ALTER TABLE budgets ENABLE ROW LEVEL SECURITY;
ALTER TABLE goals ENABLE ROW LEVEL SECURITY;
ALTER TABLE recurring_patterns ENABLE ROW LEVEL SECURITY;
-- RLS Policies for accounts
CREATE POLICY "Users can view own accounts" ON accounts
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own accounts" ON accounts
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own accounts" ON accounts
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own accounts" ON accounts
FOR DELETE USING (auth.uid() = user_id);
-- RLS Policies for transactions
CREATE POLICY "Users can view own transactions" ON transactions
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own transactions" ON transactions
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own transactions" ON transactions
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own transactions" ON transactions
FOR DELETE USING (auth.uid() = user_id);
-- RLS Policies for budgets
CREATE POLICY "Users can view own budgets" ON budgets
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own budgets" ON budgets
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own budgets" ON budgets
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own budgets" ON budgets
FOR DELETE USING (auth.uid() = user_id);
-- RLS Policies for goals
CREATE POLICY "Users can view own goals" ON goals
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own goals" ON goals
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own goals" ON goals
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own goals" ON goals
FOR DELETE USING (auth.uid() = user_id);
-- RLS Policies for recurring_patterns
CREATE POLICY "Users can view own recurring_patterns" ON recurring_patterns
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own recurring_patterns" ON recurring_patterns
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own recurring_patterns" ON recurring_patterns
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own recurring_patterns" ON recurring_patterns
FOR DELETE USING (auth.uid() = user_id);