Richard
Richard

Reputation: 6116

Flyway Callback before Migration

I have a flyway project where I want to run a java callback to change the name of certain sql files before running the migration against the database. Unfortunately what's happening is the migration is running against the database first then the callback fires to change the file name.

Here's my callback class:

public class FooCallback extends BaseFlywayCallback {
    @Override
    public void beforeMigrate(final Connection connection) {
        //rename file from V_1_FooScript.sql to V_05172018_FooScript.sql
    }
}

But in the schema_version table I see the following entry:

description   type    script                checksum
FooScript     SQL     V_1__FooScript.sql    1473655428

It should say

description   type    script                      checksum
FooScript     SQL     V_05172018_FooScript.sql    1473655428

How do I get flyway to change the file name before running the migration?

Edit 1: So it looks like flyway is running the migration, then executing the callback to change the file name, then running the migration again since the file name changed but failing when trying to run the second time because script is creating a table so when it runs a 2nd time it just says "Foo table already exists".

Upvotes: 4

Views: 1275

Answers (1)

OptimalChoice
OptimalChoice

Reputation: 132

As others have commented, your question is not completely clear. Because flyway recursively discovers changes to the schema, changing the filename in the callback causes a second migration. I suspect that your History will show you that the first migration was prepared before the run, the second migration was prepared during your run, and then both were executed.

It seems that you are running an older version of Flyway, and that you are attempting to initialize a schema with a specific version number. If this is the case, you can avoid the callback and perform this via a config file as documented:

flyway.sqlMigrationPrefix File name prefix for versioned SQL migrations (default: V) Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,which using the defaults translates to V1_1__My_description.sql

Note that you have a single underline character preceding "FooScript.sql". To achieve this you will need to change the default sqlMigrationSeparator as well.

Upvotes: 1

Related Questions