sum
This commit is contained in:
37
supabase/migrations/001_profiles.sql
Normal file
37
supabase/migrations/001_profiles.sql
Normal file
@@ -0,0 +1,37 @@
|
||||
-- Profiles table (extends Supabase auth.users)
|
||||
create table profiles (
|
||||
id uuid primary key references auth.users(id) on delete cascade,
|
||||
display_name text,
|
||||
locale text not null default 'en',
|
||||
currency text not null default 'EUR',
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
alter table profiles enable row level security;
|
||||
|
||||
create policy "Users can read own profile"
|
||||
on profiles for select
|
||||
using (id = auth.uid());
|
||||
|
||||
create policy "Users can update own profile"
|
||||
on profiles for update
|
||||
using (id = auth.uid())
|
||||
with check (id = auth.uid());
|
||||
|
||||
create policy "Users can insert own profile"
|
||||
on profiles for insert
|
||||
with check (id = auth.uid());
|
||||
|
||||
-- Auto-create profile on user signup
|
||||
create or replace function handle_new_user()
|
||||
returns trigger as $$
|
||||
begin
|
||||
insert into profiles (id) values (new.id);
|
||||
return new;
|
||||
end;
|
||||
$$ language plpgsql security definer;
|
||||
|
||||
create trigger on_auth_user_created
|
||||
after insert on auth.users
|
||||
for each row execute function handle_new_user();
|
||||
25
supabase/migrations/002_categories.sql
Normal file
25
supabase/migrations/002_categories.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
-- Category type enum
|
||||
create type category_type as enum (
|
||||
'income', 'bill', 'variable_expense', 'debt', 'saving', 'investment'
|
||||
);
|
||||
|
||||
-- Categories table
|
||||
create table categories (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
user_id uuid not null references auth.users(id) on delete cascade,
|
||||
name text not null,
|
||||
type category_type not null,
|
||||
icon text,
|
||||
sort_order int not null default 0,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index categories_user_id_idx on categories(user_id);
|
||||
|
||||
alter table categories enable row level security;
|
||||
|
||||
create policy "Users can CRUD own categories"
|
||||
on categories for all
|
||||
using (user_id = auth.uid())
|
||||
with check (user_id = auth.uid());
|
||||
46
supabase/migrations/003_templates.sql
Normal file
46
supabase/migrations/003_templates.sql
Normal file
@@ -0,0 +1,46 @@
|
||||
-- Item tier enum
|
||||
create type item_tier as enum ('fixed', 'variable', 'one_off');
|
||||
|
||||
-- Templates table (one per user)
|
||||
create table templates (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
user_id uuid not null references auth.users(id) on delete cascade,
|
||||
name text not null default 'My Template',
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now(),
|
||||
unique(user_id)
|
||||
);
|
||||
|
||||
create index templates_user_id_idx on templates(user_id);
|
||||
|
||||
alter table templates enable row level security;
|
||||
|
||||
create policy "Users can CRUD own template"
|
||||
on templates for all
|
||||
using (user_id = auth.uid())
|
||||
with check (user_id = auth.uid());
|
||||
|
||||
-- Template items table
|
||||
create table template_items (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
template_id uuid not null references templates(id) on delete cascade,
|
||||
category_id uuid not null references categories(id) on delete restrict,
|
||||
item_tier item_tier not null check (item_tier in ('fixed', 'variable')),
|
||||
budgeted_amount numeric(12,2) not null default 0,
|
||||
sort_order int not null default 0,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index template_items_template_id_idx on template_items(template_id);
|
||||
|
||||
alter table template_items enable row level security;
|
||||
|
||||
create policy "Users can CRUD own template items"
|
||||
on template_items for all
|
||||
using (
|
||||
template_id in (select id from templates where user_id = auth.uid())
|
||||
)
|
||||
with check (
|
||||
template_id in (select id from templates where user_id = auth.uid())
|
||||
);
|
||||
46
supabase/migrations/004_budgets.sql
Normal file
46
supabase/migrations/004_budgets.sql
Normal file
@@ -0,0 +1,46 @@
|
||||
-- Budgets table
|
||||
create table budgets (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
user_id uuid not null references auth.users(id) on delete cascade,
|
||||
start_date date not null,
|
||||
end_date date not null,
|
||||
currency text not null default 'EUR',
|
||||
carryover_amount numeric(12,2) not null default 0,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index budgets_user_id_idx on budgets(user_id);
|
||||
|
||||
alter table budgets enable row level security;
|
||||
|
||||
create policy "Users can CRUD own budgets"
|
||||
on budgets for all
|
||||
using (user_id = auth.uid())
|
||||
with check (user_id = auth.uid());
|
||||
|
||||
-- Budget items table
|
||||
create table budget_items (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
budget_id uuid not null references budgets(id) on delete cascade,
|
||||
category_id uuid not null references categories(id) on delete restrict,
|
||||
budgeted_amount numeric(12,2) not null default 0,
|
||||
actual_amount numeric(12,2) not null default 0,
|
||||
item_tier item_tier not null default 'fixed',
|
||||
notes text,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index budget_items_budget_id_idx on budget_items(budget_id);
|
||||
|
||||
alter table budget_items enable row level security;
|
||||
|
||||
create policy "Users can CRUD own budget items"
|
||||
on budget_items for all
|
||||
using (
|
||||
budget_id in (select id from budgets where user_id = auth.uid())
|
||||
)
|
||||
with check (
|
||||
budget_id in (select id from budgets where user_id = auth.uid())
|
||||
);
|
||||
19
supabase/migrations/005_quick_add.sql
Normal file
19
supabase/migrations/005_quick_add.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
-- Quick-add items table
|
||||
create table quick_add_items (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
user_id uuid not null references auth.users(id) on delete cascade,
|
||||
name text not null,
|
||||
icon text,
|
||||
sort_order int not null default 0,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index quick_add_items_user_id_idx on quick_add_items(user_id);
|
||||
|
||||
alter table quick_add_items enable row level security;
|
||||
|
||||
create policy "Users can CRUD own quick-add items"
|
||||
on quick_add_items for all
|
||||
using (user_id = auth.uid())
|
||||
with check (user_id = auth.uid());
|
||||
Reference in New Issue
Block a user