Merge pull request #11316 from chaos2418/JIRA-7788

updating the version of flyway-core and changing the callback implementation
This commit is contained in:
kwoyke 2021-10-12 13:52:44 +02:00 committed by GitHub
commit ca519cbd88
3 changed files with 45 additions and 34 deletions

View File

@ -64,7 +64,7 @@
</build> </build>
<properties> <properties>
<flyway-core.version>5.2.3</flyway-core.version> <flyway-core.version>8.0.0</flyway-core.version>
<flyway-maven-plugin.version>8.0.0</flyway-maven-plugin.version> <flyway-maven-plugin.version>8.0.0</flyway-maven-plugin.version>
</properties> </properties>

View File

@ -1,33 +1,40 @@
package com.baeldung.flywaycallbacks; package com.baeldung.flywaycallbacks;
import java.sql.Connection;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.flywaydb.core.api.MigrationInfo; import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.BaseFlywayCallback; 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 @Override
public void afterEachMigrate(Connection connection, MigrationInfo info) { public boolean supports(Event event, Context context) {
log.info("> afterEachMigrate"); return event == Event.AFTER_EACH_MIGRATE || event == Event.AFTER_MIGRATE || event == Event.BEFORE_EACH_MIGRATE || event == Event.BEFORE_MIGRATE;
} }
@Override @Override
public void afterMigrate(Connection connection) { public boolean canHandleInTransaction(Event event, Context context) {
log.info("> afterMigrate"); return true;
} }
@Override @Override
public void beforeEachMigrate(Connection connection, MigrationInfo info) { public void handle(Event event, Context context) {
log.info("> beforeEachMigrate"); 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 @Override
public void beforeMigrate(Connection connection) { public String getCallbackName() {
log.info("> beforeMigrate"); return ExampleFlywayCallback.class.getSimpleName();
} }
} }

View File

@ -18,7 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@ContextConfiguration(classes = FlywayCallbackTestConfig.class) @ContextConfiguration(classes = FlywayCallbackTestConfig.class)
public class FlywayApplicationUnitTest { public class FlywayApplicationUnitTest {
private Log log = LogFactory.getLog(getClass()); private final Log log = LogFactory.getLog(getClass());
@Autowired @Autowired
private DataSource dataSource; private DataSource dataSource;
@ -26,43 +26,47 @@ public class FlywayApplicationUnitTest {
@Test @Test
public void migrateWithNoCallbacks() { public void migrateWithNoCallbacks() {
logTestBoundary("migrateWithNoCallbacks"); logTestBoundary("migrateWithNoCallbacks");
Flyway flyway = new Flyway(); Flyway flyway = Flyway.configure()
flyway.setDataSource(dataSource); .dataSource(dataSource)
flyway.setLocations("db/migration"); .locations("db/migration")
flyway.migrate(); .load();
flyway.migrate();
} }
@Test @Test
public void migrateWithJavaCallbacks() { public void migrateWithJavaCallbacks() {
logTestBoundary("migrateWithJavaCallbacks"); logTestBoundary("migrateWithJavaCallbacks");
Flyway flyway = new Flyway(); Flyway flyway = Flyway.configure()
flyway.setDataSource(dataSource); .dataSource(dataSource)
flyway.setLocations("db/migration"); .locations("db/migration")
flyway.setCallbacks(new ExampleFlywayCallback()); .callbacks(new ExampleFlywayCallback())
.load();
flyway.migrate(); flyway.migrate();
} }
@Test @Test
public void migrateWithSqlCallbacks() { public void migrateWithSqlCallbacks() {
logTestBoundary("migrateWithSqlCallbacks"); logTestBoundary("migrateWithSqlCallbacks");
Flyway flyway = new Flyway(); Flyway flyway = Flyway.configure()
flyway.setDataSource(dataSource); .dataSource(dataSource)
flyway.setLocations("db/migration", "db/callbacks"); .locations("db/migration", "db/callbacks")
flyway.migrate(); .load();
flyway.migrate();
} }
@Test @Test
public void migrateWithSqlAndJavaCallbacks() { public void migrateWithSqlAndJavaCallbacks() {
logTestBoundary("migrateWithSqlAndJavaCallbacks"); logTestBoundary("migrateWithSqlAndJavaCallbacks");
Flyway flyway = new Flyway(); Flyway flyway = Flyway.configure()
flyway.setDataSource(dataSource); .dataSource(dataSource)
flyway.setLocations("db/migration", "db/callbacks"); .locations("db/migration", "db/callbacks")
flyway.setCallbacks(new ExampleFlywayCallback()); .callbacks(new ExampleFlywayCallback())
flyway.migrate(); .load();
flyway.migrate();
} }
private void logTestBoundary(String testName) { private void logTestBoundary(String testName) {
System.out.println("\n"); System.out.println("\n");
log.info("> " + testName); log.info("> " + testName);
} }
} }