Entity relationship
The schema of this context's own Postgres database ("every context owns its database; no shared tables"), as created by
the embedded migrations in internal/adapters/outbound/postgres/migrations/, applied by golang-migrate at every boot:
0001_order_sourcing_schema creates all 14 tables; 0002_idempotency_global_key, 0003_rm_lane_wave_cost and
0004_rm_listings_owner_ref only alter existing tables (none creates or drops a table). There are no foreign keys: the aggregate
is one row with jsonb columns, the read models are projections that can be dropped and replayed, and every id of another
context is a plain value.
A second, separate analytical database (ADR 0013) has its own migration, analytics/migrations/0001_sourcing_facts.up.sql,
with 4 tables; it is described in Analytical database at the end of this page.
FulfillmentAssignment (one table)
Source: the assignments table of the migration and internal/domain/assignment/snapshot.go (Snapshot).
Column notes:
(order_id, group_no)is one unique key (assignments_order_group_key): one assignment per shipment group, and the reason a repeatedCustomerOrderPlacedis a no-op. A violation isports.ErrDuplicateGroup.version(CHECK at least 1) is the optimistic lock: every save expects the stored version to be the aggregate's version minus one (ports.ErrConcurrentUpdateotherwise). It is theassignmentVersionof the API.lines,candidate,releases,excluded_nodes,tracking_numbersanddeliveredare JSON documents that belong to the aggregate: lines, the current candidate (node, pickup departure and cutoff, legs, wave, eta, cost), the release history with each outcome, the exclusion list, the shipment's tracking numbers and the delivered packages. There are no child tables.node_idis the node of the candidate whileQUEUEDand of the latest release afterwards; it is whatGET /assignments?nodeId=filters on.stateis not constrained by a CHECK; the domain validates it.destination_postal_prefixis a postal prefix, not an address: no PII is stored here.- Indexes:
assignments_promise_idx (promise_id)forFindByPromise;assignments_state_node_idx (state, node_id)for the list filters;assignments_queue_idx (release_at, assignment_id) WHERE state = 'QUEUED'for the release timer and the re-optimisation sweep.
Read-model tables
Fed only by sibling events, rebuilt by replaying their topics (an older instant or a lower revision never overwrites a newer one). The five views map to ten tables.
Source: the rm_* tables of 0001_order_sourcing_schema.up.sql, plus the columns added later: cost_per_unit_cents and cost_declared_at on rm_lanes and cost_per_stop_cents and cost_declared_at on rm_waves (0003_rm_lane_wave_cost.up.sql; cost_declared_at NULL means no declaration was seen, so the component contributes 0 to a cost estimate and marks it incomplete) and owner_ref on rm_listings (0004_rm_listings_owner_ref.up.sql; '' is operator-owned, and the unique index rm_listings_item_node_owner_key on (catalog_item_id, node_id, owner_ref) WHERE catalog_item_id <> '' keys the availability view by item, node and owner while listing_id stays the primary key). The projection rules are in internal/domain/network/network.go and
internal/application/usecases/projector.go.
| View | Tables | Event source |
|---|---|---|
NodeProfileView | rm_nodes | node-topology node events |
NetworkGraphView | rm_lanes, rm_departures, rm_waves, rm_service_areas, rm_regions | node-topology (lanes, service areas, regions), linehaul-transportation (departures), last-mile-delivery (waves) |
AvailabilityView | rm_listings, rm_discontinued_items | offer-availability |
ScorecardView | rm_scorecards (score_window stands for the scorecard's window, a reserved word) | node-performance; only the 30d window is used by the cost |
PromiseBookPressureView | rm_book_pressure | delivery-promise PromiseBookExhausted |
remaining_known marks that remaining holds a real value: the schedule's capacity or a capacity event, and a later schedule
keeps the remaining value a capacity event already set. A row is usable only if scheduled, not cancelled and remaining is
above 0; a cancellation that arrives before the schedule is kept.
Infrastructure tables
Source: 0001_order_sourcing_schema.up.sql, with idempotency_keys as reshaped by 0002_idempotency_global_key.up.sql. All three are infrastructure, not domain tables:
outbox_eventsis written in the same transaction as the aggregate and drained by the relay.valueis the already-encoded CloudEvents message,event_idits stable CloudEventsid; a partial index serves the unpublished rows byid.attemptsandlast_errorsupport the attempt cap (Runbook).processed_eventsis the inbox: one row per consumed CloudEventsid, written in the same transaction as its effect.idempotency_keysstores the first answer ofPOST /assignments/{id}/reoptimisationsperkey(global), with the request fingerprint (method + path + SHA-256 of the body), ignored after 24 hours. Migration0002moved the key from(route, key)tokeyalone.
golang-migrate also keeps its own bookkeeping table (schema_migrations), created by the tool and not by these migrations.
No foreign key across aggregates or contexts
node_id,witness_node_id,promise_id,order_id,catalog_item_id,listing_idandpickup_departure_idname entities of other contexts, which live in other databases, so they are values. A node retired innode-topologymust not block a write here.- The
rm_tables are not joined to the aggregate by constraints either: a constraint would make a replay order-dependent. - There is only one aggregate, so no cross-aggregate key exists.
Table is not aggregate
| Table | Role |
|---|---|
assignments | FulfillmentAssignment root, with its lines, release history and exclusions embedded as JSON |
rm_nodes, rm_lanes, rm_departures, rm_waves, rm_service_areas, rm_regions, rm_listings, rm_discontinued_items, rm_scorecards, rm_book_pressure | read models; no invariant of this context |
outbox_events, processed_events, idempotency_keys | infrastructure |
Analytical database
The analytical database is separate from the OLTP one, written only by cmd/os-projector and read (read-only) by cmd/os-reports (ADR 0013). It is a projection of this context's own two topics (retailnetwork.order-sourcing.events and .analytics), not a source of truth, and can be dropped and rebuilt from them. It has no foreign keys; the dotted lines are shared assignment_id values, not constraints.
Source: analytics/migrations/0001_sourcing_facts.up.sql; the projection is internal/adapters/outbound/analyticsstore/projection.go and the reports are internal/adapters/outbound/analyticsstore/reader.go.
queued_groups: one row per assignment, fromFulfillmentQueued(analytics topic);idx_queued_groups_orderonorder_idserves the order-split report.sourcing_decisions: one row per release, fromFulfillmentSourcingDecided(analytics topic), keyed(assignment_id, release_no); the checksourcing_decisions_cost_completemakescost_completeequalestimated_cost_cents IS NOT NULL(ADR 0011); indexidx_sourcing_decisions_decided_at.unsourceable_declarations: one row perFulfillmentUnsourceable(events topic), on its CloudEvents time; indexidx_unsourceable_declarations_declared_at.analytics_processed_events: the projector's inbox. Every applied CloudEvents id is recorded once, in the same transaction as its effect on a fact table (projection.goclaims the id, then projects);occurred_atis the basis of/reports/freshness; indexidx_analytics_processed_events_occurred_at.
Down migration: 0001_sourcing_facts.down.sql in the same directory.