Tearleads

organization_billing_stripe_seats API Postgres table

Durable Stripe binding and coalescing seat-sync outbox for one organization.

Declared in packages/api-shared/src/schema/organizationBillingSeats.ts

Description

Roster, binding, and plan changes write one absolute desired state here, and the seat-sync worker claims the row to bring Stripe to it. Seat quantities are tier seat limits: each Stripe Price is one fixed tier, sold at quantity one. An in-flight capacity target is sticky across retries so the same Stripe idempotency key is replayed after a crash.

Columns

  • iduuid
    • primary key
    • not null
    • default gen_random_uuid()

    Row id.

  • organization_iduuid
    • not null

    Organization the row syncs; one row each.

  • customer_idtext
    • nullable

    Stripe customer of the bound subscription.

  • subscription_idtext
    • nullable

    The bound Stripe subscription and the item whose Price is the plan; null until a subscription binds. Each binds at most one organization.

  • subscription_item_idtext
    • nullable

    The bound Stripe subscription and the item whose Price is the plan; null until a subscription binds. Each binds at most one organization.

  • price_idtext
    • nullable

    Stripe Price the bound item holds. The worker claims a row only while it and both binding ids are set; a native store subscription that takes over while the binding is kept sets it to null.

  • desired_paid_capacityinteger
    • not null
    • default 0

    The current period's licensed high-water mark: the seats Stripe should be paid for, which the worker prorates up to.

  • desired_renewal_quantityinteger
    • not null
    • default 0

    Seats of the plan the subscription renews at, as an administrator chose it or seeded from the licensed seat count. The worker writes it to Stripe without proration, never above the capacity paid for.

  • applied_paid_capacityinteger
    • not null
    • default 0

    Seats Stripe has been paid for in the bound period; it only rises within a period.

  • observed_quantityinteger
    • nullable

    Seats of the tier Stripe's item held when the row was last bound, synced, or changed; null until a subscription binds.

  • desired_seat_period_keytext
    • nullable

    The period the desired state targets and the period last applied to Stripe, as trial:<end> or paid:<start>:<end> with ISO-8601 times. While they differ, a claim starts a capacity operation.

  • applied_seat_period_keytext
    • nullable

    The period the desired state targets and the period last applied to Stripe, as trial:<end> or paid:<start>:<end> with ISO-8601 times. While they differ, a claim starts a capacity operation.

  • billing_period_starts_attimestamp
    • nullable

    The bound subscription's paid period as Stripe last reported it.

  • billing_period_ends_attimestamp
    • nullable

    The bound subscription's paid period as Stripe last reported it.

  • desired_revisioninteger
    • not null
    • default 0

    A counter every change to the desired state increments, and the revision a sync last applied in full. They differ while work is pending.

  • applied_revisioninteger
    • not null
    • default 0

    A counter every change to the desired state increments, and the revision a sync last applied in full. They differ while work is pending.

  • in_flight_operation_idtext
    • nullable

    A capacity increase the worker started: the id its Stripe idempotency key is built from, and the capacity it targets. Null when none is in flight.

  • in_flight_target_capacityinteger
    • nullable

    A capacity increase the worker started: the id its Stripe idempotency key is built from, and the capacity it targets. Null when none is in flight.

  • next_attempt_attimestamp
    • nullable

    When the row is next due for the worker. A change to the desired state makes it due at once, a clean sync a day later, a failed sync after a backoff of at most an hour, and a plan change's lease only once it has settled. Null while seat sync is paused.

  • attempt_countinteger
    • not null
    • default 0

    Consecutive failed syncs, which set the backoff; a successful sync resets it.

  • lease_idtext
    • nullable

    The claim holding the row and when it lapses: a worker claim's UUID, or a plan: id for an administrator's plan change, which the worker takes over only a settling time after it lapses.

  • lease_expires_attimestamp
    • nullable

    The claim holding the row and when it lapses: a worker claim's UUID, or a plan: id for an administrator's plan change, which the worker takes over only a settling time after it lapses.

  • last_errortext
    • nullable

    The last failed sync's redacted error, at most 1000 characters; a successful sync clears it.

  • last_synced_attimestamp
    • nullable

    When a sync last completed.

  • last_invoice_idtext
    • nullable

    Stripe invoice of the last renewal recorded for the bound subscription; cleared when a different subscription binds.

  • created_attimestamp
    • not null
    • default now()

    Server-side row timestamps.

  • updated_attimestamp
    • not null
    • default now()

    Server-side row timestamps.

Indexes

  • organization_billing_stripe_seats_org_idx(organization_id)
    • unique
  • organization_billing_stripe_seats_subscription_idx(subscription_id)
    • unique
  • organization_billing_stripe_seats_item_idx(subscription_item_id)
    • unique
  • organization_billing_stripe_seats_due_idx(next_attempt_at, lease_expires_at, organization_id)
    • partial: where "subscription_id" is not null and "subscription_item_id" is not null and "price_id" is not null

    Only a bound row can be claimed; an unbound row stays due forever, so indexing it would make every claim and probe walk unbound rows. The claim and ops-health reads repeat these three terms literally.