Add support for customizing Flyway's configuration
Closes gh-14786
This commit is contained in:
parent
e789bc0bb7
commit
d37df3d718
|
|
@ -116,6 +116,8 @@ public class FlywayAutoConfiguration {
|
|||
|
||||
private final FlywayMigrationStrategy migrationStrategy;
|
||||
|
||||
private final List<FlywayConfigurationCustomizer> configurationCustomizers;
|
||||
|
||||
private final List<Callback> callbacks;
|
||||
|
||||
private final List<FlywayCallback> flywayCallbacks;
|
||||
|
|
@ -125,6 +127,7 @@ public class FlywayAutoConfiguration {
|
|||
ObjectProvider<DataSource> dataSource,
|
||||
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
|
||||
ObjectProvider<FlywayMigrationStrategy> migrationStrategy,
|
||||
ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
|
||||
ObjectProvider<Callback> callbacks,
|
||||
ObjectProvider<FlywayCallback> 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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue