-- +goose Up -- Money. The ledger is append-only: rows are inserted, never updated or -- deleted. wallets.balance is a cached sum that is updated in the same -- transaction as each ledger row and can be checked against the ledger. CREATE TABLE wallets ( id TEXT PRIMARY KEY, owner_type TEXT NOT NULL, owner_id BIGINT NOT NULL, balance BIGINT NOT NULL DEFAULT 0, created_at BIGINT NOT NULL, UNIQUE (owner_type, owner_id) ); CREATE TABLE ledger ( id TEXT PRIMARY KEY, wallet_id TEXT NOT NULL REFERENCES wallets (id), kind TEXT NOT NULL, amount BIGINT NOT NULL, balance_after BIGINT NOT NULL, reason TEXT NOT NULL, ref TEXT NOT NULL DEFAULT '', dedupe_key TEXT NOT NULL UNIQUE, actor_id BIGINT NOT NULL DEFAULT 0, created_at BIGINT NOT NULL ); CREATE INDEX ledger_wallet_idx ON ledger (wallet_id, created_at); CREATE TABLE passes ( id TEXT PRIMARY KEY, wallet_id TEXT NOT NULL REFERENCES wallets (id), kind TEXT NOT NULL, starts_at BIGINT NOT NULL, expires_at BIGINT NOT NULL, source TEXT NOT NULL DEFAULT '', created_at BIGINT NOT NULL ); CREATE INDEX passes_wallet_idx ON passes (wallet_id, expires_at); -- A purchase is one invoice. payment_hash maps the Lightning payment back -- to the purchase and through it to the wallet that gets credited. CREATE TABLE purchases ( id TEXT PRIMARY KEY, user_id BIGINT NOT NULL, wallet_id TEXT NOT NULL REFERENCES wallets (id), product TEXT NOT NULL, amount_sats BIGINT NOT NULL, status TEXT NOT NULL, payment_hash TEXT NOT NULL UNIQUE, bolt11 TEXT NOT NULL, chat_id BIGINT NOT NULL DEFAULT 0, message_id BIGINT NOT NULL DEFAULT 0, message_is_photo BOOLEAN NOT NULL DEFAULT FALSE, lang TEXT NOT NULL DEFAULT 'en', request_id TEXT NOT NULL DEFAULT '', profile TEXT NOT NULL DEFAULT '', delivery_id TEXT NOT NULL DEFAULT '', fulfilled BOOLEAN NOT NULL DEFAULT FALSE, created_at BIGINT NOT NULL, expires_at BIGINT NOT NULL, paid_at BIGINT NOT NULL DEFAULT 0 ); CREATE INDEX purchases_status_idx ON purchases (status, created_at); CREATE INDEX purchases_user_idx ON purchases (user_id, status); CREATE TABLE usage_daily ( user_id BIGINT NOT NULL, day INTEGER NOT NULL, downloads INTEGER NOT NULL DEFAULT 0, bytes BIGINT NOT NULL DEFAULT 0, PRIMARY KEY (user_id, day) ); CREATE TABLE gift_codes ( code TEXT PRIMARY KEY, kind TEXT NOT NULL, amount BIGINT NOT NULL, max_uses INTEGER NOT NULL DEFAULT 1, uses INTEGER NOT NULL DEFAULT 0, expires_at BIGINT NOT NULL DEFAULT 0, created_by BIGINT NOT NULL DEFAULT 0, created_at BIGINT NOT NULL ); CREATE TABLE gift_redemptions ( code TEXT NOT NULL REFERENCES gift_codes (code), user_id BIGINT NOT NULL, created_at BIGINT NOT NULL, PRIMARY KEY (code, user_id) ); ALTER TABLE users ADD COLUMN referred_by BIGINT NOT NULL DEFAULT 0; -- What paid for a delivery, so a failed download can be refunded exactly. ALTER TABLE deliveries ADD COLUMN source TEXT NOT NULL DEFAULT 'free'; ALTER TABLE deliveries ADD COLUMN wallet_id TEXT NOT NULL DEFAULT ''; ALTER TABLE deliveries ADD COLUMN charge_sats BIGINT NOT NULL DEFAULT 0; ALTER TABLE deliveries ADD COLUMN quota_day INTEGER NOT NULL DEFAULT 0; ALTER TABLE deliveries ADD COLUMN released BOOLEAN NOT NULL DEFAULT FALSE; -- +goose Down ALTER TABLE deliveries DROP COLUMN released; ALTER TABLE deliveries DROP COLUMN quota_day; ALTER TABLE deliveries DROP COLUMN charge_sats; ALTER TABLE deliveries DROP COLUMN wallet_id; ALTER TABLE deliveries DROP COLUMN source; ALTER TABLE users DROP COLUMN referred_by; DROP TABLE gift_redemptions; DROP TABLE gift_codes; DROP TABLE usage_daily; DROP TABLE purchases; DROP TABLE passes; DROP TABLE ledger; DROP TABLE wallets;