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).
21 lines
1.2 KiB
SQL
21 lines
1.2 KiB
SQL
-- Idempotency / dedup guards (money-correctness).
|
|
--
|
|
-- These indexes make three billing operations idempotent under scheduler/job
|
|
-- re-runs and concurrent identical requests, mirroring Lago's own constraints.
|
|
|
|
-- 1. Usage-event dedup — the DB is the single source of truth. Faithful to Lago's
|
|
-- `index_unique_transaction_id` (getlago/lago db/structure.sql): an event is
|
|
-- unique per (organization, external_subscription, transaction). This turns the
|
|
-- former racy check-then-insert into an atomic `ON CONFLICT DO NOTHING`.
|
|
CREATE UNIQUE INDEX IF NOT EXISTS index_unique_transaction_id
|
|
ON events (organization_id, external_subscription_id, transaction_id);
|
|
|
|
-- 2. Re-bill guard — at most one recurring invoice per (subscription, charges
|
|
-- period). Faithful to Lago's
|
|
-- `index_uniq_invoice_subscriptions_on_charges_from_to_datetime`. A scheduler
|
|
-- re-run for the same subscription+period can no longer create a second
|
|
-- invoice (which, with the payment path, would double-charge).
|
|
CREATE UNIQUE INDEX IF NOT EXISTS index_uniq_invoice_subscriptions_on_charges_from_to_datetime
|
|
ON invoice_subscriptions (subscription_id, charges_from_datetime, charges_to_datetime)
|
|
WHERE recurring IS TRUE;
|