caelum0x 03a3fe911d feat(rago): Lago-port billing service with idempotent payment/invoice guards
Lands the previously-untracked rago app: a Rust port of Lago billing with
the three audit fixes from the 2026-09-03 railyard audit baked in:

- CRITICAL: deterministic Stripe idempotency key (UUIDv5 of invoice id,
  sent as 'Idempotency-Key: payment-<id>') replacing the random-UUID-per-
  attempt pattern that allowed retry double-charges
- HIGH: unique index on events(organization_id, external_subscription_id,
  transaction_id) with idempotent ON CONFLICT DO NOTHING insert; duplicate
  transaction_id surfaces as an API validation error, not a 500
- HIGH: partial unique index on invoice_subscriptions(subscription_id,
  charges_from_datetime, charges_to_datetime) WHERE recurring, with
  insert_subscription_invoice_guarded returning Created|AlreadyBilled,
  wired into bill_subscription and the scheduler

Includes real-Postgres #[sqlx::test] suites (rago-db idempotency/parity,
rago-jobs rebill/clock, rago-graphql admin1-5, rago-api rest_crud).
2026-09-05 16:35:33 +03:00

46 lines
2.4 KiB
SQL

-- Clock-jobs / billing-automation support schema.
--
-- Added by the rago-jobs clock-parity work. Kept in a single, high-numbered,
-- purely-additive migration (all `IF NOT EXISTS` / `ADD COLUMN IF NOT EXISTS`)
-- so it never conflicts with in-flight rago-db/rago-billing migrations and can
-- be applied in any order relative to them.
-- Daily usage snapshots feeding analytics (Clock::ComputeAllDailyUsagesJob →
-- DailyUsages::ComputeAllService). One row per (subscription, usage day).
CREATE TABLE IF NOT EXISTS daily_usages (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
subscription_external_id varchar NOT NULL,
usage_date date NOT NULL,
amount_cents bigint DEFAULT 0 NOT NULL,
refreshed_at timestamp(6) NOT NULL,
created_at timestamp(6) NOT NULL,
UNIQUE (organization_id, subscription_external_id, usage_date)
);
CREATE INDEX IF NOT EXISTS index_daily_usages_on_org ON daily_usages (organization_id);
-- Inbound webhooks received from payment providers (Clock::InboundWebhooksCleanupJob
-- + Clock::InboundWebhooksRetryJob → InboundWebhooks::ProcessJob).
CREATE TABLE IF NOT EXISTS inbound_webhooks (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
source varchar NOT NULL,
event_type varchar,
payload jsonb DEFAULT '{}' NOT NULL,
status varchar DEFAULT 'pending' NOT NULL, -- pending | processing | succeeded | failed
processing_attempts integer DEFAULT 0 NOT NULL,
created_at timestamp(6) NOT NULL,
updated_at timestamp(6) NOT NULL
);
CREATE INDEX IF NOT EXISTS index_inbound_webhooks_on_status ON inbound_webhooks (status);
CREATE INDEX IF NOT EXISTS index_inbound_webhooks_on_updated_at ON inbound_webhooks (updated_at);
-- Wallet recurring top-up rules gain the scheduling + lifecycle columns the
-- interval-top-up and rule-termination clocks need
-- (Clock::CreateIntervalWalletTransactionsJob + Clock::TerminateRecurringTransactionRulesJob).
ALTER TABLE wallet_recurring_rules ADD COLUMN IF NOT EXISTS status integer DEFAULT 0 NOT NULL; -- 0 active, 1 terminated
ALTER TABLE wallet_recurring_rules ADD COLUMN IF NOT EXISTS started_at timestamp(6);
ALTER TABLE wallet_recurring_rules ADD COLUMN IF NOT EXISTS next_top_up_at timestamp(6);
ALTER TABLE wallet_recurring_rules ADD COLUMN IF NOT EXISTS expiration_at timestamp(6);
ALTER TABLE wallet_recurring_rules ADD COLUMN IF NOT EXISTS terminated_at timestamp(6);