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 a4f15eaf61..9b768756ca 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 @@ -141,6 +141,12 @@ public class FlywayAutoConfiguration { return new PropertiesFlywayConnectionDetails(this.properties); } + @Bean + @ConditionalOnClass(name = "org.flywaydb.database.sqlserver.SQLServerConfigurationExtension") + SqlServerFlywayConfigurationCustomizer sqlServerFlywayConfigurationCustomizer() { + return new SqlServerFlywayConfigurationCustomizer(this.properties); + } + @Bean @ConditionalOnClass(name = "org.flywaydb.database.oracle.OracleConfigurationExtension") OracleFlywayConfigurationCustomizer oracleFlywayConfigurationCustomizer() { @@ -267,10 +273,6 @@ public class FlywayAutoConfiguration { map.from(properties.getJdbcProperties()).whenNot(Map::isEmpty).to(configuration::jdbcProperties); map.from(properties.getKerberosConfigFile()).to(configuration::kerberosConfigFile); map.from(properties.getOutputQueryResults()).to(configuration::outputQueryResults); - map.from(properties.getSqlServerKerberosLoginFile()) - .whenNonNull() - .to((sqlServerKerberosLoginFile) -> configureSqlServerKerberosLoginFile(configuration, - sqlServerKerberosLoginFile)); map.from(properties.getSkipExecutingMigrations()).to(configuration::skipExecutingMigrations); map.from(properties.getIgnoreMigrationPatterns()) .whenNot(List::isEmpty) @@ -289,14 +291,6 @@ public class FlywayAutoConfiguration { } } - private void configureSqlServerKerberosLoginFile(FluentConfiguration configuration, - String sqlServerKerberosLoginFile) { - SQLServerConfigurationExtension sqlServerConfigurationExtension = configuration.getPluginRegister() - .getPlugin(SQLServerConfigurationExtension.class); - Assert.state(sqlServerConfigurationExtension != null, "Flyway SQL Server extension missing"); - sqlServerConfigurationExtension.getKerberos().getLogin().setFile(sqlServerKerberosLoginFile); - } - private void configureCallbacks(FluentConfiguration configuration, List callbacks) { if (!callbacks.isEmpty()) { configuration.callbacks(callbacks.toArray(new Callback[0])); @@ -484,6 +478,31 @@ public class FlywayAutoConfiguration { } + @Order(Ordered.HIGHEST_PRECEDENCE) + static final class SqlServerFlywayConfigurationCustomizer implements FlywayConfigurationCustomizer { + + private final FlywayProperties properties; + + SqlServerFlywayConfigurationCustomizer(FlywayProperties properties) { + this.properties = properties; + } + + @Override + public void customize(FluentConfiguration configuration) { + ConfigurationExtensionMapper map = new ConfigurationExtensionMapper<>( + PropertyMapper.get().alwaysApplyingWhenNonNull(), () -> { + SQLServerConfigurationExtension extension = configuration.getPluginRegister() + .getPlugin(SQLServerConfigurationExtension.class); + Assert.notNull(extension, "Flyway SQL Server extension missing"); + return extension; + }); + + map.apply(this.properties.getSqlServerKerberosLoginFile(), + (extension, file) -> extension.getKerberos().getLogin().setFile(file)); + } + + } + static class ConfigurationExtensionMapper { private final PropertyMapper map; 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 27c947d609..62c03c62b6 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 @@ -52,6 +52,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.FlywayAutoConfigurationRuntimeHints; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.OracleFlywayConfigurationCustomizer; +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.SqlServerFlywayConfigurationCustomizer; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; @@ -732,6 +733,13 @@ class FlywayAutoConfigurationTests { .run(validateFlywayTeamsPropertyOnly("outputQueryResults")); } + @Test + void sqlServerExtensionIsNotLoadedByDefault() { + FluentConfiguration configuration = mock(FluentConfiguration.class); + new SqlServerFlywayConfigurationCustomizer(new FlywayProperties()).customize(configuration); + then(configuration).shouldHaveNoInteractions(); + } + @Test void sqlServerKerberosLoginFileIsCorrectlyMapped() { this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)