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_idisUNIQUE: an offer backs at most one promise.promises_order_groupis a partial unique index on(order_id, group_no)for promises that are notRELEASEDorCANCELLED, 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_pickupandpromises_wave(CONFIRMED or AT_RISK by pickup departure and wave), a GIN index onleg_ids,delivery_offers_quoted_expiry,promise_books_open_due,outbox_events_unpublished(the relay's work list). versionimplements optimistic concurrency: the repository updates the row only when the version is the next one; a lost race isErrConcurrentUpdate, retried by the use case up to three times.outbox_events.id(abigserial) is the publication order;event_idis the stable CloudEvents id and isUNIQUE;attemptsandlast_errorrecord relay failures;published_at IS NULLmeans not yet published.idempotency_keysrows 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 (claimKeyininternal/adapters/inbound/http/idempotency.go). Migration0003_idempotency_global_keymade the key global: it deleted all but the newest row of any key present on two routes and replaced the primary key(route, key)withkeyalone, so the same key on another operation is a 409idempotency-key-reused.routestays as a column and holdsMETHOD /concrete/pathfor new rows; rows written before0003keep their route pattern.0002_window_time_zonechanges no column: the window'sTimeZonelives in the jsonb snapshot and the migration backfills old offers and promises fromrm_areasandrm_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.
| Table | Fed by | Used by |
|---|---|---|
rm_nodes, rm_regions, rm_areas, rm_lanes | node-topology events | the search (candidate nodes, service area to delivery station, lane transit for NOMINAL), ListPromiseBooks (node time zone) |
rm_items, rm_listings | offer-availability events | shipment 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_waves | linehaul and last-mile events | path search, MakePromise revalidation, cancellation risk |
rm_scorecards | node-performance events | the ScorecardHandicap |
rm_node_tier_eligibility (node_id, tier, eligible, changed_at; PK (node_id, tier); migration 0007) | node-performance NodeTierEligibilityChanged | removing ineligible nodes from SAME_DAY and NEXT_DAY candidates (ADR 0022); a missing row means eligible |
rm_assignments, rm_shipments, rm_trackings, rm_outcomes | order-sourcing and shipping-and-tracking events | finding 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_actuals | linehaul LoadDeparted and LoadArrived | the measured transit behind a MEASURED basis (ADR 0014) |
rm_promise_calibration | this service's own PromiseKept and late PromiseBroken judgements | the calibration buffer and the confidence of a quote (ADR 0020) |
rm_departure_arrival | linehaul LoadArrived that carries a departureId | the 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_eventsis the inbox of this database: every applied CloudEvents id is recorded once, in the same transaction as its effect on a fact table (Applyclaims the id withON CONFLICT DO NOTHINGand 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_idof 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 bypromise_id, revision_no, fromPromiseRevised) andpromise_unkeepable(fromPromiseUnkeepable) are insert-or-ignore. promise_judgementstakes the verdict fromPromiseKeptorPromiseBroken. A CHECK ties the two columns:causeis empty exactly when the verdict isKEPT.- 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.regionandpromised_daysare added byanalytics/migrations/0002_offer_coverage(ADR 0030) and feed thepromiseCoverageblock 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_atandidx_analytics_processed_events_occurred_at, because the reports filter each fact on its own instant.