owpengram-server/deploy/migrations/20260714003057_star_gift_peer_stars_ledger.up.sql
2026-07-21 00:18:12 +03:00

40 lines
1.7 KiB
SQL

-- Owner-scoped Stars revenue for channel Star Gifts. These are internal
-- telesrv ledgers only; no blockchain, wallet, TON node or Fragment endpoint is
-- contacted. Conversion is recorded as one aggregate terminal transition.
CREATE TABLE public.channel_stars_balances (
channel_id bigint PRIMARY KEY,
balance bigint DEFAULT 0 NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT channel_stars_balances_shape_check CHECK (channel_id>0 AND balance>=0)
);
CREATE TABLE public.channel_stars_transactions (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
channel_id bigint NOT NULL,
actor_user_id bigint NOT NULL,
amount bigint NOT NULL,
reason text NOT NULL,
peer_type text DEFAULT '' NOT NULL,
peer_id bigint DEFAULT 0 NOT NULL,
gift_id bigint,
date integer NOT NULL,
CONSTRAINT channel_stars_transactions_shape_check CHECK (
channel_id>0 AND actor_user_id>0 AND amount<>0 AND date>0 AND
((peer_type='' AND peer_id=0) OR (peer_type IN ('user','channel') AND peer_id>0)))
);
CREATE INDEX channel_stars_transactions_channel_idx
ON public.channel_stars_transactions(channel_id,id DESC);
CREATE TABLE public.star_gift_conversions (
saved_gift_id bigint PRIMARY KEY REFERENCES public.peer_star_gifts(id) ON DELETE RESTRICT,
actor_user_id bigint NOT NULL,
owner_peer_type text NOT NULL,
owner_peer_id bigint NOT NULL,
amount bigint NOT NULL,
balance_after bigint NOT NULL,
converted_at integer NOT NULL,
CONSTRAINT star_gift_conversions_shape_check CHECK (
actor_user_id>0 AND owner_peer_type IN ('user','channel') AND owner_peer_id>0 AND
amount>=0 AND balance_after>=0 AND converted_at>0)
);