0053. Match planned capacity to an order's site by the plan's site_id
Status
Accepted — implemented by the change that introduces this record. Amends ADR 0031 §5 (site matching) and extends §1 (the read-model row and its merge). ADR 0031 is an accepted ADR and, by the rule in About ADRs, is not edited to change its decision: this record is the amendment, and ADR 0031's Status line points here, as it already does for ADR 0047 and ADR 0052. A new record rather than a rewrite of ADR 0031 is required precisely because the decision changes.
Context
ADR 0031 §5 matches a window to an order's site when window.location == site, where
site is the order's fulfillmentSiteId or PLANNED_CAPACITY_SITE_ID (ADR 0047). It
documented the weakness: warehouse-planning's location is the ProcessCapacity
location the plan evaluates, not necessarily a site code, so a deployment whose site
code differs from a plan's location annotates no order, silently.
warehouse-planning's own ADR 0012 added the canonical site_id for exactly this
reason: a facility-layout site code, additive and optional on the event (omitempty;
a plan stored before its migration 0008 carries none). Only CapacityPlanPublished
and CapacityPlanSuperseded carry it. The events that follow a Published on the same
plan do not:
CapacityShortageDetectedis queued AFTERCapacityPlanPublishedand carrieslocationonly — and it is the event that carries the shortage.- A late
CapacityPlanCreatedcarrieslocationonly.
The read model upserts by plan_id, last writer wins by AsOf. A naive upsert
therefore overwrites the stored site_id with an empty one the moment the shortage
event lands.
Decision
-
The row keeps
site_id.order.PlannedCapacityWindowgainsSiteID.planned_capacity_windowsgainssite_id TEXT NOT NULL DEFAULT ''(migration0016, additive: existing rows are not touched and read as "no site_id", with a partial index on(site_id, window_end)for the rows that have one). The consumer readsdata.site_idon every type that may carry it. -
Matching:
site_idwhen the plan has one, elselocation.PlannedCapacityWindow.MatchesSite(site)is the one rule: a non-emptySiteIDmatches only when it equals the site, whatever the location (so a plan for another site whoselocationhappens to equal this site does NOT annotate); an emptySiteIDmatches whenLocationequals the site, which is the legacy behaviour for every plan that never carried one. Both are exact and case-sensitive, as in ADR 0031.CapacityConstraintsusesMatchesSite. -
A write without a
site_idnever erases a stored one. The merge lives in the domain function both adapters already share,PlannedCapacityWindow.ReplacementOf:- a Created, Published or ShortageDetected write replaces the row whole (ADR 0031
§1), except that an empty incoming
site_idkeeps the stored one, and a non-empty one replaces it; - a SUPERSEDED write keeps the stored facts (ADR 0052), including a stored
site_id, and fills an empty one from the event. The Postgres adapter reads the rowFOR UPDATEand callsSupersedesthenReplacementOf, the in-memory one the same two functions, so they cannot disagree.Supersedesis unchanged. A write thatSupersedesrejects (stale, a downgrade, or onto a SUPERSEDED row) is dropped whole and teaches nothing, as before.
- a Created, Published or ShortageDetected write replaces the row whole (ADR 0031
§1), except that an empty incoming
-
The annotation lists by site; the REST listing does not change. A new port method
PlannedCapacityRepo.ListForSite(site, endingAfter)returns the rowsMatchesSite(site)accepts (the SQL predicate mirrors the domain rule; the domain re-applies it).OrderCapacityConstraints.Foruses it.GET /planned-capacity?site=still filters onlocationexactly as before andListByLocationis untouched. The only change to that response is an additive, optionalsiteIdon each window (omitempty, absent for a plan without one): a response property added to an existing schema, not a breaking change. The order'scapacityConstraint.siteis still the site that was matched. -
Operator guidance. A deployment no longer needs
PLANNED_CAPACITY_SITE_IDto equal a plan'slocation: it needs to equal the plan'ssite_id. For a plan with nosite_idthe old rule stands, so no deployment that annotated before stops doing so.
Consequences
Easier
- A plan whose
locationis a zone annotates the orders of its site, and a plan of another site no longer annotates an order by coincidence of a location string. - The
site_idsurvives the Published → ShortageDetected → Superseded sequence, pinned by a table over (stored, incoming) × (equal, differs, empty) and by a consumer test that replays that sequence.
Harder / what this does NOT solve
- A plan stored before this change has no
site_idin this read model, even if warehouse-planning's event carried one: the migration does not backfill. Such a plan keeps matching bylocationuntil a Published, or a Superseded, for it is redelivered (a DLQ or partition replay). That is the legacy rule, not a regression. - Two plans of one window but different
site_iddo not supersede one another here. This context still trusts warehouse-planning'sCapacityPlanSuperseded(ADR 0052). site_idis not validated. Likelocation, it is a producer-owned code taken as published; a plan carrying a code that matches no order simply annotates none.- The in-flight window of a deploy. The migration is additive and the column is
read and written only by this binary, so a rolling update where an old pod upserts a
row without
site_id(the column default) leaves that row on the legacy rule until a new-version write for the plan arrives. - Mutation score. The new decisions live in
MatchesSite,ReplacementOfandfirstNonEmptyinplanned_capacity.go, inside thegremlinsgate oninternal/domain/order.