Spaces:
Sleeping
Sleeping
File size: 3,826 Bytes
c824289 | 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 | -- #############################################################
-- EXPENSE IQ: COMPLETE DATABASE SETUP SCRIPT
-- Paste this entire script into the Supabase SQL Editor
-- #############################################################
-- 1. PROFILES TABLE
-- This table stores additional user metadata and links to Supabase Auth
CREATE TABLE profiles (
id UUID REFERENCES auth.users(id) PRIMARY KEY,
full_name TEXT NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Enable RLS for Profiles
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
-- Policies for Profiles
CREATE POLICY "Users can only view their own profile"
ON profiles FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can only update their own profile"
ON profiles FOR UPDATE
USING (auth.uid() = id);
-- 2. EXPENSES TABLE
-- Main storage for all transaction data
CREATE TABLE expenses (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID REFERENCES profiles(id) NOT NULL,
vendor_name TEXT NOT NULL,
total_amount NUMERIC(10,2) NOT NULL,
category TEXT NOT NULL CHECK (category IN (
'Food', 'Travel', 'Groceries', 'Shopping', 'Health', 'Utilities', 'Entertainment', 'Other'
)),
payment_mode TEXT DEFAULT 'Cash',
date DATE NOT NULL,
raw_ocr_text TEXT,
is_manual BOOLEAN DEFAULT TRUE,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Index for performance on queries by user and date
CREATE INDEX idx_expenses_user_date ON expenses(user_id, date);
-- Enable RLS for Expenses
ALTER TABLE expenses ENABLE ROW LEVEL SECURITY;
-- Policies for Expenses
CREATE POLICY "Users can fully manage their own expenses"
ON expenses FOR ALL
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- 3. BUDGETS TABLE
-- Stores monthly limits per category
CREATE TABLE budgets (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID REFERENCES profiles(id) NOT NULL,
category TEXT NOT NULL CHECK (category IN (
'Food', 'Travel', 'Groceries', 'Shopping', 'Health', 'Utilities', 'Entertainment', 'Other'
)),
limit_amount NUMERIC(10,2) NOT NULL,
month TEXT NOT NULL, -- Format: YYYY-MM
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, category, month)
);
-- Enable RLS for Budgets
ALTER TABLE budgets ENABLE ROW LEVEL SECURITY;
-- Policies for Budgets
CREATE POLICY "Users can fully manage their own budgets"
ON budgets FOR ALL
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- 4. PREDICTIONS TABLE
-- Stores AI forecast data
CREATE TABLE predictions (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID REFERENCES profiles(id) NOT NULL,
predicted_date DATE NOT NULL,
predicted_amount NUMERIC(10,2) NOT NULL,
model_used TEXT NOT NULL CHECK (model_used IN ('LSTM', 'ARIMA')),
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Enable RLS for Predictions
ALTER TABLE predictions ENABLE ROW LEVEL SECURITY;
-- Policies for Predictions
CREATE POLICY "Users can fully manage their own predictions"
ON predictions FOR ALL
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- 5. AUTOMATIC PROFILE CREATION TRIGGER
-- This ensures a profile is created every time a user signs up via Auth
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, full_name, email)
VALUES (
NEW.id,
COALESCE(NEW.raw_user_meta_data->>'full_name', 'New Operative'),
NEW.email
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_user();
-- #############################################################
-- SETUP COMPLETE
-- Your database is now production-ready for ExpenseIQ
-- #############################################################
|