ADR 0005: Flyway migrates the schema at boot, in per-module version bands, out of order
Status: Accepted
Context
Every module keeps its tables in the one karyo schema
(ADR 0004) and changes its own tables as it changes.
The schema has to be reproducible from nothing, versioned alongside the code that expects it, and
changed forward only. The supported deployment is one application container
(ADR 0022).
Decision
Flyway, through
quarkus-flyway, runs inside the application at startup (migrate-at-start: true) and creates thekaryoschema if it is absent (services/karyo-app/src/main/resources/application.yaml,quarkus.flyway.schemas, quarkus.flyway.default-schema, quarkus.flyway.create-schemas).Every migration lives in the aggregator, under
services/karyo-app/src/main/resources/db/migration/<module>/, and Flyway reads exactly the directories listed inquarkus.flyway.locations(application.yaml,quarkus.flyway.locations). Migrations are plain SQL.One Flyway history covers every module, so a version number is unique across the whole schema (
application.yaml,quarkus.flyway). Each module owns a numeric band, listed with the ranges in use in the data model.out-of-order: true(application.yaml,quarkus.flyway.out-of-order), because the bands interleave: a module's next migration is usually numbered below versions that other modules have already applied to a long-lived database. Within a band, versions stay in order.A released migration is immutable, comments included. Flyway validates the checksum of every applied migration when it migrates, so editing an applied file stops every existing installation at boot. A change is a new forward migration. A decision that has to stay correctable is recorded in code, not in a migration comment (
KpiViewRepository).
Consequences
- The schema is versioned with the code, and a fresh database reaches the current schema by replaying every migration.
- Installing or upgrading needs no separate step: starting the new image migrates the database.
- A failed migration fails startup, and there is no rollback. The fix is a further migration.
- A migration directory missing from
quarkus.flyway.locationsnever runs, and nothing warns. - With out-of-order application, a long-lived database can apply migrations in a different order from a fresh one, so a migration must not depend on another module's later migration having run.
- This record is the band allocation; no other document holds it. Known latent defect:
crossdock holds
V1300and docstore already holdsV1301andV1302, so the next crossdock migration numbered by the obvious convention collides with an applied docstore version and fails startup on every installation. Nothing breaks until that migration is written. - The migrations of the monitors, crossdock, wave and streaming engines are in this repository and on the location list, so every installation creates their tables and leaves them empty until a capability acts on them.
- Two identity idioms coexist in the schema,
BIGSERIALandGENERATED BY DEFAULT AS IDENTITY. Both satisfy the entities' identity generation, so the mix is cosmetic.
Alternatives considered
- A separate migration step that runs before the application starts. Not taken. It would keep a failed migration from taking the application down with it. The reason for migrating inside the application instead is not recorded.
- Independent version numbering per module. Not possible with one Flyway history over one schema; hence the bands.
- Liquibase. Rejected: its changeset formats are more verbose and harder to review than SQL, and a database-agnostic abstraction buys nothing for a PostgreSQL-only product while hiding PostgreSQL features such as partial indexes.
- Hibernate schema auto-update. Rejected: it cannot drop anything, cannot create indexes or constraints the entities do not map, keeps no version history and is unsafe in production.
- Hand-applied SQL scripts. Rejected: no record of what has run, no ordering and no checksums.
Evidence
services/karyo-app/src/main/resources/application.yaml,quarkus.flyway- Flyway configuration and the location listservices/karyo-app/src/main/resources/db/migration/- 121 migrations in 17 directoriesservices/karyo-app/src/main/resources/db/migration/crossdock/V1300__create_cross_dock_orders.sql,services/karyo-app/src/main/resources/db/migration/docstore/V1301__create_documents.sql,services/karyo-app/src/main/resources/db/migration/docstore/V1302__create_document_templates.sql- the shared 1300 bandservices/reporting-service/karyo-reporting-core/src/main/kotlin/com/karyo/reporting/repository/KpiViewRepository.kt,KpiViewRepository- why a decision cannot live in a migration comment- Data and persistence
Related
- ADR 0004 - the schema being migrated
- ADR 0022 - the deployment that runs the migration
- Upgrade, backup and recovery - what a migration means for an upgrade