Skip to main content

Entity-relationship diagram

Synced from inbound-receiving

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.

DockAppointment moved (ADR 0019)

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): migrations 0001_inbound_receiving to 0004_receipt_carton_discrepancies, applied at start-up;
  • the separate analytical database (ANALYTICS_DATABASE_URL, inbound_receiving_analytics): analytics/migrations/0001_receiving_performance, written only by cmd/inbound-projector and read (read-only) by cmd/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.

TableRoleBacks
asns, asn_linesdomainthe Asn aggregate (root row plus its lines)
appointments, appointment_asnslocal copythe 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_linesdomainthe Receipt aggregate (root row plus the snapshot of the ASN's lines and the two counters)
receipt_cartonsdomainthe receipt's carton ledger (ADR 0012): manifest cartons plus cases received by GTIN; part of the Receipt read model
receipt_carton_discrepanciesdomainthe carton discrepancies raised on a receipt (ADR 0015); append-only, part of the Receipt read model
asn_cartons, asn_carton_contentsdomainthe hierarchical ASN's announced SSCC cartons and their contents (ADR 0013), rolling up into asn_lines
pack_levelslocal copythe PackagingView, projection of product-master's ProductPackLevelDeclared / ProductPackLevelRetired (contracts R8)
known_skus, dock_doorslocal copyread models fed by ProductRegistered and the facility-layout slot events; not an aggregate
outbox_eventsinfrastructurethe transactional outbox (unique (event_id, topic); partial index on unpublished rows)
processed_eventsinfrastructureidempotency guard of the consumers, primary key (consumer, event_id)
idempotency_keysinfrastructurethe Idempotency-Key middleware's store

Where there is no foreign key, on purpose​

  • appointment_asns.asn_number has no FK to asns: Asn and DockAppointment are 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_id and receipts.door_code have no FK to appointments: a walk-in receipt has neither, and the appointment is a separate aggregate. receipts.asn_number does have an FK to asns.
  • known_skus is deliberately existence-only (sku, first_seen_at): there is no foreign key from asn_lines.sku or receipt_lines.sku to it, because the copy is only consulted in kafka mode.
  • asn_cartons.parent_sscc references another carton of the same ASN only by value: a pallet is not a row, just the SSCC its cartons name.
  • pack_levels has deliberately no constraint beyond the shape the consumer has already validated, and no foreign key from asn_carton_contents.gtin: a violated constraint on the consumer path would be retried forever.
  • receipt_cartons.lpn / receipt_carton_discrepancies.sscc name 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​

ConstraintRule
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_linesSKUs unique across an ASN's lines
UNIQUE (appointment_id, asn_number) on appointment_asnsan ASN is covered once per appointment
CHECK (window_end > window_start) on appointmentsthe window ends after it starts
CHECK ((state = 'Closed') = (closed_at IS NOT NULL)) on receiptsclosed_at is set exactly when closed
CHECK (received_good + received_damaged <= 2147483647) on receipt_linesthe per-line total stays within the quantity bound
CHECK (version >= 1) on the aggregate rootsversion starts at 1
idx_appointments_active_door (partial on Booked, CheckedIn)serves the overlap read of one door
outbox_events_event_id_topic_keya 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_cartonsthe scan instant is set exactly when the carton is accepted
UNIQUE (asn_number, sscc) on asn_cartons and UNIQUE (receipt_id, lpn) on receipt_cartonsa 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.