Skip to main content

Entity Relationship

The schema is 0001_checkout_schema.up.sql plus 0002_idempotency_key_global.up.sql (the Idempotency-Key alone becomes the primary key of idempotency_keys), applied at boot by cmd/api with golang-migrate (which adds its own schema_migrations table; MIGRATIONS_DATABASE_URL overrides the connection used for it). Checkout owns its database and shares no table with another context.

Three tables. Key markers: PK primary key, UK unique. Types are the column types of the migration; constraints (CHECK, defaults, partial indexes) are in the file. There is no foreign key: the aggregate is one row, and the infrastructure tables are keyed by their own ids.

Source: internal/adapters/outbound/postgres/migrations/0001_checkout_schema.up.sql and 0002_idempotency_key_global.up.sql.

Tables​

TableColumnsPrimary keyRole
checkout_sessions31session_idthe CheckoutSession aggregate, one row per session
idempotency_keys6keystored REST answers per Idempotency-Key (global to the service; the row keeps the request fingerprint)
outbox_events13idtransactional outbox the relay publishes to Kafka

How the aggregate maps onto checkout_sessions​

The CheckoutSession aggregate is one row, saved conditional on version (optimistic concurrency, ErrConcurrentUpdate on a mismatch). The collections that belong to the aggregate are jsonb columns, not child tables: lines, groups and requoted_offers (session_json.go). The stored placement request is the three columns placement_body, placement_hash and placing_since; the owed void is void_authorization_id; the PSP token is pending_token, set only while the state is AUTHORIZING. placement_attempts, next_attempt_at and alerted_at drive ResolveInDoubtPlacements. The migration adds CHECK constraints on state (the eight values), currency (3 characters), total_minor (not negative), cart_version and version (at least 1) and auth_attempt (not negative); the invariants proper are enforced by the aggregate in internal/domain before a row is written.

Indexes​

Each policy scan has a partial index, so a sweep reads only the rows it needs.

IndexColumnsPredicateUsed by
checkout_sessions_authorizing_idxauthorizing_sincestate = 'AUTHORIZING'ResolveStuckAuthorizations
checkout_sessions_placing_idxnext_attempt_atstate = 'PLACING'ResolveInDoubtPlacements
checkout_sessions_expiry_idxquote_expires_atstate IN ('OPEN','DECLINED')ExpireOnQuoteExpiry
checkout_sessions_void_idxupdated_atvoid_authorization_id IS NOT NULLRetryOwedVoids
checkout_sessions_purge_idxterminal_atplacement_body IS NOT NULL AND terminal_at IS NOT NULLPurgeStoredPlacements
idempotency_keys_created_idxcreated_atnoneexpiry of stored answers
outbox_events_unpublished_idxidpublished_at IS NULLthe outbox relay

The down migration drops the three tables (0001_checkout_schema.down.sql).