Skip to content

Commit d37df3d

Browse files
committed
Add support for customizing Flyway's configuration
Closes gh-14786
1 parent e789bc0 commit d37df3d

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public static class FlywayConfiguration {
116116

117117
private final FlywayMigrationStrategy migrationStrategy;
118118

119+
private final List<FlywayConfigurationCustomizer> configurationCustomizers;
120+
119121
private final List<Callback> callbacks;
120122

121123
private final List<FlywayCallback> flywayCallbacks;
@@ -125,6 +127,7 @@ public FlywayConfiguration(FlywayProperties properties,
125127
ObjectProvider<DataSource> dataSource,
126128
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
127129
ObjectProvider<FlywayMigrationStrategy> migrationStrategy,
130+
ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
128131
ObjectProvider<Callback> callbacks,
129132
ObjectProvider<FlywayCallback> flywayCallbacks) {
130133
this.properties = properties;
@@ -133,6 +136,8 @@ public FlywayConfiguration(FlywayProperties properties,
133136
this.dataSource = dataSource.getIfUnique();
134137
this.flywayDataSource = flywayDataSource.getIfAvailable();
135138
this.migrationStrategy = migrationStrategy.getIfAvailable();
139+
this.configurationCustomizers = fluentConfigurationCustomizers.orderedStream()
140+
.collect(Collectors.toList());
136141
this.callbacks = callbacks.orderedStream().collect(Collectors.toList());
137142
this.flywayCallbacks = flywayCallbacks.orderedStream()
138143
.collect(Collectors.toList());
@@ -145,6 +150,8 @@ public Flyway flyway() {
145150
checkLocationExists(dataSource);
146151
configureProperties(configuration);
147152
configureCallbacks(configuration);
153+
this.configurationCustomizers
154+
.forEach((customizer) -> customizer.customize(configuration));
148155
Flyway flyway = configuration.load();
149156
configureFlywayCallbacks(flyway);
150157
return flyway;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.flyway;
18+
19+
import org.flywaydb.core.api.configuration.FluentConfiguration;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to customize the flyway
23+
* configuration.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 2.1.0
27+
*/
28+
@FunctionalInterface
29+
public interface FlywayConfigurationCustomizer {
30+
31+
/**
32+
* Customize the flyway configuration.
33+
* @param configuration the {@link FluentConfiguration} to customize
34+
*/
35+
void customize(FluentConfiguration configuration);
36+
37+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,21 @@ public void callbacksAndLegacyCallbacksCannotBeMixed() {
363363
});
364364
}
365365

366+
@Test
367+
public void configurationCustomizersAreConfiguredAndOrdered() {
368+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
369+
ConfigurationCustomizerConfiguration.class).run((context) -> {
370+
assertThat(context).hasSingleBean(Flyway.class);
371+
Flyway flyway = context.getBean(Flyway.class);
372+
assertThat(flyway.getConfiguration().getConnectRetries())
373+
.isEqualTo(5);
374+
assertThat(flyway.getConfiguration().isIgnoreMissingMigrations())
375+
.isTrue();
376+
assertThat(flyway.getConfiguration().isIgnorePendingMigrations())
377+
.isTrue();
378+
});
379+
}
380+
366381
@Configuration
367382
protected static class FlywayDataSourceConfiguration {
368383

@@ -478,4 +493,23 @@ public FlywayCallback legacyCallbackTwo() {
478493

479494
}
480495

496+
@Configuration
497+
static class ConfigurationCustomizerConfiguration {
498+
499+
@Bean
500+
@Order(1)
501+
public FlywayConfigurationCustomizer customizerOne() {
502+
return (configuration) -> configuration.connectRetries(5)
503+
.ignorePendingMigrations(true);
504+
}
505+
506+
@Bean
507+
@Order(0)
508+
public FlywayConfigurationCustomizer customizerTwo() {
509+
return (configuration) -> configuration.connectRetries(10)
510+
.ignoreMissingMigrations(true);
511+
}
512+
513+
}
514+
481515
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,9 @@ of supported databases is available in
23292329

23302330
{sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[`FlywayProperties`]
23312331
provides most of Flyway's settings and a small set of additional properties that can be
2332-
used to disable the migrations or switch off the location checking.
2332+
used to disable the migrations or switch off the location checking. If you need more
2333+
control over the configuration, consider registering a `FlywayConfigurationCustomizer`
2334+
bean.
23332335

23342336
Spring Boot calls `Flyway.migrate()` to perform the database migration. If you would like
23352337
more control, provide a `@Bean` that implements

0 commit comments

Comments
 (0)