7
votes

I have both SQL and Java based migrations. I am trying to use the Flyway callback hook to do something else right after the validation is done, but it is not catching this callback. From the documentation, it seems like it's as simple as the following.

Here is my file structure:

-java
--db
---migrations
----V1__apple   <----java based
--FruitShopFlywayCallback.java  <---- Callback class
-resources
--migrations
--- V1__orange.sql  <----sql based

My callback:

public class FruitShopFlywayCallback extends BaseFlywayCallback {
    @Override
    public void afterValidate(Connection dataConnection) {
        System.out.println("it worksssssssss");
    }
}

My thought was that once the migration is done, flyway was going to callback into this method. I was not sure what am I missing?

2
Did you register your callback in flyway? In java based migrations it should be done by method flyway.setCallbacks(FlywayCallback... callbacks).merz
I don't think so, I thought it was done automatically with the new update, so i am guessing i should set that before I do flyway.migrate()AirWick219
How do you perform migrations? As for me, the right way do it - init flyway -> setCallbacks -> validate -> migrate. Is there anything I've missed?merz
Yea ..thank you so much ... that did it .. I found an example here github.com/flyway/flyway/blob/…AirWick219
I've found the same :) Good luck!merz

2 Answers

4
votes

I just needed to register the call back when initializing flyway. Here is what i did. After that. it works as expected

// Initializing Flyway
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);

flyway.setValidateOnMigrate(true);

// Register call back. 
FruitShopFlywayCallback callback = new FruitShopFlywayCallback();
flyway.setCallbacks(callback);
3
votes

In case this is helpful. I was looking for how to configure Flyway to work with Java callbacks using Maven. You need to register your callback classes with Flyway (using Flyway with pure Java you would use setCallbacks).

In maven this looks something like this:

<plugin>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-maven-plugin</artifactId>
  <version>${flyway.version}</version>
  <configuration>
    <driver>org.hsqldb.jdbcDriver</driver>
    <url>jdbc:hsqldb:file:${project.build.directory}/db/flyway_sample;shutdown=true</url>
    <user>SA</user>
    <callbacks>
      <callback>example.MyCallback</callback>
    </callbacks>
  </configuration>
</plugin>