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 ba4897d970..8d0484b373 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 @@ -145,10 +145,6 @@ public class FlywayAutoConfiguration { String user = getProperty(properties::getUser, dataSourceProperties::determineUsername); String password = getProperty(properties::getPassword, dataSourceProperties::determinePassword); configuration.dataSource(url, user, password); - if (!CollectionUtils.isEmpty(properties.getInitSqls())) { - String initSql = StringUtils.collectionToDelimitedString(properties.getInitSqls(), "\n"); - configuration.initSql(initSql); - } } else if (flywayDataSource != null) { configuration.dataSource(flywayDataSource); @@ -206,6 +202,9 @@ public class FlywayAutoConfiguration { map.from(properties.isSkipDefaultCallbacks()).to(configuration::skipDefaultCallbacks); map.from(properties.isSkipDefaultResolvers()).to(configuration::skipDefaultResolvers); map.from(properties.isValidateOnMigrate()).to(configuration::validateOnMigrate); + map.from(properties.getInitSqls()).whenNot(CollectionUtils::isEmpty) + .as((initSqls) -> StringUtils.collectionToDelimitedString(initSqls, "\n")) + .to(configuration::initSql); // Pro properties map.from(properties.getBatch()).whenNonNull().to(configuration::batch); map.from(properties.getDryRunOutput()).whenNonNull().to(configuration::dryRunOutput); 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 122a609d7f..bb60b8eb46 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 @@ -502,6 +502,24 @@ class FlywayAutoConfigurationTests { }); } + @Test + void initSqlsWithDataSource() { + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) + .withPropertyValues("spring.flyway.init-sqls=SELECT 1").run((context) -> { + Flyway flyway = context.getBean(Flyway.class); + assertThat(flyway.getConfiguration().getInitSql()).isEqualTo("SELECT 1"); + }); + } + + @Test + void initSqlsWithFlywayUrl() { + this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:h2:mem:" + UUID.randomUUID(), + "spring.flyway.init-sqls=SELECT 1").run((context) -> { + Flyway flyway = context.getBean(Flyway.class); + assertThat(flyway.getConfiguration().getInitSql()).isEqualTo("SELECT 1"); + }); + } + @Configuration(proxyBeanMethods = false) static class FlywayDataSourceConfiguration {