Entity-relationship diagram
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 dock appointment is owned by dock-scheduling since ADR 0019. This context keeps a read-only local copy fed by dock-scheduling's DockAppointmentBooked / DockAppointmentCancelled, no longer books, checks in, cancels or completes appointments, and no longer raises the four DockAppointment* events (the one-shot api republish-appointments backfill that re-announced them was removed, ADR 0022). Where this page describes those commands, rules or events, read it as the pre-transfer design; the page is not yet rewritten.
The final schemas after all migrations. There are two Postgres databases:
- the OLTP database (
DATABASE_URL): migrations0001_inbound_receivingto0004_receipt_carton_discrepancies, applied at start-up; - the separate analytical database (
ANALYTICS_DATABASE_URL,inbound_receiving_analytics):analytics/migrations/0001_receiving_performance, written only bycmd/inbound-projectorand read (read-only) bycmd/inbound-reports(ADR 0006).
A relationship line is drawn only where a real FOREIGN KEY exists;
logical links without one are described in prose below.
OLTP database
Source: internal/adapters/outbound/postgres/migrations/0001_inbound_receiving.up.sql
through 0004_receipt_carton_discrepancies.up.sql (0002 adds housekeeping
indexes only and changes no table).
Omits: CHECK constraints and indexes (listed below), the "C" collation on the
text identifiers, and schema_migrations (golang-migrate's own table).
Table to aggregate
"Table is not aggregate": the mapping is not one to one.
| Table | Role | Backs |
|---|---|---|
asns, asn_lines | domain | the Asn aggregate (root row plus its lines) |
appointments, appointment_asns | local copy | the read-only copy of dock-scheduling's appointments (root row plus the ASN numbers it covers, in booking order), fed by its DockAppointment* events (ADR 0019, ADR 0020) |
receipts, receipt_lines | domain | the Receipt aggregate (root row plus the snapshot of the ASN's lines and the two counters) |
receipt_cartons | domain | the receipt's carton ledger (ADR 0012): manifest cartons plus cases received by GTIN; part of the Receipt read model |
receipt_carton_discrepancies | domain | the carton discrepancies raised on a receipt (ADR 0015); append-only, part of the Receipt read model |
asn_cartons, asn_carton_contents | domain | the hierarchical ASN's announced SSCC cartons and their contents (ADR 0013), rolling up into asn_lines |
pack_levels | local copy | the PackagingView, projection of product-master's ProductPackLevelDeclared / ProductPackLevelRetired (contracts R8) |
known_skus, dock_doors | local copy | read models fed by ProductRegistered and the facility-layout slot events; not an aggregate |
outbox_events | infrastructure | the transactional outbox (unique (event_id, topic); partial index on unpublished rows) |
processed_events | infrastructure | idempotency guard of the consumers, primary key (consumer, event_id) |
idempotency_keys | infrastructure | the Idempotency-Key middleware's store |
Where there is no foreign key, on purpose
appointment_asns.asn_numberhas no FK toasns:AsnandDockAppointmentare separate aggregates that reference each other by id. The copy is written from dock-scheduling's events (the booking and its ASN checks live there, ADR 0019).receipts.appointment_idandreceipts.door_codehave no FK toappointments: a walk-in receipt has neither, and the appointment is a separate aggregate.receipts.asn_numberdoes have an FK toasns.known_skusis deliberately existence-only (sku,first_seen_at): there is no foreign key fromasn_lines.skuorreceipt_lines.skuto it, because the copy is only consulted inkafkamode.asn_cartons.parent_ssccreferences another carton of the same ASN only by value: a pallet is not a row, just the SSCC its cartons name.pack_levelshas deliberately no constraint beyond the shape the consumer has already validated, and no foreign key fromasn_carton_contents.gtin: a violated constraint on the consumer path would be retried forever.receipt_cartons.lpn/receipt_carton_discrepancies.ssccname labels and findings of other contexts by value; the carton ledger's only real keys are the ones drawn above.
Constraints that carry domain rules
| Constraint | Rule |
|---|---|
receipts_one_open_per_asn (unique, partial on state = 'Open') | at most one open receipt per ASN (ADR 0002) |
UNIQUE (asn_number, sku) on asn_lines | SKUs unique across an ASN's lines |
UNIQUE (appointment_id, asn_number) on appointment_asns | an ASN is covered once per appointment |
CHECK (window_end > window_start) on appointments | the window ends after it starts |
CHECK ((state = 'Closed') = (closed_at IS NOT NULL)) on receipts | closed_at is set exactly when closed |
CHECK (received_good + received_damaged <= 2147483647) on receipt_lines | the per-line total stays within the quantity bound |
CHECK (version >= 1) on the aggregate roots | version starts at 1 |
idx_appointments_active_door (partial on Booked, CheckedIn) | serves the overlap read of one door |
outbox_events_event_id_topic_key | a relay retry never duplicates a row |
asn_cartons_active_sscc (unique, partial on active) | an announced SSCC is unique per site until the ASN is cancelled |
receipt_cartons_accepted_sscc (unique, partial on Accepted with an SSCC) | an SSCC is accepted once per site; a second acceptance is a DuplicateSscc |
CHECK ((status = 'Accepted') = (received_at IS NOT NULL)) on receipt_cartons | the scan instant is set exactly when the carton is accepted |
UNIQUE (asn_number, sscc) on asn_cartons and UNIQUE (receipt_id, lpn) on receipt_cartons | a carton is announced once per ASN and received once per receipt |
Analytical database
A separate database (ANALYTICS_DATABASE_URL, inbound_receiving_analytics),
written only by cmd/inbound-projector and read (read-only) by
cmd/inbound-reports (ADR 0006). Everything here is a projection derived from
warehouse.inbound-receiving.analytics, not a source of truth: it can be
dropped and rebuilt by replaying the topic under a new consumer group. Facts,
not aggregates: each milestone column is the CloudEvents time of the
earliest such event seen (kept with LEAST, which ignores NULLs), so
arrival order never matters. There are no foreign keys in this database.
Source: analytics/migrations/0001_receiving_performance.up.sql.
Omits: the partial milestone indexes (one per nullable milestone column) and
schema_migrations.