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?
Format XML in Java
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
Do not write if (isAdmin == true) code in Java
Write a class in Kotlin and call it in Java
Topic Analysis (LDA) in Java
Importing Excel data in Java 2
NEologd preprocessing in Java neologdn-java
Change java encoding in windows
Java Stream API in 5 minutes
Cannot find javax.annotation.Generated in Java 11
Read standard input in Java
Implement Basic authentication in Java
Perl's grep-ish guy in Java
Find a subset in Java
Null-safe program in Java (Eclipse)
[Java] Get KClass in Java [Kotlin]
Create Azure Functions in Java
Import Excel data in Java
Implement math combinations in Java
JavaFX environment construction in Java 13
Enum Strategy pattern in Java
2 Implement simple parsing in Java
Read binary files in Java 2
Sftp (JSch wrapper) in Java
Check https connection in Java
Java in Visual Studio Code
Calculate prime numbers in Java
Implement Email Sending in Java
Measured parallel processing in Java
POST Json in Java ~ HttpURLConnection ~
Write rich text in Clojure
Define abstract methods in Java enum and write each behavior
First Java development in Eclipse
Json serialization / deserialization in Java 1.4
Implement functional quicksort in Java