Tutorial - Create a beforeMigrate SQL callback

The following tutorial uses a disposable H2 database connection and associated SQL syntax (e.g. with JDBC URL jdbc:h2:file:./foobardb )


Creating a callback

Let's create a callback to flush all data to disk before a migration run. To do so, we'll make use of Flyway's beforeMigrate callback.

Make sure that you have the following setting configured in your toml configuration file:

callbackLocations = ["filesystem:callbacks"]

So go ahead and create beforeMigrate.sql in the /callbacks directory:

CHECKPOINT SYNC;

Triggering the callback

To trigger the execution of the callback, we'll clean and migrate our database.

So go ahead and invoke

flyway-11.12.0> flyway clean migrate

This will give you output similar to the following:

Database: jdbc:h2:file:./foobardb (H2 1.4)
Successfully cleaned schema "PUBLIC" (execution time 00:00.003s)
Successfully validated 2 migrations (execution time 00:00.010s)
Executing SQL callback: beforeMigrate
Creating Schema History table: "PUBLIC"."flyway_schema_history"
Current version of schema "PUBLIC": << Empty Schema >>
Migrating schema "PUBLIC" to version 1 - Create person table
Migrating schema "PUBLIC" to version 2 - Add people
Successfully applied 2 migrations to schema "PUBLIC" (execution time 00:00.034s)

As expected we can see that the beforeMigrate callback was triggered and executed successfully before the migrate operation. Each time you invoke migrate again in the future, the callback will now be executed again.

It is not yet possible to create callbacks within Flyway Desktop, although they will be honored if present.

Open the project folder on the filesystem and follow the same steps as for the command line, in order to create the callbacks.



Didn't find what you were looking for?