Vadym Kovalenko
Vadym Kovalenko

Reputation: 652

How to build flyway java-based migrations

I'm using flyway command-line tool to handle my db migrations. Till now all migrations are sql

Config file(only used options):

flyway.url=jdbc:postgresql://db.host
flyway.user=user
flyway.password=password

flyway.table=flyway_migrations

flyway.locations=filesystem:/home/........./sql/migrations

flyway.sqlMigrationPrefix=update
flyway.validateOnMigrate=false
flyway.outOfOrder=true

That works perfectly.

But for now I need to add one java-based migration. And I'm really puzzled I can't find any How todo examples. How to compile, where to put java migrations.

I've tried simple migration-class from official documentation:

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

But what to do next? Tried compiling:

javac -cp "./flyway-core-3.2.1.jar" V50_121_1__update_notes.java
jar cf V50_121_1__update_dataNode_notes.jar V50_121_1__update_dataNode_notes.class

And then putting that jar to different locations, have no effect.

flyway info - don't see the migration.

So how to build the simplest java-based migration. I will prefer not to use Maven, or something similar. Just simple jar file(???) which is picked up by flyway command-line tool.

Thank you.

Upvotes: 7

Views: 13015

Answers (2)

Ronald Coarite
Ronald Coarite

Reputation: 4726

Specify for .java files as .java como

classpath: package_example.migrations

flyway.url=jdbc:postgresql://db.host
flyway.user=user
flyway.password=password

flyway.table=flyway_migrations

flyway.locations=filesystem:/home/...../sql/migrations,classpath:pack_example.migrations

flyway.sqlMigrationPrefix=update
flyway.validateOnMigrate=false
flyway.outOfOrder=true

See the link for documentation https://flywaydb.org/documentation/commandline/migrate

Upvotes: 1

Axel Fontaine
Axel Fontaine

Reputation: 35169

Make sure your .class file is in a db/migration directory inside your .jar file file and that your .jar file is placed in the /jars directory of your Flyway installations.

flyway.locations should also be set to:

db.migration,filesystem:/home/........./sql/migrations

Upvotes: 4

Related Questions