Sub topic 4
What happens when the application starts
This chapter assumes you have Flyway on the classpath, a db/migration folder with at least one versioned migration such as V1__create_app_user.sql, and a configured DataSource. You need no extra code to run migrations. Spring Boot does it during startup.
Auto-configuration does the wiring
When Spring Boot sees flyway-core and a DataSource, FlywayAutoConfiguration creates a Flyway bean from your spring.flyway.* properties. It also registers a bean named flywayInitializer. That bean calls migrate() as soon as the application context creates it.
Boot then makes the JPA EntityManagerFactory depend on flywayInitializer. So migrations always finish before Hibernate starts. This ordering is what lets ddl-auto: validate check a schema that already exists.
spring:
flyway:
enabled: true
locations: classpath:db/migrationBoth values shown are the defaults. Setting enabled: false skips the whole process.
The sequence inside migrate()
In words:
- Flyway connects with the application's
DataSourceand takes a lock so that two instances starting together do not migrate at once. - It creates
flyway_schema_historyif this is the first run. - It scans the configured locations and compares the files with the history table.
- With
validate-on-migrateon (the default), it checks that applied migrations still match their recorded checksums. - It runs pending versioned migrations in ascending version order, then any repeatable migrations whose checksum changed.
- It inserts one history row per migration.
On a fresh database, the log looks like this:
Flyway Community Edition by Redgate
Database: jdbc:postgresql://localhost:5432/app (PostgreSQL 16.2)
Successfully validated 2 migrations
Creating Schema History table "public"."flyway_schema_history" ...
Migrating schema "public" to version "1 - create app user"
Migrating schema "public" to version "2 - add display name"
Successfully applied 2 migrations to schema "public", now at version v2On the next start, nothing is pending. Flyway logs Schema "public" is up to date. No migration necessary. and startup continues.
When a migration fails
Flyway runs each migration in its own transaction when the database supports transactional DDL. PostgreSQL does, so a failing V2 rolls back completely and leaves no history row. The exception propagates, the application context fails, and the application does not start. This is intended. The application never runs against a half-migrated schema.
On MySQL and some other databases, DDL statements commit implicitly. A failure partway through can leave a partial change plus a failed history row. You then fix the database by hand and run repair.
Checking the result
After startup, query flyway_schema_history or run an automated test that confirms nothing is pending. Later chapters cover both.
Exercise
Hibernate runs with ddl-auto: validate and a new V3 migration adds a column mapped in an entity. Why does validation pass on startup instead of failing because the column is missing?
Show answer
Boot makes the EntityManagerFactory depend on the flywayInitializer bean, so migrate() finishes and applies V3 before Hibernate starts. When Hibernate validates the entities, the column already exists.
Exercise
A V2 migration fails on PostgreSQL during startup. What happens to the database and to the application?
Show answer
The migration's transaction rolls back, so no V2 changes or history row remain. The exception stops the application context, so the application does not start.