Java migrations reside on the classpath in the package referenced by the locations property.
New java migrations are discovered automatically through classpath scanning at runtime. The
scanning is recursive. Java migrations in subpackages of the specified ones are also picked up.
In order to be picked up, the Java Migrations must implement JdbcMigration.
A Java Migration automatically
The class name consists of:
V
for versioned migrations, R
for repeatable migrations__
(Two underscores)If you need more control over the class name, you can override the default convention by implementing MigrationInfoProvider.
This will allow you to name your class as you wish. Version and description are provided by implement the
respective methods.
Unlike SQL migrations, Java migrations by default do not have a checksum and therefore do not participate in the change detection of Flyway's validation. This can be remedied by implementing MigrationChecksumProvider.
By implementing the getChecksum() method you can provide your own checksum, which will then be stored and
validated for changes.
package db.migration; import org.flywaydb.core.api.migration.jdbc.JdbcMigration; import java.sql.Connection; import java.sql.PreparedStatement; /** * Example of a Java-based migration. */ public class V1_2__Another_user implements JdbcMigration { public void migrate(Connection connection) throws Exception { PreparedStatement statement = connection.prepareStatement("INSERT INTO test_user (name) VALUES ('Obelix')"); try { statement.execute(); } finally { statement.close(); } } }
If you use Spring, you can choose to implement SpringJdbcMigration instead. This works exactly like JdbcMigration, the only difference being that you now receive a convenient Spring JdbcTemplate to work with instead of a plain Jdbc Connection.
package db.migration; import org.flywaydb.core.api.migration.spring.SpringJdbcMigration; import org.springframework.jdbc.core.JdbcTemplate; /** * Example of a Spring Jdbc migration. */ public class V1_2__Another_user implements SpringJdbcMigration { public void migrate(JdbcTemplate jdbcTemplate) throws Exception { jdbcTemplate.execute("INSERT INTO test_user (name) VALUES ('Obelix')"); } }