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.
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, …)
Zero required dependencies All you need is Java 7+ and your Jdbc driver and you're good to go!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With