Skip to main content

Entity-relationship diagram

The OLTP schema (migrations 0001-0003) created by the migrations internal/adapters/outbound/postgres/migrations/0001_slotting_schema.up.sql, 0002_slot_plan_move_executions.up.sql (ADR 0010, additive) and 0003_case_pick_faces.up.sql (ADR 0012, additive), applied at boot (see Runbook). Text keys compared or ordered in Go (plan ids, SKUs, slot codes) use the "C" collation so database order equals the in-memory adapters' byte order.

Source: internal/adapters/outbound/postgres/migrations/0001_slotting_schema.up.sql, 0002_slot_plan_move_executions.up.sql, 0003_case_pick_faces.up.sql.

Which table backs what​

TableBacksWritten byRead by
slot_plansthe SlotPlan aggregate rootSlotPlanRepo.Save (insert at version 1, then UPDATE ... WHERE version = loaded)every plan query, Current (the site's Approved plan)
slot_plan_assignments, slot_plan_moves, slot_plan_unassignedthe plan's content, written once with the plan, ON DELETE CASCADESlotPlanRepo.Save on insertplan reads, GET /forward-slots
demand_linesthe demand copyApplyDemandChanged (upsert by order and line)DemandLedger.Velocity
product_profilesthe product copyApplyProductClassified, ApplyPhysicalProfile (upsert WHERE version < new)ProfileDirectory.Many
pack_levelsthe pack-level copy (ADR 0012): one row per (SKU, level), guarded by the producer's aggregate version; effective dims are the measured ones if present, else the declared onesApplyPackLevelDeclared, ApplyPackLevelRetired, ApplyPackLevelMeasuredPackLevelView.Get / ActiveCases (case-pick faces)
zones, slotsthe layout copyApplyZoneRegistered, ApplyLocationSlotRegistered (upsert WHERE slots.active), ApplyLocationSlotDecommissionedSlotCatalogue.ForwardSlots (join on zone_id)
slot_plan_move_executionsthe execution-progress read model (ADR 0010)RecordRelocation (INSERT ... ON CONFLICT (relocation_id) DO NOTHING), in its unit of workGetPlan.View (GET /slot-plans/{planId}, MCP get_slot_plan) through slotplan.Progress
outbox_eventsthe transactional outboxevery write use case, in its unit of workthe relay (FOR UPDATE SKIP LOCKED)
processed_eventsconsumer idempotencyevery consumer use case, in its unit of workthe same claim
idempotency_keysREST idempotency of every POSTthe middleware, in the same transaction as the use casethe middleware on a repeated key

Indexes and constraints that carry rules​

ObjectRule
uq_slot_plans_one_approved_per_site (unique on site_id where state = 'Approved')at most one Approved plan per site; the losing approval of a race becomes 409 approved-plan-conflict
idx_slot_plans_listing (generated_at DESC, plan_id DESC)keyset paging of GET /slot-plans
idx_slot_plans_site_stateCurrent and the state filter
UNIQUE (plan_id, slot) on assignmentsone SKU per slot within a plan
CHECK (window_from < window_to) and the state / class / kind / reason checksthe domain enums mirrored in the database
idx_demand_lines_site_due_active (partial, state = 'ACTIVE'), idx_demand_lines_sku_duethe velocity query
CHECK ((volume_mm3 IS NULL) = (weight_g IS NULL))a unit size is both dimensions or none
slot_plan_assignments_cases_capacity_only_casea case-pick assignment carries its cases capacity, an each-pick none
the two pack_levels dim-group CHECKs (declared and measured)a level's dimensions are all four or none
idx_zones_site_zone_code, idx_slots_zone (partial, active)the forward-slot join
outbox_events_event_id_topic_key (unique event_id, topic)one row per CloudEvent per topic
idx_outbox_events_unpublished (partial, published_at IS NULL)the relay scans only the unpublished tail
idx_idempotency_keys_created_atage-based cleanup (Runbook)
slot_plan_move_executions_pkey (relocation_id)a relocation counts once, whatever its CloudEvents id
idx_slot_plan_move_executions_plan (plan_id, move_seq)the progress read of one plan

slots.zone_id has no foreign key on purpose: zone and slot events may arrive in any order, and the join happens at plan time. pack_levels has no foreign key either: it is a local copy of product-master's data, guarded by the producer's version, never by a constraint into another context's key.

The analytical database (ADR 0006)​

A separate database (ANALYTICS_DATABASE_URL; warehouse-infra names it slotting_optimization_analytics), created by the one migration analytics/migrations/0001_slot_plan_funnel.up.sql and applied by cmd/slotting-projector only. It is a projection of warehouse.slotting-optimization.analytics, not a source of truth: drop it and replay the topic under a new consumer group to rebuild it. No foreign key links the two tables or reaches the OLTP schema.

Source: analytics/migrations/0001_slot_plan_funnel.up.sql, internal/adapters/outbound/analyticsstore/postgres_projection.go.

TableBacksWritten byRead by
analytics_processed_eventsprojector idempotency (one row per applied CloudEvents id) and the freshness basisProjection.Apply: INSERT ... ON CONFLICT (event_id) DO NOTHING, in the same transaction as the fact upsertReader.LastEventAt (GET /reports/freshness)
slot_plan_factsone row per plan the projection has seen, one column per milestoneProjection.Apply: upsert keeping the EARLIEST time per milestone (LEAST ignores NULLs), so arrival order does not matter; an approval with supersedes_plan_id also stamps superseded_at on that plan's rowReader.FunnelDays (GET /reports/slot-plan-funnel): per UTC day window, the counts of generated, approved and rejected milestones, the approval latency (approved_at - generated_at, only when both were seen) and the supersessions
ObjectRule
analytics_processed_events_pkey (event_id)an event is applied once, whatever the redelivery
idx_analytics_processed_events_occurred_atthe freshness query
idx_slot_plan_facts_sitethe siteId filter
idx_slot_plan_facts_{generated,approved,rejected,superseded}_at (partial, IS NOT NULL)the per-milestone day-window counts
COLLATE "C" on plan_id, site_id, supersedes_plan_idbyte order, like the OLTP text keys