Entity-relationship diagrams
This page is a copy of docs/docs/ddd/entity-relationship.md on develop, derived from that repository's code. Edit it there, then re-sync.
The final schema after applying the migrations in order. There are two separate databases:
- the OLTP database (
DATABASE_URL):migrations/0001_init,0002_outbox,0003_submitted_state,0004_capability_offers,0005_hold_outcome,0006_retail_network_map,0008_inbound_loop,0009_inbound_receipts,0010_node_profile,0011_inventory_feed(there is no0007: the node-profile migration was written as 0007 and renumbered to 0010 when the inbound loop reached develop first, because golang-migrate never runs a file numbered below a database's current version); - the analytical database (
ANALYTICS_DATABASE_URL):migrations/analytics/0001_report,0002_rejected_submission_failed.
Both are required: there is no in-memory mode (ADR 0025).
A relationship line is drawn only where a real FOREIGN KEY exists.
OLTP database
Source: migrations/0001_init.up.sql, migrations/0002_outbox.up.sql,
migrations/0003_submitted_state.up.sql, migrations/0004_capability_offers.up.sql,
migrations/0005_hold_outcome.up.sql, migrations/0006_retail_network_map.up.sql,
migrations/0008_inbound_loop.up.sql, migrations/0009_inbound_receipts.up.sql,
migrations/0010_node_profile.up.sql, migrations/0011_inventory_feed.up.sql;
internal/adapters/outbound/postgres/migrate.go (golang-migrate).
Omitted: indexes. They are idx_network_orders_unanswered (partial on
acknowledge_by WHERE state = 'NEW'), idx_network_orders_local_order
(partial, local_order_id IS NOT NULL), network_orders_open_holds (partial
on acknowledge_by WHERE local_order_id IS NOT NULL AND hold_outcome IS NULL AND state IN ('NEW', 'REJECTED'), the orphaned-hold sweep's candidates; empty in a
healthy fleet), idx_network_order_lines_sku,
idx_outbox_events_unpublished (partial, published_at IS NULL),
network_submissions_unsettled (partial on kind, updated_at WHERE status IN ('SENDING', 'PROCESSING'), the reconciliation candidates),
inbound_shipments_due and inbound_receipts_due (both partial on
next_attempt_at WHERE status = 'PENDING') and
inbound_receipt_findings_asn.
The named CHECKs added by 0005 are hold_outcome_vocabulary,
hold_outcome_has_settled_at (outcome and instant are set together) and
hold_outcome_requires_hold (an outcome needs a local_order_id); the same
migration drops local_order_requires_answer, the CHECK that allowed
local_order_id only from SUBMITTED on (ADR 0024). The table network_orders
therefore now carries a local_order_id on a NEW or REJECTED order too.
schema_migrations is created by golang-migrate, not by a migration file;
its columns are shown as golang-migrate's Postgres driver creates them.
Infrastructure tables: outbox_events is the transactional outbox
(ADR 0003), one row per event per topic. headers holds the CloudEvents
content-type plus, when the row was written inside a span, its W3C
traceparent (ADR 0022); attempts counts failed sends and a row at
OUTBOX_MAX_ATTEMPTS is no longer claimed by the relay (ADR 0021). schema_migrations is
golang-migrate's bookkeeping. network_submissions is the submission ledger
of the live retail-network adapter (ADR 0026): one row per (kind, subject),
with the idempotency key and the exact request body written before the
first HTTP call, so a retry after a crash re-sends byte for byte what was
first meant. gtin_sku_cache keeps product-master's GTIN→SKU answers so a
hot path never asks twice (only an answer is cached, never a miss).
inbound_receipt_events is the receipt consumer's dedupe gate: the
CloudEvents id of every applied inbound-receiving event, so a redelivery is
a no-op (ADR 0028). There is no processed_events inbox and
no idempotency_keys table in this database: the remaining OLTP Kafka
consumers are the two in-memory capability caches, and inbound HTTP
idempotency is "find by network_ref first".
The tables of 0006, 0010 and 0011 belong to the live retail-network
adapter (internal/adapters/outbound/retailnetwork, ADR 0026 and ADR 0027);
in NETWORK_MODE=stub they stay empty. Those of 0008 and 0009 belong to
the inbound loop (ADR 0028): sellers ship SSCC-labelled cartons to a site,
the network reports the shipments through its vendor API, this service
registers an ASN in inbound-receiving and, when its receipt closes, reports
the receipt back — every step restart-safe because the state lives here.
Logical links without a foreign key:
network_orders.local_order_id→order-management's order id. That is another context's database, so the link is by value only (the persisted correlation of OM ADR 0018). It names the held order from the moment it is raised, so the orphaned-hold sweep can find and cancel it.capability_offers.sku/network_order_lines.sku→ the same SKU vocabulary. They are separate aggregates and are deliberately not joined.capability_offers.site_id/network_orders.site_id: a shared value, not a reference.outbox_events.keyholds thenetwork_refof the order that raised the event. It is a Kafka partition key, not a foreign key.- The retail-network adapter tables deliberately have no foreign key to
network_orders: the aggregate must not depend on which gateway answered for it.network_purchase_orders.network_ref↔network_orders.network_refandnetwork_nodes.node_id↔ the node anetwork_purchase_ordersrow was issued to are links by value only. inbound_shipments.asn_number↔ inbound-receiving's ASN, andinbound_receipts.receipt_id↔ its receipt: another context's database, so the links are by value; the FKs that do exist here point atinbound_shipments, the local root of the inbound loop.gtin_sku_cache.sku/network_inventory_feeds' feed content ↔ the product-master and network vocabularies; the cache answers for a GTIN the feed lists, but neither is constrained.
Analytical database
Source: migrations/analytics/0001_report.up.sql and
0002_rejected_submission_failed.up.sql (adds
orders_rejected_submission_failed);
internal/adapters/outbound/analyticsstore/postgres_projection.go,
consumed_events_repo.go.
Omitted: the indexes idx_analytics_processed_events_occurred_at and
idx_acknowledgement_rollup_day_bucket. DOUBLE_PRECISION stands for the
SQL type DOUBLE PRECISION, because Mermaid needs a single token. There
are no foreign keys in this database.
All tables here are analytics/projection or infrastructure tables.
They are derived from the analytics event stream and are not a source of
truth. analytics_consumed_events is the consumer's dedupe gate; it is
written in the same transaction as the projection write (ADR 0023).
analytics_processed_events is claimed by the projection upsert and
drives freshness. acknowledgement_rollup is the report fact table.
Table ≠ aggregate
| Table | Aggregate / role | Notes |
|---|---|---|
network_orders | NetworkOrder (root) | One row per aggregate. Save upserts on network_ref; there is no version column (no optimistic concurrency). |
network_order_lines | NetworkOrder (its Lines) | Inserted with ON CONFLICT DO NOTHING; lines never change after receipt. |
capability_offers | CapabilityOffer | One row per (sku, site_id), upserted on every recompute. It is a snapshot with no history. |
network_nodes, network_purchase_orders, network_purchase_order_lines, network_labels | none (adapter state, ADR 0026) | The live retail-network adapter's memory: fleet ids ↔ network ids, and the labels a shipment confirmation must echo. Owned by internal/adapters/outbound/retailnetwork; empty in NETWORK_MODE=stub. |
network_submissions | none (adapter state) | The submission ledger: idempotency key + exact request body per write kind, before the first HTTP call. |
network_node_profiles, network_inventory_feeds, network_rejected_listings | none (adapter state, ADR 0027) | The supply loop's memory: the node-profile revision in force, where each feed part stands, and the listings the network refused. |
inbound_shipments, inbound_poll_cursors, gtin_sku_cache | none (inbound-loop state, ADR 0028) | Shipments as polled (with retry), the per-site poll cursor, and the GTIN→SKU answer cache, in internal/adapters/outbound/postgres. |
inbound_receipt_events, inbound_receipt_cartons, inbound_receipt_findings, inbound_receipts | none (inbound-loop projection + report queue, ADR 0028) | The local projection of inbound-receiving's receipt events (with consumer dedupe) and the queue of closed receipts still to report to the network. |
outbox_events | none (infrastructure) | Written in the same transaction as the aggregate by postgres.OutboxPublisher; drained by postgres.OutboxRelay (FOR UPDATE SKIP LOCKED). |
schema_migrations | none (infrastructure) | golang-migrate. |
analytics_processed_events, analytics_consumed_events | none (analytics infrastructure) | Idempotency for the projector. |
acknowledgement_rollup | none (read model) | The "Acknowledgement & Translation" report. |