Data and persistence
One database, one schema, one Flyway instance: how Karyo stores its state, how the schema is versioned, and where the module boundary does and does not hold in the data.
Derived from services/karyo-app/src/main/resources/application.yaml, the 193 migration files
under services/karyo-app/src/main/resources/db/migration/, and
libs/karyo-common/src/main/kotlin/com/karyo/common/domain/BaseEntity.kt.
One schema, one Flyway instance
All persistence is one PostgreSQL database, schema karyo
(ADR 0004). Flyway runs at startup
(migrate-at-start: true), creates the schema if absent, and applies the registered locations out
of order (services/karyo-app/src/main/resources/application.yaml, quarkus.flyway,
ADR 0005).
Out-of-order is on deliberately, and the config file says why (quarkus.flyway.out-of-order): modules ship
independently, so layout V308 can land after orders V4xx are already applied to a long-lived
database. Each module's own range stays internally ordered.
Every migration lives in the aggregator, not in the module that owns the table. There is one
datasource, one Flyway instance, and quarkus.flyway.locations is a single app-owned list. A
directory that is not on that list silently never runs.
Version bands
Each module owns a numeric band; the bands and the ranges in use are listed in the data model. That list was reconstructed by listing the directories; nothing in the build enforces it.
crossdock and docstore share the 1300 band. crossdock holds V1300, V1303 and V1305, docstore holds
V1301, V1302 and V1304. A migration numbered by the obvious convention inside one directory
can collide with an applied version in the other and fail startup on a duplicate version. This is
latent, not live.
The engine schema lives here
Four of those directories - monitors, crossdock, wave and streaming - hold the schema of
engine capabilities that are part of the same distribution. They are on the Flyway
locations list (application.yaml, quarkus.flyway.locations) like every other directory, so every installation
creates the engines' tables; a deployment that never uses a capability still leaves its
tables empty:
| Band | Directory | Tables |
|---|---|---|
| V1101-V1108 | db/migration/monitors |
monitor_config, alerts, alert_deliveries |
| V1300 | db/migration/crossdock |
cross_dock_orders |
| V1302 | db/migration/docstore |
document_templates, used only by the document-templates engine |
| V1400-V1402 | db/migration/wave |
waves, wave_selection_rules, consolidation_groups, consolidation_lines, sort_scans |
| V1500 | db/migration/streaming |
stream_batches |
The engines also stamp tables owned by the core modules. V430__delivery_order_wave.sql adds
wave membership to
delivery_orders and V431__delivery_order_streaming.sql adds a release-mode override and three
streaming timestamps; V610__pick_order_wave.sql, V611__pick_order_bulk.sql and
V612__group_shipments.sql add wave, bulk and group-shipment columns to the picking and shipping
tables. Those columns and their indexes exist in every installation too.
Cartonization, forecasting, slotting and simulation add no schema at all; they read the core modules' tables.
The schema and the code ship together. The engine schemas ship with the distribution, and no engine-specific schema or data migration was introduced (source record).
Entity base classes
BaseEntity // id (GenerationType.IDENTITY), version (@Version), created, modified
TenantEntity // + clientId (column business_unit_id): master and configuration rows keyed on a Business Unit
FacilityEntity // + facilityId: operational rows, keyed on their Facility alone
FacilityConfigEntity // + facilityId, not null and not updatable: Facility configuration
In this repository 5 entity classes extend TenantEntity, 36 extend FacilityEntity, 8 extend
FacilityConfigEntity and 25 extend BaseEntity directly. Optimistic locking via @Version is universal.
No base class carries a Hibernate filter. A FacilityEntity has no clientId and no business_unit_id column; a reader that needs its
Business Unit resolves the stored Facility through OrganizationLookup
(ADR 0050). What enforces isolation is
covered in Identity and tenancy.
Business Unit and Facility keys
Operational tables store facility_id, not a Business Unit column.
Master data and layered configuration store business_unit_id.
outbox_events, webhook_delivery and alert_deliveries use the same name for their Business Unit audience.
Migration auth/V1602__business_unit_storage_and_principal_kind.sql establishes those names.
The three configuration tables retain key 0 for the Company layer, without a Business Unit row.
Identity generation is likewise mixed: V1300__create_cross_dock_orders.sql uses BIGSERIAL,
V1301__create_documents.sql uses GENERATED BY DEFAULT AS IDENTITY. Both satisfy
GenerationType.IDENTITY, so this is cosmetic, but the schema has two idioms for the same thing.
No foreign keys across module boundaries
Cross-module references are plain IDs. V1500__create_stream_batches.sql, stream_batches states the house rule
inline: "ID-only references, no FK (house rule)." Validation happens through the owning module's
lookup SPI, which keeps each module extractable
(ADR 0007).
Principal identity migration
Principal identity is UUID-based. principals owns local account type, state, approved affiliation
and display snapshots; external_identities binds an exact provider and opaque subject; aliases
are explicit byte-sensitive issuer rows. Personal layouts, grants and live work ownership carry a
principal UUID while retaining historical subject/name snapshots. Cross-domain principal columns
remain ID-only under ADR 0007.
The structural migrations are additive. They deliberately leave required-reference constraints
NOT VALID until scripts/identity_migration.py applies a reviewed manifest in one serializable
transaction, verifies source database/schema/counts and exact row snapshots, validates constraints,
and writes the digest cutover marker last. Released migration files are never edited to repair a
data decision; a changed manifest or source stops instead of guessing.
No row-level security
There are no CREATE POLICY statements, no ENABLE ROW LEVEL SECURITY, and no
current_setting calls anywhere in the migration chain.
Isolation between goods owners is enforced in application code and nowhere else (Identity and tenancy). Why no database-level layer backs it is not recorded.
The read side bypasses the module boundary
Reporting reads foreign tables through native SQL, and so do the forecasting, slotting and simulation engines. See Modules and boundaries for the full table. Two properties of those queries matter here:
- They filter on another module's encodings, and two of those are linked at compile time. The readers take a
stock unit's state from
StockState.ON_STOCKinkaryo-inventory-apiand a pick's type fromPickingType.EXTINGUISHinkaryo-fulfillment-api, so renaming either is a compile error and not a silent wrong answer. Every other column and every other state number a reader filters on is still a literal the owner can change without a signal; treat each as a published contract. - Daily reporting, forecasting and simulation use the named Facility's
karyo.facility.time-zone. The setting inherits through Business Unit and Company before environment/default. UTC is the default, not the server'sTZ. Native demand queries useAT TIME ZONE; reporting's V1009 SQL functions accept the Facility and resolved zone explicitly. Each operation uses the same zone for buckets, date-window bounds and its calendar anchor. Year-to-date starts in that Facility's calendar.
Applied migrations are immutable, including their comments, once a release has applied them. Current calculation rules also live in ADR 0011 and the reporting module's README.
Related
- Modules and boundaries - the module graph the schema cuts across
- Identity and tenancy - what enforces owner isolation
- The engine boundaries - how the engines sit in the one distribution
- Reference: data model - the entity model