updating the version of flyway-core and changing the callback implementation.

This commit is contained in:
Shashank 2021-10-11 21:32:52 +05:30
parent 81cc62aac8
commit 28a9519e8e
3 changed files with 45 additions and 34 deletions

View File

@ -64,7 +64,7 @@
</build>
<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>
</properties>

View File

@ -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();
}
}

View File

@ -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);
}
}
}