Write flyway callbacks in Java


An app that uses Gradle and flyway for database migration I made a trial and error when I wrote the callback in Java.

The bottom line is that you should compile the Callback class before executing the flyway task.

Please refer to the following repository for the code verified this time. https://github.com/mahaker/flyway-callback-java


Java: OpenJDK 11 Gradle: 5.2.1 flyway: 6.4.2 Postgres: 12

Write the callback in Java

ʻOrg.flywaydb.core.api.callback.Callback` Create a class that implements the interface Set the flyway.callbacks property to a fully qualified name. https://flywaydb.org/documentation/api/hooks#java-based-callbacks

Also, make sure that the classes task is executed before the flyway task. * This is important </ b> (To be exact, is it a compile Java task?)


package db.migration;

import java.sql.PreparedStatement;

import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;

public class FillTestData implements Callback {

    public boolean supports(Event event, Context context) {
        return Event.AFTER_MIGRATE.equals(event);

    public boolean canHandleInTransaction(Event event, Context context) {
        return false;

    public void handle(Event event, Context context) {
        try (
            final PreparedStatement statement = context
                .prepareStatement("INSERT INTO PERSON(ID, NAME, AGE) VALUES (1, 'userA', 20), (2, 'userB', 22)")
        ) {
        } catch (Exception e) {


//Excerpt only what you need
plugins {
    id "org.flywaydb.flyway" version "6.4.2"

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

flyway {
    url = 'jdbc:postgresql://localhost:15432/exampledb'
    user = 'postgres'
    password = 'example'
    locations = [ 'filesystem:./src/main/resources/db/migration/' ]
    callbacks = [ 'db.migration.FillTestData' ]

// !!Important! !!


When you execute the migration, you can see that the insert statement has been executed.

This method was taught by a person inside the flyway. https://github.com/flyway/flyway/issues/2829

Recommended Posts

Write flyway callbacks in Java
Write Java8-like code in Java8
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
How to write Java String # getBytes in Kotlin?
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
[Java] DB migration (Flyway)
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Write ABNF in Java and pass the email address
Write a class that can be ordered in Java
Do not write if (isAdmin == true) code in Java
Write a class in Kotlin and call it in Java
Try using RocksDB in Java
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world
Write class inheritance in Ruby
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Write Processing in IntelliJ IDEA
How to write java comments
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Topic Analysis (LDA) in Java