Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.FlywayCallback;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand Down Expand Up @@ -58,6 +59,7 @@
* @author Vedran Pavic
* @author Stephane Nicoll
* @author Jacques-Etienne Beaudet
* @author Eddú Meléndez
* @since 1.1.0
*/
@Configuration
Expand Down Expand Up @@ -89,16 +91,20 @@ public static class FlywayConfiguration {

private final FlywayMigrationStrategy migrationStrategy;

private FlywayCallback[] flywayCallbacks;

public FlywayConfiguration(FlywayProperties properties,
ResourceLoader resourceLoader,
ObjectProvider<DataSource> dataSourceProvider,
@FlywayDataSource ObjectProvider<DataSource> flywayDataSourceProvider,
ObjectProvider<FlywayMigrationStrategy> migrationStrategyProvider) {
ObjectProvider<FlywayMigrationStrategy> migrationStrategyProvider,
ObjectProvider<FlywayCallback[]> flywayCallbacks) {
this.properties = properties;
this.resourceLoader = resourceLoader;
this.dataSource = dataSourceProvider.getIfUnique();
this.flywayDataSource = flywayDataSourceProvider.getIfAvailable();
this.migrationStrategy = migrationStrategyProvider.getIfAvailable();
this.flywayCallbacks = flywayCallbacks.getIfAvailable();
}

@PostConstruct
Expand Down Expand Up @@ -138,6 +144,9 @@ else if (this.flywayDataSource != null) {
else {
flyway.setDataSource(this.dataSource);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually need this. We use it to not execute the migrations on startup by providing a noop FlywayMigrationStrategy as described in #4295.

if (this.flywayCallbacks != null && this.flywayCallbacks.length > 0) {
flyway.setCallbacks(this.flywayCallbacks);
}
flyway.setLocations(this.properties.getLocations().toArray(new String[0]));
return flyway;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.BaseFlywayCallback;
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -56,6 +57,7 @@
* @author Phillip Webb
* @author Andy Wilkinson
* @author Vedran Pavic
* @author Eddú Meléndez
*/
public class FlywayAutoConfigurationTests {

Expand Down Expand Up @@ -200,6 +202,16 @@ public void customFlywayMigrationInitializer() throws Exception {
assertThat(initializer.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
}

@Test
public void customFlywayCallback() throws Exception {
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
MockOneFlywayCallback.class, MockTwoFlywayCallback.class);
assertThat(this.context.getBean(Flyway.class)).isNotNull();
this.context.getBean(MockOneFlywayCallback.class).assertCalled();
this.context.getBean(MockTwoFlywayCallback.class).assertCalled();
}

@Test
public void customFlywayWithJpa() throws Exception {
registerAndRefresh(CustomFlywayWithJpaConfiguration.class,
Expand Down Expand Up @@ -303,4 +315,34 @@ public void assertCalled() {
}
}

@Component
protected static class MockOneFlywayCallback
extends BaseFlywayCallback {

private boolean called = false;

public MockOneFlywayCallback() {
this.called = true;
}

public void assertCalled() {
assertThat(this.called).isTrue();
}
}

@Component
protected static class MockTwoFlywayCallback
extends BaseFlywayCallback {

private boolean called = false;

public MockTwoFlywayCallback() {
this.called = true;
}

public void assertCalled() {
assertThat(this.called).isTrue();
}
}

}
2 changes: 1 addition & 1 deletion spring-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<ehcache.version>2.10.1</ehcache.version>
<ehcache3.version>3.0.0</ehcache3.version>
<embedded-mongo.version>1.50.2</embedded-mongo.version>
<flyway.version>3.2.1</flyway.version>
<flyway.version>4.0</flyway.version>
<freemarker.version>2.3.23</freemarker.version>
<elasticsearch.version>2.2.2</elasticsearch.version>
<gemfire.version>8.2.0</gemfire.version>
Expand Down