|
|
|
@ -24,7 +24,11 @@ import java.util.Map;
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
import org.flywaydb.core.Flyway;
|
|
|
|
|
import org.flywaydb.core.api.Location;
|
|
|
|
|
import org.flywaydb.core.api.MigrationVersion;
|
|
|
|
|
import org.flywaydb.core.api.callback.Callback;
|
|
|
|
|
import org.flywaydb.core.api.callback.Context;
|
|
|
|
|
import org.flywaydb.core.api.callback.Event;
|
|
|
|
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
|
|
|
|
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
@ -48,6 +52,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.BDDMockito.given;
|
|
|
|
|
import static org.mockito.Mockito.inOrder;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
|
|
|
@ -62,6 +67,7 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
* @author Dominic Gunn
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
public class FlywayAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
|
|
@ -137,7 +143,7 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
assertThat(flyway.getLocations())
|
|
|
|
|
.containsExactly("classpath:db/migration");
|
|
|
|
|
.containsExactly(new Location("classpath:db/migration"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -150,7 +156,8 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
assertThat(flyway.getLocations()).containsExactly(
|
|
|
|
|
"classpath:db/changelog", "classpath:db/migration");
|
|
|
|
|
new Location("classpath:db/changelog"),
|
|
|
|
|
new Location("classpath:db/migration"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -163,7 +170,8 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
assertThat(flyway.getLocations()).containsExactly(
|
|
|
|
|
"classpath:db/changelog", "classpath:db/migration");
|
|
|
|
|
new Location("classpath:db/changelog"),
|
|
|
|
|
new Location("classpath:db/migration"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -278,7 +286,8 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
assertThat(flyway.getLocations()).containsExactlyInAnyOrder(
|
|
|
|
|
"classpath:db/vendors/h2", "classpath:db/changelog");
|
|
|
|
|
new Location("classpath:db/vendors/h2"),
|
|
|
|
|
new Location("classpath:db/changelog"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -291,7 +300,7 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
assertThat(flyway.getLocations())
|
|
|
|
|
.containsExactly("classpath:db/vendors/h2");
|
|
|
|
|
.containsExactly(new Location("classpath:db/vendors/h2"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -301,14 +310,31 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
CallbackConfiguration.class).run((context) -> {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
FlywayCallback callbackOne = context.getBean("callbackOne",
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
FlywayCallback callbackTwo = context.getBean("callbackTwo",
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
Callback callbackOne = context.getBean("callbackOne", Callback.class);
|
|
|
|
|
Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
|
|
|
|
|
assertThat(flyway.getCallbacks()).hasSize(2);
|
|
|
|
|
assertThat(flyway.getCallbacks()).containsExactly(callbackTwo,
|
|
|
|
|
callbackOne);
|
|
|
|
|
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
|
|
|
|
|
orderedCallbacks.verify(callbackTwo).handle(any(Event.class),
|
|
|
|
|
any(Context.class));
|
|
|
|
|
orderedCallbacks.verify(callbackOne).handle(any(Event.class),
|
|
|
|
|
any(Context.class));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void legacyCallbacksAreConfiguredAndOrdered() {
|
|
|
|
|
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
|
|
|
|
|
LegacyCallbackConfiguration.class).run((context) -> {
|
|
|
|
|
assertThat(context).hasSingleBean(Flyway.class);
|
|
|
|
|
Flyway flyway = context.getBean(Flyway.class);
|
|
|
|
|
FlywayCallback callbackOne = context.getBean("legacyCallbackOne",
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
FlywayCallback callbackTwo = context.getBean("legacyCallbackTwo",
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
assertThat(flyway.getCallbacks()).hasSize(2);
|
|
|
|
|
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
|
|
|
|
|
orderedCallbacks.verify(callbackTwo)
|
|
|
|
|
.beforeMigrate(any(Connection.class));
|
|
|
|
|
orderedCallbacks.verify(callbackOne)
|
|
|
|
@ -316,6 +342,19 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void callbacksAndLegacyCallbacksCannotBeMixed() {
|
|
|
|
|
this.contextRunner
|
|
|
|
|
.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
|
|
|
|
|
LegacyCallbackConfiguration.class, CallbackConfiguration.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
assertThat(context).hasFailed();
|
|
|
|
|
assertThat(context.getStartupFailure()).hasMessageContaining(
|
|
|
|
|
"Found a mixture of Callback and FlywayCallback beans."
|
|
|
|
|
+ " One type must be used exclusively.");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
protected static class FlywayDataSourceConfiguration {
|
|
|
|
|
|
|
|
|
@ -395,13 +434,37 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Order(1)
|
|
|
|
|
public FlywayCallback callbackOne() {
|
|
|
|
|
public Callback callbackOne() {
|
|
|
|
|
return mockCallback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Order(0)
|
|
|
|
|
public Callback callbackTwo() {
|
|
|
|
|
return mockCallback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Callback mockCallback() {
|
|
|
|
|
Callback callback = mock(Callback.class);
|
|
|
|
|
given(callback.supports(any(Event.class), any(Context.class)))
|
|
|
|
|
.willReturn(true);
|
|
|
|
|
return callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
static class LegacyCallbackConfiguration {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Order(1)
|
|
|
|
|
public FlywayCallback legacyCallbackOne() {
|
|
|
|
|
return mock(FlywayCallback.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Order(0)
|
|
|
|
|
public FlywayCallback callbackTwo() {
|
|
|
|
|
public FlywayCallback legacyCallbackTwo() {
|
|
|
|
|
return mock(FlywayCallback.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|