Barney
Barney

Reputation: 915

Flyway: prefix for Java migrations

I have a composite JAR with SQL + Java migrations. For backward compatibility I have to distinguish between a new installation and an update. I use prefixes "I" (new installation) and "V" (update):

configuration.sqlMigrationPrefix("I");

This works fine for SQL migrations, but for a Java migration class I1_1__test.class I get the error:

org.flywaydb.core.api.FlywayException: Invalid JDBC migration class name: db.migration.test.I1_1__test=> ensure it starts with V or R, or implement org.flywaydb.core.api.migration.MigrationInfoProvider for non-default naming

In the documentation, there is a mention of a possibility to implement my own MigrationResolver coupled with a custom MigrationExecutor.

The problem is, I still need to keep the SqlMigrationResolver active, and I find very exhausting to re-implement both strategies (SQL and Java) almost from scratch just because of this.

Is there a better and simpler way?

PS: there is an old pull request, unfortunately not merged, for this feature.

Upvotes: 1

Views: 791

Answers (1)

ttulka
ttulka

Reputation: 10882

One way, how to achieve this with no coding whatsoever, would be to put different types of migrations onto different classpath locations:

src/main/
    /java/
        /my/comp/db/migration/
            /install/
                V1_1__Test1.java
            /update/
                V1_2__Test2.java 
    /resources/
        /my/comp/db/migration/
            /install/
                V1.1__Test1.sql
            /update/
                V1.2__Test2.sql

And then set the configuration:

Configuration config = Flyway.configure();
...

if (isInstall())  {
    config.locations("classpath:my/comp/db/migration/install");
} else {
    config.locations("classpath:my/comp/db/migration/update");
}

For consistence, I would recommend to treat both SQL and Java migrations in the same manner, but you can keep the prefix-based control as well:

...
src/main/resources/my/comp/db/migration/sql/
    I1.1__Test1.sql
    V1.2__Test2.sql

...
if (isInstall())  {
    config.locations(
        "classpath:my/comp/db/migration/sql",
        "classpath:my/comp/db/migration/install");
    config.sqlMigrationPrefix("I");
} else {
    config.locations(
        "classpath:my/comp/db/migration/sql",
        "classpath:my/comp/db/migration/update");
    //config.sqlMigrationPrefix("V"); -- this is default
}

Upvotes: 1

Related Questions