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
This commit is contained in:
Andy Wilkinson 2020-09-18 13:03:24 +01:00
parent 5ec673ff2a
commit 95f26c6358
2 changed files with 21 additions and 4 deletions

View File

@ -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);

View File

@ -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 {