railyard/apps/rago/crates/rago-db/migrations/0010_integrations_alerts.sql
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

60 lines
2.4 KiB
SQL

-- Phase 14: integration connections (OAuth lifecycle) + resource mappings,
-- and usage-monitoring alerts + their trigger history.
CREATE TABLE IF NOT EXISTS integration_connections (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
integration_type varchar NOT NULL, -- tax | accounting | crm
provider_code varchar NOT NULL, -- avalara | xero | salesforce | ...
name varchar,
access_token varchar,
refresh_token varchar,
token_expires_at timestamp(6),
settings jsonb DEFAULT '{}' NOT NULL,
created_at timestamp(6) NOT NULL,
updated_at timestamp(6) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_integration_connections_org
ON integration_connections (organization_id, provider_code);
CREATE TABLE IF NOT EXISTS integration_mappings (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
integration_connection_id uuid NOT NULL,
resource_type varchar NOT NULL, -- invoice | customer
lago_id uuid NOT NULL,
external_id varchar NOT NULL,
created_at timestamp(6) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_integration_mappings_resource
ON integration_mappings (organization_id, resource_type, lago_id);
CREATE TABLE IF NOT EXISTS usage_alerts (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
subscription_external_id varchar NOT NULL,
alert_type varchar NOT NULL, -- current_usage_amount | billable_metric_current_usage_amount
billable_metric_code varchar, -- NULL = whole-subscription amount
code varchar,
threshold_cents bigint NOT NULL,
recurring boolean DEFAULT false NOT NULL,
previous_value_cents bigint DEFAULT 0 NOT NULL,
last_triggered_at timestamp(6),
created_at timestamp(6) NOT NULL,
updated_at timestamp(6) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_usage_alerts_sub
ON usage_alerts (organization_id, subscription_external_id);
CREATE TABLE IF NOT EXISTS usage_alert_triggers (
id uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
organization_id uuid NOT NULL,
usage_alert_id uuid NOT NULL,
subscription_external_id varchar NOT NULL,
crossed_threshold_cents bigint NOT NULL,
current_value_cents bigint NOT NULL,
triggered_at timestamp(6) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_usage_alert_triggers_alert
ON usage_alert_triggers (usage_alert_id, triggered_at);