owpengram-server/deploy/migrations/0009_stars_ledger.up.sql

29 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Stars 本地账本per-user 余额 + 交易流水。此前 payments.getStarsStatus 余额恒 0、
-- getStarsTransactions 未注册、sendPaidReaction 恒返 BALANCE_TOO_LOW——无任何余额持久化。
-- 本地账本非真实支付起始余额走惰性首读授予granted 布尔幂等,新老账号都覆盖、免回填)。
-- 借记原子性由 store 层 withTx 保证SELECT ... FOR UPDATE + CHECK(balance>=0) + UPDATE + INSERT
CREATE TABLE public.stars_balances (
user_id bigint NOT NULL,
balance bigint DEFAULT 0 NOT NULL,
granted boolean DEFAULT false NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT stars_balances_pkey PRIMARY KEY (user_id),
CONSTRAINT stars_balances_balance_nonneg CHECK ((balance >= 0))
);
-- 流水amount 带符号(贷记 > 0 / 借记 < 0peer_* 为对手方grant/topup 等无对手时为空)。
CREATE TABLE public.stars_transactions (
id bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL,
user_id bigint NOT NULL,
peer_type text DEFAULT '' NOT NULL,
peer_id bigint DEFAULT 0 NOT NULL,
amount bigint NOT NULL,
reason text DEFAULT 'adjust' NOT NULL,
title text DEFAULT '' NOT NULL,
description text DEFAULT '' NOT NULL,
date integer DEFAULT 0 NOT NULL,
CONSTRAINT stars_transactions_pkey PRIMARY KEY (id)
);
-- keyset 分页WHERE user_id=$1 [AND id < cursor] ORDER BY id DESC LIMIT n。
CREATE INDEX stars_transactions_user_id_idx ON public.stars_transactions USING btree (user_id, id DESC);