Guenther
Guenther

Reputation: 2045

flyway4, repeatable migration, dynamic checksum change, maven, always execute

I have a repeatable flyway sql migration script, that I want executed everytime flyway:migrate is called.

R__Always_Executed.sql:

/* Version: ${timestamp} <- changes on each execution */
...

I define the placeholder timestamp in pom.xml:

<properties>
    <flyway.placeholders.timestamp>${maven.build.timestamp}</flyway.placeholders.timestamp>
</properties>

Each time I do

mvn clean install flyway:migrate

the timestamp changes, therefore the file contents and with it the checksum should be different. Hence the script should get executed. It does not, however.

Anyone any idea, why flyway doesn't consider the placeholder replacements when calculating the checksum?

Upvotes: 1

Views: 1777

Answers (3)

Miikka
Miikka

Reputation: 4653

Flyway calculates the migration checksum before the placeholders are replaced with actual values. This means ${timestamp} won't do the trick.

Instead of a repeatable migrations, you could use Flyway's callback mechanism. Create a SQL file called afterMigrate.sql and put it in the same directory as your migrations. It will be executed every time flyway:migrate is called. It's run after all the migrations have been executed – if you need to run it before the migrations, use beforeMigrate.sql.

Upvotes: 2

markdsievers
markdsievers

Reputation: 7319

It is because the default location being scanned in 4.x is

filesystem:src/main/resources/db/migration

Checkout the Improved compatibility with schema-first persistence frameworks section in the release notes. and also the locations entry in the Maven Plugin migrate documentation .

All your configuration is not present so it's hard to say what is wrong. Have you tried adding a timestamp tag in the <placeholders> section instead of the property based version? See the Maven Plugin migrate documentation for an example configuration of placeholders. eg

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.0.3</version>
    <configuration>
        <!-- ... -->
        <placeholders>
            <timestamp>${maven.build.timestamp}</timestamp>
        </placeholders>
    </configuration>
</plugin>

Upvotes: 0

Guenther
Guenther

Reputation: 2045

Ok, so I figured out a workaround. If I change the location to the target folder, flyway will execute the repeatable migration every time mvn flyway:migrate is called:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.0.3</version>
    <configuration>
        <!-- ... -->
        <locations>
            <location>filesystem:target/classes/db/migration</location>
        </locations>
    </configuration>
</plugin>

Btw., I checked my target folder. Maven is NOT replacing anything in the R__Always_Executed.sql file. It still looks like this

/* Version: ${timestamp} <- changes on each execution */
...

Maybe a bug in flyway, that it doesn't replace the variables for checksum calculation if the files are in the src folder? Hopefully Axel can comment on that.

Upvotes: 0

Related Questions