# Migration Guide Design §31-§32. Flyway owns the schema; Hibernate only validates. ## Who may change the schema | Environment | Mode | |---|---| | local, test, dev | migrate at startup with the migration credential | | staging, prod | deployment-owned migration; the application validates only | Migrating from inside the application in production means every instance of a rolling deploy races to apply the same script, and the loser's failure is indistinguishable from a real one. `ddl-auto` is `validate` or `none`. Never `update`: it never drops or narrows anything, so it produces a schema that is neither the old one nor the one the migrations describe — silently, on whichever instance started first. ## Validation fails closed and never repairs At startup Flyway's own validation (and Hibernate `ddl-auto=validate`) refuses a checksum mismatch, a missing migration, or a schema Hibernate disagrees with. Nothing in the application calls `repair`. In tests, the testkit's `FlywayValidationGate` turns a failed `ValidateResult` into `SchemaMismatchException`; the migration contract tests use it. Repair rewrites the schema history table to match whatever scripts are on disk. That resolves the symptom by deleting the evidence: a checksum mismatch means the deployed script differs from the applied one, and the interesting question is which change is missing from this database. Repair makes that question unaskable. It exists only as an explicit admin operation with an operator, a reason, and an approval (design §8.4). Only Flyway's structured error codes reach the exception. Its messages embed the script path and part of the failing statement. ## Bound the lock wait A migration that locks a table already in use starts with `SET LOCAL lock_timeout = '5s';`, before the first statement that takes the lock. `ALTER TABLE` takes the strongest lock there is, which conflicts even with a plain read; a non-concurrent `CREATE INDEX` takes one that stops every write; `UPDATE` and `DELETE` wait on the rows other transactions hold. Unbounded, the migration waits for the oldest open transaction that touched the table — an operator's forgotten `BEGIN` in psql has no deadline — and every read and write that arrives meanwhile queues behind the migration, holding a pool connection while it waits. With the bound, the migration fails after five seconds, Flyway rolls it back whole, the database stays on the version before it and the new release does not start: a failed deploy instead of an outage. Deploy again when the transaction has ended. `LOCAL`, because a plain `SET` outlives the migration on the pooled connection Flyway borrowed and changes every statement that later runs on it. A statement on a table the same migration created a moment earlier waits for nothing and needs no bound. Enforced, not remembered: `LockWaitMigrationInspector` (persistence-jpa testkit) reads a script and names every statement that locks an existing table before the bound. `MigrationLockWaitTest` runs it over every tree this leaf's database runs, `SampleMigrationLockWaitTest` over the sample's stream; a service stream copies the latter with its own directory. The migrations released before the rule wait unbounded and stay that way (they are applied, and applied files are never edited); the test lists them and refuses any new one. `PostgreSqlMigrationLockWaitIntegrationTest` (`postgresqlMigrationLockWaitIntegrationTest`) and the gRPC ledger's `GrpcOperationLedgerMigrationLockWaitIT` show on real PostgreSQL that a bounded migration gives up within the bound while another transaction holds the table, and leaves the version below it. The rule came from V21 having the bound and V22, in the same commit, not having it (re-review 2026-11-03 round 2, RR-02-09). ## Concurrent index builds `CREATE INDEX CONCURRENTLY` cannot run inside a transaction block, and Flyway wraps migrations in one by default. The migration therefore needs a companion configuration: ```conf # V42__order_index.sql.conf executeInTransaction=false ``` The testkit's `ConcurrentIndexMigrationInspector` checks a script for this in a migration test (nothing checks it at startup), and additionally requires the migration to contain nothing else. A failed concurrent build leaves an invalid index behind; recovering is a single `DROP INDEX` when the migration did nothing else, and a manual reconstruction of partial state when it did. An invalid index is not merely useless — the planner ignores it while every write still maintains it. [runbooks.md](runbooks.md) has the query that finds them. Nothing drops them automatically: an invalid index can also mean a build is still running, and the two are indistinguishable from the catalog alone. ## Upgrade scenarios Three, each catching something the others do not: | Scenario | Catches | |---|---| | `empty` | an early migration edited to match a later one, no longer applying to a fresh database | | `previous-release` | the actual deployment path; the only one exercising this release's migrations | | `oldest-supported` | a migration that silently assumes state only recent databases have | Each asserts a data invariant, not just the schema version. A migration that renames a column and loses its contents leaves the version correct and the data gone.