Karyo
Documentation/ Decision records

ADR 0050: Operational rows are keyed by Facility alone

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Supersedes the temporary Business Unit key on operational rows in ADR 0040.

Context

The Company contains Business Units, each with one or more Facilities. Operational consumers now name a Facility explicitly. Keeping client_id on an operational row duplicates the Facility's immutable parent and makes readers and writers maintain two ownership keys.

Decision

FacilityEntity extends BaseEntity and stores only facility_id. The 36 operational tables lose client_id. Their writers set a Facility explicitly. The database continues to refuse missing, unknown and archived Facilities; it never chooses one for a writer.

A reader that needs the Business Unit derives it through OrganizationLookup from the row's Facility. Collection readers batch the Facility lookup. Event producers pass the derived unit to the outbox explicitly. The events library has no dependency on the auth implementation or API. A formula-backed ownership field is not used: a freshly persisted row must not publish an event with an uninitialized Business Unit.

Transport numbers are unique within a Facility. Count-line uniqueness uses the count order and stock unit. Consolidation-line uniqueness uses the group, order line, lot and cart. Sequence keys remain per Business Unit, derived from the Facility.

The legacy Business Unit scope type, its read/write/affiliation accessors and its operational scalar accessors are removed. Operational request bodies and responses no longer carry clientId. Products and substitutions resolve their catalogue through the selected or sole authorized Facility. An ambiguous Facility is refused, including when the caller can read catalogues in multiple units.

Business Unit visibility uses organizational reach. Administration requires the endpoint's action at the Business Unit or Company node. A Facility grant does not grant Business Unit administration. Approved affiliation and principal kind remain identity facts, not operational scope.

A webhook subscription belongs to one Facility. A Facility event reaches only that Facility's subscriptions. A Business Unit event reaches subscriptions of its Facilities. The existing archive cutoff and event matching still apply.

Consequences

Operational ownership has one key. Six master-data and configuration tables still carry client_id: item_data, item_substitutions, inactive_products, system_properties, home_layouts and document_templates. Configuration unit 0 still denotes the Company layer. Storage, claim and public Business Unit vocabulary are separate follow-up work; Kotlin affiliation identifiers remain unchanged.

Isolation remains explicit application code. The unused Hibernate tenantFilter definition is removed. Facility scope, declaration and registry guards remain in force. There is no compatibility window, legacy adapter or upgrade tooling: Karyo is unreleased and has no users.

Alternatives considered

  • Keep client_id on operational rows and check it against the Facility: rejected because it keeps two ownership keys that must agree on every write.
  • A read-only client_id formula derived from the Facility: rejected because a freshly persisted row would publish Business Unit 0 until it is reloaded.
  • A compatibility window or legacy API adapter: not chosen; Karyo is unreleased and has no users.

Evidence

  • services/karyo-app/src/main/resources/db/migration/common/V1601__drop_operational_business_unit_column.sql, karyo.alerts, karyo.asns, karyo.consolidation_groups, karyo.consolidation_lines, karyo.count_campaigns, karyo.count_lines, karyo.count_orders, karyo.count_sessions, karyo.cross_dock_orders, karyo.delivery_orders, karyo.documents, karyo.fix_assignments, karyo.goods_receipts, karyo.inventory_journals, karyo.item_data_areas, karyo.monitor_config, karyo.pick_orders, karyo.picks, karyo.product_facility_policies, karyo.report_definitions, karyo.shipment_orders, karyo.shipments, karyo.shipping_unit_lines, karyo.shipping_units, karyo.sort_scans, karyo.stock_units, karyo.storage_locations, karyo.storage_strategies, karyo.stream_batches, karyo.transport_orders, karyo.unit_loads, karyo.wave_selection_rules, karyo.waves, karyo.webhook_subscription, karyo.work_group_members, karyo.work_groups - drops client_id from the 36 operational tables and adds the Facility-keyed uniques.
  • services/karyo-app/src/main/resources/db/migration/common/R__explicit_facility_writes.sql, karyo.validate_facility_write - the write guard refuses a missing, unknown or archived Facility.
  • libs/karyo-common/src/main/kotlin/com/karyo/common/domain/BaseEntity.kt, BaseEntity - FacilityEntity carries only the Facility.
  • services/integration-hub-service/karyo-webhooks-core/src/main/kotlin/com/karyo/webhooks/relay/WebhookFanoutScheduler.kt, WebhookFanoutScheduler.audienceMatches - Facility and Business Unit audiences.
  • services/karyo-app/src/test/kotlin/com/karyo/webhooks/relay/FanoutTest.kt, FanoutTest - sibling Facilities are isolated and a Business Unit event reaches both.
  • services/karyo-app/src/test/kotlin/com/karyo/layout/FacilityStampingTest.kt, FacilityStampingTest - a fresh schema keeps client_id on exactly the six master tables.
  • ADR 0007 - other modules' rows are referenced by id.
  • ADR 0040 - the temporary Business Unit key this record removes.
  • ADR 0045 - webhook subscriptions belong to a Facility.
  • ADR 0048 - grants and node reach.