Skip to main content

Entity relationship

The service has two Postgres databases, each with its own embedded migrations. The OLTP schema is created by five migrations in internal/adapters/outbound/postgres/migrations/ (0001_delivery_promise_schema, 0002_window_time_zone, 0003_idempotency_global_key, 0004_transit_actuals, 0005_listing_owner). The analytical schema is created by analytics/migrations/0001_promise_facts.up.sql and is covered in Analytical database. The rest of this page up to that section is the OLTP database. Aggregates are stored as a jsonb snapshot that Restore() validates on read, with columns beside it for the queries and constraints the use cases need. There are almost no foreign keys: relations between aggregates are by id, and the read models are independent copies of other contexts' facts.

Aggregates, outbox and inbox​

Source: internal/adapters/outbound/postgres/migrations/0001_delivery_promise_schema.up.sql, 0003_idempotency_global_key.up.sql, internal/adapters/outbound/postgres/ (repositories).

  • promises.offer_id is UNIQUE: an offer backs at most one promise. promises_order_group is a partial unique index on (order_id, group_no) for promises that are not RELEASED or CANCELLED, so one live promise per order group.
  • Partial indexes serve the sweeper and the reactive policies: promises_hold_expiry (PROVISIONAL by hold expiry), promises_at_risk, promises_pickup and promises_wave (CONFIRMED or AT_RISK by pickup departure and wave), a GIN index on leg_ids, delivery_offers_quoted_expiry, promise_books_open_due, outbox_events_unpublished (the relay's work list).
  • version implements optimistic concurrency: the repository updates the row only when the version is the next one; a lost race is ErrConcurrentUpdate, retried by the use case up to three times.
  • outbox_events.id (a bigserial) is the publication order; event_id is the stable CloudEvents id and is UNIQUE; attempts and last_error record relay failures; published_at IS NULL means not yet published.
  • idempotency_keys rows are inserted by the request's own transaction and their outcome written just before it commits; rows older than 24 hours are purged on the next keyed write (claimKey in internal/adapters/inbound/http/idempotency.go). Migration 0003_idempotency_global_key made the key global: it deleted all but the newest row of any key present on two routes and replaced the primary key (route, key) with key alone, so the same key on another operation is a 409 idempotency-key-reused. route stays as a column and holds METHOD /concrete/path for new rows; rows written before 0003 keep their route pattern.
  • 0002_window_time_zone changes no column: the window's TimeZone lives in the jsonb snapshot and the migration backfills old offers and promises from rm_areas and rm_nodes.
  • Each line of an offer and of a promise snapshot carries OwnerRef, the owner whose stock covers it ("" = operator-owned, ADR 0017; a line persisted before it existed reads as operator-owned). The hold queries group by (node_id, item, OwnerRef).

Local read models​

One jsonb row per entity, written by the consumer's projections (internal/application/usecases/projections.go). They are not related to the aggregates by foreign keys: the search joins them in memory.

Source: 0001_delivery_promise_schema.up.sql (lines 55 to 73), 0004_transit_actuals.up.sql, 0005_listing_owner.up.sql, 0006_promise_calibration.up.sql, 0007_node_tier_eligibility.up.sql, 0008_departure_arrival.up.sql, 0009_tracking_many_assignments.up.sql, internal/application/readmodel.

TableFed byUsed by
rm_nodes, rm_regions, rm_areas, rm_lanesnode-topology eventsthe search (candidate nodes, service area to delivery station, lane transit for NOMINAL), ListPromiseBooks (node time zone)
rm_items, rm_listingsoffer-availability eventsshipment grouping and availability per (node, item, owner): rm_listings is keyed by (item_id, node_id, owner_ref) (index rm_listings_node_item_owner, migration 0005) and a line is covered by ONE owner, never a sum (ADR 0017)
rm_departures, rm_waveslinehaul and last-mile eventspath search, MakePromise revalidation, cancellation risk
rm_scorecardsnode-performance eventsthe ScorecardHandicap
rm_node_tier_eligibility (node_id, tier, eligible, changed_at; PK (node_id, tier); migration 0007)node-performance NodeTierEligibilityChangedremoving ineligible nodes from SAME_DAY and NEXT_DAY candidates (ADR 0022); a missing row means eligible
rm_assignments, rm_shipments, rm_trackings, rm_outcomesorder-sourcing and shipping-and-tracking eventsfinding a promise's assignment and judging it. rm_trackings is keyed by (tracking_number, assignment_id) (migration 0009): a merged package is shared by several assignments and one delivery, loss or return judges every sharing promise (ADR 0025)
rm_load_transit_open, rm_transit_actualslinehaul LoadDeparted and LoadArrivedthe measured transit behind a MEASURED basis (ADR 0014)
rm_promise_calibrationthis service's own PromiseKept and late PromiseBroken judgementsthe calibration buffer and the confidence of a quote (ADR 0020)
rm_departure_arrivallinehaul LoadArrived that carries a departureIdthe late-risk sweep: a witness departure past its expected arrival with no row is flagged (ADR 0021); pruned past the transit window

Analytical database​

A second, separate database (ADR 0018) written only by cmd/dp-projector and read through a read-only pool by cmd/dp-reports. It is a projection of this context's own retailnetwork.delivery-promise.events and .analytics topics, not a source of truth, and can be dropped and rebuilt from them. It has no foreign keys and no link to the OLTP tables.

Source: analytics/migrations/0001_promise_facts.up.sql, internal/adapters/outbound/analyticsstore/projection.go.

  • analytics_processed_events is the inbox of this database: every applied CloudEvents id is recorded once, in the same transaction as its effect on a fact table (Apply claims the id with ON CONFLICT DO NOTHING and skips the fact when the id was already there). A failure leaves neither the id nor the fact behind.
  • Each fact table carries the event_id of the event that wrote the row. The tables of promises made, offers quoted and promise judgements are upserts keyed by their id (ON CONFLICT ... DO UPDATE); promise_revisions (keyed by promise_id, revision_no, from PromiseRevised) and promise_unkeepable (from PromiseUnkeepable) are insert-or-ignore.
  • promise_judgements takes the verdict from PromiseKept or PromiseBroken. A CHECK ties the two columns: cause is empty exactly when the verdict is KEPT.
  • The three derived columns of promises_made (window_minutes, exact_day, zone_known) are computed when the fact is applied, so the reports only count and sum them.
  • offers_quoted.region and promised_days are added by analytics/migrations/0002_offer_coverage (ADR 0030) and feed the promiseCoverage block of /reports/quote-conversion; only the two-character region is stored, never the full postal prefix.
  • Indexes: idx_offers_quoted_quoted_at, idx_promises_made_made_at, idx_promise_judgements_judged_at and idx_analytics_processed_events_occurred_at, because the reports filter each fact on its own instant.