Entity-relationship diagram
The schemas after applying every migration in order. The service has two Postgres databases:
- the OLTP database (
DATABASE_URL), migrated bycmd/network-inventory-planningandcmd/mcpfrominternal/adapters/outbound/postgres/migrations0001to0009; - the analytical database (
ANALYTICS_DATABASE_URL), migrated and written only bycmd/nip-projectorfromanalytics/migrations/0001(ADR 0009).
Migrations run through golang-migrate. The OLTP step reads
MIGRATIONS_DATABASE_URL and falls back to DATABASE_URL, because a
transaction pooler cannot hold golang-migrate's session advisory lock
(ADR 0006).
OLTP database
Source: internal/adapters/outbound/postgres/migrations/0001_planning_read_models.up.sql, 0002_transfer_saga_and_outbox.up.sql, 0003_work_demand_release.up.sql, 0004_rebalance_runs.up.sql, 0005_rebalance_catalogue.up.sql, 0006_pack_levels.up.sql (the local copy of product-master pack levels, its ADR 0017), 0007_transfer_case_rounding.up.sql (two columns on inter_warehouse_transfer), 0008_idempotency_keys.up.sql and 0009_transfer_release_state.up.sql (widens two CHECKs on inter_warehouse_transfer)
Only transfer_audit has a foreign key. The read models mirror sibling facts
and do not reference one another, processed_events is a guard keyed by
consumer and event id, and outbox_events is a queue.
What the columns do not show:
- Migration 0003 widened the state CHECK to eleven states and migration 0009 to twelve
by adding
RELEASING(ADR 0022); 0009 also admitsRELEASINGin thereservation_idCHECK, because a releasing transfer still carries the reservation it is releasing. CANCELLED stays outside that CHECK: the release settle clears the reservation id (ADR 0023), so a cancelled transfer persists withreservation_idNULL. 0003 re-created the constraints under stable names (inter_warehouse_transfer_state_check,_quantity_check,_origin_dest_check,_reservation_id_check) and addedtransfer_picked_quantity_check: a picked quantity exists only fromPICKEDon and never exceedsquantity. - Indexes:
idx_transfer_state,idx_transfer_origin (origin_site_id, sku),idx_outbox_events_unpublished (id) WHERE published_at IS NULL,idx_site_sku_demand_site_due (site_id, due_at),idx_published_capacity_plan_site_window (site_id, window_start),rebalance_runs_started_at_idx (started_at DESC),idempotency_keys_created_at_idx (created_at). - Migration 0007 added
case_multipleandrequested_qty_eaches, both NULL for every earlier transfer and for a SKU without an active CASE level. Two CHECKs keep them consistent: both set or both null, and when setcase_multiple >= 2,requested_qty_eaches >= 1andquantitya multiple ofcase_multiple. idempotency_keys(migration 0008, fleet REST guideline section 2) holds one row perIdempotency-Keyof aPOST. The key alone is the primary key, so it is global and not scoped to a route;fingerprintis the method, the escaped path and the SHA-256 of the body. The store inserts the row withON CONFLICT DO NOTHING(status 0) in the same transaction as the aggregate change and its outbox rows, then fillsstatus,content_type,locationandbodywith the answer. The same key and fingerprint replays that answer withIdempotency-Replayed: true; the same key with another fingerprint is 409idempotency-key-reused. Answers 2xx and 4xx are stored; a 5xx or a panic rolls the transaction back, so no key is left behind. Retention is at least 24 hours; no code in the repository deletes rows (ADR 0021 lists the sweep bycreated_atas future work), so the table grows until a sweep exists.outbox_events.topicis per row, so one approval writes rows for both the integration and the analytics topic.site_sku_demand.as_ofis the applied event's CloudEventstimeand is also the row's ordering watermark: the upsert only writes when the incoming event is not older (ADR 0014).site_capabilityorders oncapability_revision,published_capacity_planonpublished_at.
Analytical database
Source: analytics/migrations/0001_saga_facts.up.sql
Append-only: one row per CloudEvents id, written in the same transaction as
its analytics_processed_events mark. It is a derived copy that can be
rebuilt from the topic, so it has no foreign keys and no link to the OLTP
tables. nip-reports reads it through a read-only pool.
Table is not aggregate
Only inter_warehouse_transfer with its transfer_audit rows maps to an
aggregate (see Aggregate design canvas). The
three planning tables are local copies of sibling facts, rebalance_runs is a
record of scheduled passes, outbox_events, processed_events and idempotency_keys are
infrastructure, and the analytical tables are projections.