This brief tutorial will teach how to use callbacks. It will take you through the steps on how to create and use them.
Callbacks let you hook into MigrateDB’s lifecycle. This is particularly useful when you execute the same housekeeping action over and over again.
They are typically used for
VACUUM
for PostgreSQL for example)Now let’s create a callback to flush all data to disk before a migration run. To do so, we’ll make use of MigrateDB’s
beforeMigrate
callback.
So go ahead and create beforeMigrate.sql
in the /sql
directory:
CHECKPOINT SYNC;
To trigger the execution of the callback, we’ll migrate the database again.
So go ahead and invoke migrate
.
This will give you the following result:
Database: jdbc:h2:file:./foobardb (H2 1.4) Successfully validated 2 migrations (execution time 00:00.010s) Executing SQL callback: beforeMigrate Creating Schema History table: "PUBLIC"."migratedb_state" 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.
In this brief tutorial we saw how to