Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build flyway java-based migrations

Tags:

java

flyway

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.

like image 529
Vadym Kovalenko Avatar asked Nov 04 '15 15:11

Vadym Kovalenko


People also ask

What is Java based migration Flyway?

Java-based migrations are a great fit for all changes that can not easily be expressed using SQL. These would typically be things like. BLOB & CLOB changes. Advanced bulk data changes (Recalculations, advanced format changes, …)

Does Flyway require Java?

Zero required dependencies All you need is Java 7+ and your Jdbc driver and you're good to go!


2 Answers

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
like image 151
Axel Fontaine Avatar answered Sep 30 '22 10:09

Axel Fontaine


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

like image 24
Ronald Coarite Avatar answered Sep 30 '22 09:09

Ronald Coarite