Skip to content

Commit 95f26c6

Browse files
committed
Configure Flyway with initSqls for any DataSource configuration
Previously, spring.flyway.init-sqls was only applied to Flyway's configuration if Flyway was being configured to create the DataSource. If Flyway was being configured to use an existing DataSource, init-sqls was not applied. This is a hangover from when the init SQLs support was introduced. At that time, Flyway only supported SQL to initialize the connection when it was creating the DataSource. Flyway 5.2 added init SQL support no matter how Flyway's DataSource was configured. This commit updates FlywayAutoConfiguration to always apply the init-sqls property to Flyway's configuration. The property's documentation does not describe the current limitation so this change should align the behaviour with what the documentation leads people to expect. Fixes gh-23392
1 parent 5ec673f commit 95f26c6

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ private DataSource configureDataSource(FluentConfiguration configuration, Flyway
145145
String user = getProperty(properties::getUser, dataSourceProperties::determineUsername);
146146
String password = getProperty(properties::getPassword, dataSourceProperties::determinePassword);
147147
configuration.dataSource(url, user, password);
148-
if (!CollectionUtils.isEmpty(properties.getInitSqls())) {
149-
String initSql = StringUtils.collectionToDelimitedString(properties.getInitSqls(), "\n");
150-
configuration.initSql(initSql);
151-
}
152148
}
153149
else if (flywayDataSource != null) {
154150
configuration.dataSource(flywayDataSource);
@@ -206,6 +202,9 @@ private void configureProperties(FluentConfiguration configuration, FlywayProper
206202
map.from(properties.isSkipDefaultCallbacks()).to(configuration::skipDefaultCallbacks);
207203
map.from(properties.isSkipDefaultResolvers()).to(configuration::skipDefaultResolvers);
208204
map.from(properties.isValidateOnMigrate()).to(configuration::validateOnMigrate);
205+
map.from(properties.getInitSqls()).whenNot(CollectionUtils::isEmpty)
206+
.as((initSqls) -> StringUtils.collectionToDelimitedString(initSqls, "\n"))
207+
.to(configuration::initSql);
209208
// Pro properties
210209
map.from(properties.getBatch()).whenNonNull().to(configuration::batch);
211210
map.from(properties.getDryRunOutput()).whenNonNull().to(configuration::dryRunOutput);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,24 @@ void customFlywayClassLoader() {
502502
});
503503
}
504504

505+
@Test
506+
void initSqlsWithDataSource() {
507+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
508+
.withPropertyValues("spring.flyway.init-sqls=SELECT 1").run((context) -> {
509+
Flyway flyway = context.getBean(Flyway.class);
510+
assertThat(flyway.getConfiguration().getInitSql()).isEqualTo("SELECT 1");
511+
});
512+
}
513+
514+
@Test
515+
void initSqlsWithFlywayUrl() {
516+
this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:h2:mem:" + UUID.randomUUID(),
517+
"spring.flyway.init-sqls=SELECT 1").run((context) -> {
518+
Flyway flyway = context.getBean(Flyway.class);
519+
assertThat(flyway.getConfiguration().getInitSql()).isEqualTo("SELECT 1");
520+
});
521+
}
522+
505523
@Configuration(proxyBeanMethods = false)
506524
static class FlywayDataSourceConfiguration {
507525

0 commit comments

Comments
 (0)