Want to deploy your JVM, Node.js and Go apps effortlessly to AWS? Try our service Boxfuse

Java-based migrations

Typical usage

  • BLOB & CLOB changes
  • Advanced bulk data changes (Recalculations, advanced format changes, ...)

Location and discovery

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.

Naming

In order to be picked up, the Java Migrations must implement JdbcMigration.

A Java Migration automatically

  • wraps the migration in a transaction
  • extracts the version and the description from the class name

The class name consists of:

  • prefix: Always V for versioned migrations, R for repeatable migrations
  • version: (Versioned migrations only) Underscores separate as many parts as you like
  • separator: Always __ (Two underscores)
  • description: Underscores separate the words

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.

Checksums and Validation

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.

Sample Class

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();
        }
    }
}

Spring support (Optional)

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')");
    }
}

Callbacks