Sub topic 11
Java-based migrations
This chapter assumes you can write versioned SQL migrations, know how Flyway records them in flyway_schema_history, and run Flyway at startup in Spring Boot. Here you will write a migration in Java for work that SQL handles badly.
When SQL is not enough
Most migrations should stay in SQL. Java is the better choice when a change needs logic. Examples are:
- Backfilling data with rules that are hard to write as one statement.
- Reading rows, computing a value in application code, and writing it back.
- Branching on conditions that plain SQL cannot express portably.
A Java migration is versioned and ordered exactly like a SQL one. It has a version, a description, and an entry in the history table. The only difference is how the change is written.
Writing a Java migration
A Java migration is a class that extends BaseJavaMigration. Flyway finds it on the classpath, by default in the db.migration package. Flyway reads the version and description from the class name, using the same pattern as SQL files: V<version>__<description>.
package db.migration;
import java.sql.Statement;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
public class V3__Backfill_display_name extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
try (Statement st = context.getConnection().createStatement()) {
st.execute("UPDATE app_user SET display_name = username WHERE display_name IS NULL");
}
}
}This class is version 3 with the description Backfill display name. Flyway replaces the underscores in the description with spaces. The class sits after V2__add_display_name.sql, which created the display_name column, so the backfill runs once that column exists.
The Context object gives you the JDBC connection that Flyway is using. Use that connection and do not open your own. Flyway runs the migration inside a transaction on it, so a failure rolls back the changes where the database supports transactional DDL. Do not commit or close the connection yourself.
How it fits with SQL migrations
SQL and Java migrations share one version sequence. Flyway sorts them together, so V1 (SQL), V2 (SQL) and V3 (Java) run in that order.
The diagram below shows the order.
In the history table, the type column shows SQL for file migrations and JDBC for Java migrations. For Java migrations, the checksum is NULL unless the class implements getChecksum(). Without a checksum, Flyway cannot detect that you edited the class after it was applied. You can override the method to return a value you compute yourself.
Common errors
| Symptom | Likely cause |
|---|---|
| Migration is not picked up | The class is outside `db.migration`, or `spring.flyway.locations` does not include its package |
| Wrong version or description | The class name does not use the `V<version>__<description>` form with two underscores |
| Changes persist after a failure | The database does not support transactional DDL, or the code committed the connection |
| Edited class is not detected | No `getChecksum()` override, so there is nothing to compare |
If you keep Java migrations in a different package, add it to the locations:
spring:
flyway:
locations: classpath:db/migration,classpath:com/example/migrationsPractical advice
Keep Java migrations small and self-contained. Do not call Spring beans, repositories or JPA entities from them. Entities change over time, and a migration written against today's entity can break when the entity changes later. Use plain JDBC and write the SQL inside the migration, so it behaves the same way in a year.
Exercise
Why should a Java migration use the JDBC connection from Context and avoid JPA entities?
Show answer
The Context connection is the one Flyway manages, so the migration runs in Flyway's transaction and is recorded correctly. JPA entities change as the application evolves, so a migration that depends on them can break later, while plain JDBC with fixed SQL keeps behaving the same.