Entity-relationship diagram
Two databases, each with its own embedded golang-migrate migrations and its own diagrams below:
- OLTP (written by
cmd/api):internal/adapters/outbound/postgres/migrations/holds three migrations,0001_last_mile_delivery_schema(creates all 15 tables),0002_idempotency_keys_global(reshapesidempotency_keys) and0003_wave_stop_cost(addsdelivery_waves.cost_per_stop_cents); neither of the later two creates or drops a table. They are applied at boot by golang-migrate. - Analytics (written only by
cmd/lm-projector, read bycmd/lm-reports):analytics/migrations/0001_last_mile_facts.up.sqlcreates 9 tables, described in Analytics database.
The OLTP schema has one table per aggregate root, child tables for the collections it owns, four rm_ read-model tables and three infrastructure tables. No foreign key crosses an aggregate or a context: ids of other aggregates are carried by value, so the diagrams below show the real foreign keys only inside an aggregate.
Aggregates
Source: 0001_last_mile_delivery_schema.up.sql (delivery_waves, wave_inductions, routes, route_stops, route_stop_packages, route_attempts) plus 0004_attempt_outcome_detail.up.sql (route_attempts.outcome_detail, nullable, with the constraint route_attempts_detail_check: a detail only in the set of its outcome; ADR 0014) plus 0003_wave_stop_cost.up.sql (cost_per_stop_cents, with the constraint delivery_waves_cost_check: BETWEEN 0 AND 10000000; rows stored before the migration keep 0, which reads as "no declaration").
Key constraints: delivery_waves_times_check (induct_cutoff_at < window_start < window_end), delivery_waves_capacity_check (assigned_stops <= stop_capacity), delivery_waves_cancel_reason_check (CANCELLED exactly when a reason exists); indexes idx_delivery_waves_station_date and the partial idx_delivery_waves_open_cutoff on OPEN waves (the induction scan); idx_routes_wave; route_attempts_reason_check (a failure_reason exactly when FAILED); the partial unique index idx_route_attempts_delivered_once on tracking_number WHERE outcome = 'DELIVERED' (a package is delivered at most once, across routes).
Partners and package histories
Source: same migration. idx_package_histories_waiting is a partial index on (station_node_id, arrived_at, tracking_number) WHERE status = 'AT_STATION': the sweep that a newly scheduled wave runs.
Read models (event-fed)
Source: same migration. Fed by NodeRegistered (rm_stations), ServiceAreaAssigned (rm_station_service_areas), LabelIssued (rm_package_stops) and LoadArrived (rm_inbound_packages). No aggregate points at them.
Infrastructure
Source: 0001_last_mile_delivery_schema.up.sql; idempotency_keys as reshaped by 0002_idempotency_keys_global.up.sql (it deleted the old rows, dropped the route column and the old primary key, added content_type and made idempotency_key the primary key).
outbox_eventsis the transactional outbox.event_idis the CloudEvents id minted at encode time, so a relay retry republishes the same id;(event_id, topic)is unique; the partial indexidx_outbox_events_unpublishedonid WHERE published_at IS NULLserves the relay's claim;attemptsandlast_errorcarry the failure count the attempt cap reads.processed_eventsis the inbox: the consumer claims the CloudEvents id withINSERT ... ON CONFLICT DO NOTHINGin the same transaction as the effect, so a rollback un-claims it.idempotency_keysholds theIdempotency-Keymiddleware's stored 2xx and 4xx answers (status, content type, body), keyed by the key alone (global; the request hash decides replay or 409); rows older than 24 hours are ignored by the lookup and deleted by the retention loop ofcmd/api(hourly, 1000 rows per statement;idempotency.Store.PurgeExpired).
Down migrations: 0001_last_mile_delivery_schema.down.sql, 0002_idempotency_keys_global.down.sql and 0003_wave_stop_cost.down.sql in the same directory.
Analytics database
The analytical database is separate from the OLTP one and is a projection of this context's own two topics (retailnetwork.last-mile-delivery.events and .analytics), not a source of truth: it can be dropped and rebuilt from them. analytics/migrations/0001_last_mile_facts.up.sql creates nine tables. There are no foreign keys; the lines below are the joins the reports in internal/adapters/outbound/analyticsstore/reader.go make, by value. The wave_inductions table here is not the OLTP table of the same name: it holds one row per wave with the highest assigned_stops.
Source: 0001_last_mile_facts.up.sql and 0002_stop_eta_windows.up.sql. Each fact table is fed by one event type:
wavesbywave.DeliveryWaveScheduled;wave_inductionsby the highestassignedStopsofwave.DeliveryWaveCapacityChanged;wave_cancellationsbywave.DeliveryWaveCancelled;wave_costsbywave.WaveCostDeclared(the last declaration wins; 0 is a declared cost).route_plansbyroute.RoutePlanned(the analytics topic);route_packagesbyroute.PackageOutForDelivery;delivery_attemptsbyroute.DeliveryAttempted(attempted_atis the attempt's own instant);stop_eta_windows(migration0002_stop_eta_windows, ADR 0013: one row per package of aroute.StopEtaWindowAssigned, the first row of a route and package stays) byroute.StopEtaWindowAssigned;package_returnsbypackageattempthistory.PackageReturnInitiated(initiated_atis the CloudEvents time, the payload carries no instant).analytics_processed_eventsis the projector's inbox: every applied CloudEvents id is recorded once, in the same transaction as its effect on a fact table;occurred_atis the basis ofGET /reports/freshness.
Indexes: idx_delivery_attempts_route on (route_id, tracking_number), idx_delivery_attempts_tracking on (tracking_number, attempted_at), idx_delivery_attempts_attempted_at, idx_package_returns_initiated_at, idx_waves_service_date, idx_analytics_processed_events_occurred_at and idx_stop_eta_windows_tracking on (tracking_number). Down migration: 0001_last_mile_facts.down.sql.