diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index f0b24e1e869..b8d329e3d89 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -116,6 +116,8 @@ public class FlywayAutoConfiguration { private final FlywayMigrationStrategy migrationStrategy; + private final List configurationCustomizers; + private final List callbacks; private final List flywayCallbacks; @@ -125,6 +127,7 @@ public class FlywayAutoConfiguration { ObjectProvider dataSource, @FlywayDataSource ObjectProvider flywayDataSource, ObjectProvider migrationStrategy, + ObjectProvider fluentConfigurationCustomizers, ObjectProvider callbacks, ObjectProvider flywayCallbacks) { this.properties = properties; @@ -133,6 +136,8 @@ public class FlywayAutoConfiguration { this.dataSource = dataSource.getIfUnique(); this.flywayDataSource = flywayDataSource.getIfAvailable(); this.migrationStrategy = migrationStrategy.getIfAvailable(); + this.configurationCustomizers = fluentConfigurationCustomizers.orderedStream() + .collect(Collectors.toList()); this.callbacks = callbacks.orderedStream().collect(Collectors.toList()); this.flywayCallbacks = flywayCallbacks.orderedStream() .collect(Collectors.toList()); @@ -145,6 +150,8 @@ public class FlywayAutoConfiguration { checkLocationExists(dataSource); configureProperties(configuration); configureCallbacks(configuration); + this.configurationCustomizers + .forEach((customizer) -> customizer.customize(configuration)); Flyway flyway = configuration.load(); configureFlywayCallbacks(flyway); return flyway; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayConfigurationCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayConfigurationCustomizer.java new file mode 100644 index 00000000000..991a226d251 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayConfigurationCustomizer.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.flyway; + +import org.flywaydb.core.api.configuration.FluentConfiguration; + +/** + * Callback interface that can be implemented by beans wishing to customize the flyway + * configuration. + * + * @author Stephane Nicoll + * @since 2.1.0 + */ +@FunctionalInterface +public interface FlywayConfigurationCustomizer { + + /** + * Customize the flyway configuration. + * @param configuration the {@link FluentConfiguration} to customize + */ + void customize(FluentConfiguration configuration); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 8329c03d24e..bb1bf169ddf 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -363,6 +363,21 @@ public class FlywayAutoConfigurationTests { }); } + @Test + public void configurationCustomizersAreConfiguredAndOrdered() { + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, + ConfigurationCustomizerConfiguration.class).run((context) -> { + assertThat(context).hasSingleBean(Flyway.class); + Flyway flyway = context.getBean(Flyway.class); + assertThat(flyway.getConfiguration().getConnectRetries()) + .isEqualTo(5); + assertThat(flyway.getConfiguration().isIgnoreMissingMigrations()) + .isTrue(); + assertThat(flyway.getConfiguration().isIgnorePendingMigrations()) + .isTrue(); + }); + } + @Configuration protected static class FlywayDataSourceConfiguration { @@ -478,4 +493,23 @@ public class FlywayAutoConfigurationTests { } + @Configuration + static class ConfigurationCustomizerConfiguration { + + @Bean + @Order(1) + public FlywayConfigurationCustomizer customizerOne() { + return (configuration) -> configuration.connectRetries(5) + .ignorePendingMigrations(true); + } + + @Bean + @Order(0) + public FlywayConfigurationCustomizer customizerTwo() { + return (configuration) -> configuration.connectRetries(10) + .ignoreMissingMigrations(true); + } + + } + } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 0dad0de8136..c470e562998 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -2329,7 +2329,9 @@ of supported databases is available in {sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[`FlywayProperties`] provides most of Flyway's settings and a small set of additional properties that can be -used to disable the migrations or switch off the location checking. +used to disable the migrations or switch off the location checking. If you need more +control over the configuration, consider registering a `FlywayConfigurationCustomizer` +bean. Spring Boot calls `Flyway.migrate()` to perform the database migration. If you would like more control, provide a `@Bean` that implements