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

26 lines
1.0 KiB
SQL

-- Activity + audit logs (Lago writes these to ClickHouse; here in Postgres).
CREATE TABLE IF NOT EXISTS activity_logs (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
activity_type varchar NOT NULL,
activity_source varchar DEFAULT 'system' NOT NULL,
resource_type varchar,
resource_id uuid,
external_customer_id varchar,
external_subscription_id varchar,
payload jsonb DEFAULT '{}' NOT NULL,
created_at timestamp(6) DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_activity_logs_org ON activity_logs (organization_id, created_at);
CREATE TABLE IF NOT EXISTS audit_logs (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
resource_type varchar NOT NULL,
resource_id uuid NOT NULL,
action varchar NOT NULL,
changes jsonb DEFAULT '{}' NOT NULL,
created_at timestamp(6) DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_audit_logs_org ON audit_logs (organization_id, created_at);