本月收入
¥0.00
本月支出
¥0.00
本月结余
¥0.00
还没有交易记录
-- 启用 RLS
alter database postgres set "Timezone" to 'UTC';
create table if not exists profiles (
id uuid references auth.users primary key,
username text unique,
created_at timestamptz default now()
);
create table if not exists transactions (
id text primary key,
user_id uuid references auth.users(id),
type text check (type in ('income','expense')),
amount numeric,
category text,
date text,
merchant text,
note text,
created_at timestamptz default now()
);
create table if not exists cards (
id text primary key,
user_id uuid references auth.users(id),
name text, type text, number text, bank text,
color text, balance numeric, credit_limit numeric,
arrears numeric, due_date int, statement_date int,
created_at timestamptz default now()
);
create table if not exists budgets (
id text primary key,
user_id uuid references auth.users(id),
name text, category text, amount numeric,
created_at timestamptz default now()
);
create table if not exists categories (
id text primary key,
user_id uuid references auth.users(id),
type text, name text, icon text, color text,
is_default boolean default false,
created_at timestamptz default now()
);
-- RLS
alter table transactions enable row level security;
alter table cards enable row level security;
alter table budgets enable row level security;
alter table categories enable row level security;
create policy "own_transactions" on transactions for all using (auth.uid() = user_id);
create policy "own_cards" on cards for all using (auth.uid() = user_id);
create policy "own_budgets" on budgets for all using (auth.uid() = user_id);
create policy "own_categories" on categories for all using (auth.uid() = user_id);
create policy "own_profiles" on profiles for all using (auth.uid() = id);
-- 自动创建 profile
create or replace function handle_new_user() returns trigger as $$
begin
insert into profiles (id, username)
values (new.id, new.raw_user_meta_data->>'username');
return new;
end;
$$ language plpgsql;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure handle_new_user();