-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
194 lines (160 loc) · 6.56 KB
/
supabase-schema.sql
File metadata and controls
194 lines (160 loc) · 6.56 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
-- Financial Tracker Database Schema
-- Run this SQL in your Supabase SQL Editor
-- Enable UUID extension (if not already enabled)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- =====================================================
-- TABLE 1: transactions
-- Stores all financial transactions (income/expenses)
-- =====================================================
CREATE TABLE IF NOT EXISTS transactions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
amount DECIMAL(12, 2) NOT NULL,
description TEXT,
category TEXT,
type TEXT CHECK (type IN ('income', 'expense')) NOT NULL,
date DATE NOT NULL DEFAULT CURRENT_DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add index for faster queries
CREATE INDEX IF NOT EXISTS transactions_user_id_idx ON transactions(user_id);
CREATE INDEX IF NOT EXISTS transactions_type_idx ON transactions(type);
CREATE INDEX IF NOT EXISTS transactions_date_idx ON transactions(date DESC);
-- =====================================================
-- TABLE 2: categories
-- Stores transaction categories
-- =====================================================
CREATE TABLE IF NOT EXISTS categories (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
name TEXT NOT NULL,
color TEXT DEFAULT '#6366f1',
icon TEXT,
type TEXT CHECK (type IN ('income', 'expense', 'both')) DEFAULT 'expense',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add index
CREATE INDEX IF NOT EXISTS categories_user_id_idx ON categories(user_id);
-- =====================================================
-- TABLE 3: budgets
-- Stores budget planning data
-- =====================================================
CREATE TABLE IF NOT EXISTS budgets (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
category TEXT NOT NULL,
amount DECIMAL(12, 2) NOT NULL,
period TEXT CHECK (period IN ('daily', 'weekly', 'monthly', 'yearly')) DEFAULT 'monthly',
start_date DATE NOT NULL DEFAULT CURRENT_DATE,
end_date DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add index
CREATE INDEX IF NOT EXISTS budgets_user_id_idx ON budgets(user_id);
CREATE INDEX IF NOT EXISTS budgets_period_idx ON budgets(period);
-- =====================================================
-- ROW LEVEL SECURITY (RLS) POLICIES
-- =====================================================
-- Enable RLS on all tables
-- Set REPLICA IDENTITY FULL for all tables
ALTER TABLE transactions REPLICA IDENTITY FULL;
ALTER TABLE categories REPLICA IDENTITY FULL;
ALTER TABLE budgets REPLICA IDENTITY FULL;
-- Transactions Policies
-- Users can only see their own transactions
CREATE POLICY "Users can view own transactions" ON transactions
FOR SELECT
USING (auth.uid() = user_id);
-- Users can insert their own transactions
CREATE POLICY "Users can insert own transactions" ON transactions
FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Users can update their own transactions
CREATE POLICY "Users can update own transactions" ON transactions
FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Users can delete their own transactions
CREATE POLICY "Users can delete own transactions" ON transactions
FOR DELETE
USING (auth.uid() = user_id);
-- Categories Policies
CREATE POLICY "Users can view own categories" ON categories
FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own categories" ON categories
FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own categories" ON categories
FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own categories" ON categories
FOR DELETE
USING (auth.uid() = user_id);
-- Budgets Policies
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)
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own budgets" ON budgets
FOR DELETE
USING (auth.uid() = user_id);
-- =====================================================
-- ENABLE REALTIME REPLICATION
-- =====================================================
-- Enable realtime for all tables
ALTER PUBLICATION supabase_realtime ADD TABLE transactions;
ALTER PUBLICATION supabase_realtime ADD TABLE categories;
ALTER PUBLICATION supabase_realtime ADD TABLE budgets;
-- =====================================================
-- FUNCTIONS FOR AUTO-UPDATING timestamps
-- =====================================================
-- Create 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;
-- Create triggers for auto-updating updated_at
CREATE TRIGGER update_transactions_updated_at
BEFORE UPDATE ON transactions
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_budgets_updated_at
BEFORE UPDATE ON budgets
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- =====================================================
-- SAMPLE DATA (Optional - for testing)
-- =====================================================
-- Note: Replace 'YOUR_USER_ID' with an actual user ID from auth.users
-- You can get this by running: SELECT id FROM auth.users LIMIT 1;
-- INSERT INTO categories (user_id, name, color, type) VALUES
-- ('YOUR_USER_ID', 'Groceries', '#10b981', 'expense'),
-- ('YOUR_USER_ID', 'Salary', '#3b82f6', 'income'),
-- ('YOUR_USER_ID', 'Transportation', '#f59e0b', 'expense'),
-- ('YOUR_USER_ID', 'Entertainment', '#ec4899', 'expense');
-- =====================================================
-- VERIFICATION QUERIES
-- =====================================================
-- Check if tables were created
-- SELECT table_name FROM information_schema.tables
-- WHERE table_schema = 'public'
-- AND table_name IN ('transactions', 'categories', 'budgets');
-- Check RLS is enabled
-- SELECT tablename, rowsecurity FROM pg_tables
-- WHERE schemaname = 'public'
-- AND tablename IN ('transactions', 'categories', 'budgets');
-- Check realtime is enabled
-- SELECT * FROM pg_publication_tables WHERE pubname = 'supabase_realtime';