predikt/oracle/backend/supabase/user_portfolio_history.sql
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

64 lines
2.5 KiB
PL/PgSQL

-- This file is autogenerated from regen-schema.ts
create table if not exists
user_portfolio_history (
balance numeric,
cash_balance numeric default 0 not null,
cash_investment_value numeric default 0 not null,
id bigint primary key generated always as identity not null,
investment_value numeric,
loan_total numeric,
profit numeric,
spice_balance numeric default 0 not null,
total_cash_deposits numeric default 0 not null,
total_deposits numeric,
ts timestamp without time zone,
user_id text not null
);
-- Triggers
create trigger user_portfolio_history_insert
after insert on public.user_portfolio_history for each row
execute function update_user_portfolio_history_latest ();
-- Functions
create
or replace function public.update_user_portfolio_history_latest () returns trigger language plpgsql as $function$
begin
insert into user_portfolio_history_latest (user_id, ts, investment_value, cash_investment_value, balance, total_deposits, total_cash_deposits, cash_balance, spice_balance, loan_total, profit, last_calculated)
values (new.user_id, new.ts, new.investment_value, new.cash_investment_value, new.balance, new.total_deposits, new.total_cash_deposits, new.cash_balance, new.spice_balance, new.loan_total, new.profit, new.ts)
on conflict (user_id) do update
set ts = excluded.ts,
investment_value = excluded.investment_value,
cash_investment_value = excluded.cash_investment_value,
total_deposits = excluded.total_deposits,
total_cash_deposits = excluded.total_cash_deposits,
balance = excluded.balance,
cash_balance = excluded.cash_balance,
spice_balance = excluded.spice_balance,
loan_total = excluded.loan_total,
profit = excluded.profit,
last_calculated = excluded.ts
where user_portfolio_history_latest.ts < excluded.ts;
return new;
end;
$function$;
-- Row Level Security
alter table user_portfolio_history enable row level security;
-- Policies
drop policy if exists "public read" on user_portfolio_history;
create policy "public read" on user_portfolio_history for
select
using (true);
-- Indexes
drop index if exists user_portfolio_history_pkey;
create unique index user_portfolio_history_pkey on public.user_portfolio_history using btree (id);
drop index if exists user_portfolio_history_user_ts;
create index user_portfolio_history_user_ts on public.user_portfolio_history using btree (user_id, ts desc);