From 28a9519e8e06e65ce62d6417f8ff8b58f1436246 Mon Sep 17 00:00:00 2001 From: Shashank Date: Mon, 11 Oct 2021 21:32:52 +0530 Subject: [PATCH] updating the version of flyway-core and changing the callback implementation. --- persistence-modules/flyway/pom.xml | 2 +- .../ExampleFlywayCallback.java | 35 +++++++++------- .../FlywayApplicationUnitTest.java | 42 ++++++++++--------- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/persistence-modules/flyway/pom.xml b/persistence-modules/flyway/pom.xml index 55dda3b915..a339a266e3 100644 --- a/persistence-modules/flyway/pom.xml +++ b/persistence-modules/flyway/pom.xml @@ -64,7 +64,7 @@ - 5.2.3 + 8.0.0 8.0.0 diff --git a/persistence-modules/flyway/src/main/java/com/baeldung/flywaycallbacks/ExampleFlywayCallback.java b/persistence-modules/flyway/src/main/java/com/baeldung/flywaycallbacks/ExampleFlywayCallback.java index 6ad4ce510c..68e54fd721 100644 --- a/persistence-modules/flyway/src/main/java/com/baeldung/flywaycallbacks/ExampleFlywayCallback.java +++ b/persistence-modules/flyway/src/main/java/com/baeldung/flywaycallbacks/ExampleFlywayCallback.java @@ -1,33 +1,40 @@ package com.baeldung.flywaycallbacks; -import java.sql.Connection; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.flywaydb.core.api.MigrationInfo; -import org.flywaydb.core.api.callback.BaseFlywayCallback; +import org.flywaydb.core.api.callback.Callback; +import org.flywaydb.core.api.callback.Context; +import org.flywaydb.core.api.callback.Event; -public class ExampleFlywayCallback extends BaseFlywayCallback { +public class ExampleFlywayCallback implements Callback { - private Log log = LogFactory.getLog(getClass()); + private final Log log = LogFactory.getLog(getClass()); @Override - public void afterEachMigrate(Connection connection, MigrationInfo info) { - log.info("> afterEachMigrate"); + public boolean supports(Event event, Context context) { + return event == Event.AFTER_EACH_MIGRATE || event == Event.AFTER_MIGRATE || event == Event.BEFORE_EACH_MIGRATE || event == Event.BEFORE_MIGRATE; } @Override - public void afterMigrate(Connection connection) { - log.info("> afterMigrate"); + public boolean canHandleInTransaction(Event event, Context context) { + return true; } @Override - public void beforeEachMigrate(Connection connection, MigrationInfo info) { - log.info("> beforeEachMigrate"); + public void handle(Event event, Context context) { + if (event == Event.AFTER_EACH_MIGRATE) { + log.info("> afterEachMigrate"); + } else if (event == Event.AFTER_MIGRATE) { + log.info("> afterMigrate"); + } else if (event == Event.BEFORE_EACH_MIGRATE) { + log.info("> beforeEachMigrate"); + } else if (event == Event.BEFORE_MIGRATE) { + log.info("> beforeMigrate"); + } } @Override - public void beforeMigrate(Connection connection) { - log.info("> beforeMigrate"); + public String getCallbackName() { + return ExampleFlywayCallback.class.getSimpleName(); } } diff --git a/persistence-modules/flyway/src/test/java/com/baeldung/flywaycallbacks/FlywayApplicationUnitTest.java b/persistence-modules/flyway/src/test/java/com/baeldung/flywaycallbacks/FlywayApplicationUnitTest.java index bf30ce604d..796f83e9e0 100644 --- a/persistence-modules/flyway/src/test/java/com/baeldung/flywaycallbacks/FlywayApplicationUnitTest.java +++ b/persistence-modules/flyway/src/test/java/com/baeldung/flywaycallbacks/FlywayApplicationUnitTest.java @@ -18,7 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner; @ContextConfiguration(classes = FlywayCallbackTestConfig.class) public class FlywayApplicationUnitTest { - private Log log = LogFactory.getLog(getClass()); + private final Log log = LogFactory.getLog(getClass()); @Autowired private DataSource dataSource; @@ -26,43 +26,47 @@ public class FlywayApplicationUnitTest { @Test public void migrateWithNoCallbacks() { logTestBoundary("migrateWithNoCallbacks"); - Flyway flyway = new Flyway(); - flyway.setDataSource(dataSource); - flyway.setLocations("db/migration"); - flyway.migrate(); + Flyway flyway = Flyway.configure() + .dataSource(dataSource) + .locations("db/migration") + .load(); + flyway.migrate(); } @Test public void migrateWithJavaCallbacks() { logTestBoundary("migrateWithJavaCallbacks"); - Flyway flyway = new Flyway(); - flyway.setDataSource(dataSource); - flyway.setLocations("db/migration"); - flyway.setCallbacks(new ExampleFlywayCallback()); + Flyway flyway = Flyway.configure() + .dataSource(dataSource) + .locations("db/migration") + .callbacks(new ExampleFlywayCallback()) + .load(); flyway.migrate(); } @Test public void migrateWithSqlCallbacks() { logTestBoundary("migrateWithSqlCallbacks"); - Flyway flyway = new Flyway(); - flyway.setDataSource(dataSource); - flyway.setLocations("db/migration", "db/callbacks"); - flyway.migrate(); + Flyway flyway = Flyway.configure() + .dataSource(dataSource) + .locations("db/migration", "db/callbacks") + .load(); + flyway.migrate(); } @Test public void migrateWithSqlAndJavaCallbacks() { logTestBoundary("migrateWithSqlAndJavaCallbacks"); - Flyway flyway = new Flyway(); - flyway.setDataSource(dataSource); - flyway.setLocations("db/migration", "db/callbacks"); - flyway.setCallbacks(new ExampleFlywayCallback()); - flyway.migrate(); + Flyway flyway = Flyway.configure() + .dataSource(dataSource) + .locations("db/migration", "db/callbacks") + .callbacks(new ExampleFlywayCallback()) + .load(); + flyway.migrate(); } private void logTestBoundary(String testName) { System.out.println("\n"); log.info("> " + testName); } - } +}