Fix password handling in Flyway auto-configuration

Previously, FlywayProperties returned an empty string when its
password had not been set. This prevented the desired fallback to
JdbcConnectionDetails.

Fixes gh-35046
pull/35090/head
Andy Wilkinson 2 years ago
parent 83c45d14a4
commit e2ee1ed50d

@ -188,7 +188,8 @@ public class FlywayAutoConfiguration {
private void applyConnectionDetails(FlywayConnectionDetails connectionDetails, DataSourceBuilder<?> builder) {
builder.username(connectionDetails.getUsername());
builder.password(connectionDetails.getPassword());
String password = connectionDetails.getPassword();
builder.password((password != null) ? password : "");
String driverClassName = connectionDetails.getDriverClassName();
if (StringUtils.hasText(driverClassName)) {
builder.driverClassName(driverClassName);

@ -576,7 +576,7 @@ public class FlywayProperties {
}
public String getPassword() {
return (this.password != null) ? this.password : "";
return this.password;
}
public void setPassword(String password) {

@ -192,6 +192,28 @@ class FlywayAutoConfigurationTests {
});
}
@Test
void jdbcConnectionDetailsAreUsedOverDataSourceProperties() {
this.contextRunner
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, JdbcConnectionDetailsConfiguration.class,
MockFlywayMigrationStrategy.class)
.withPropertyValues("spring.datasource.url=jdbc:hsqldb:mem:flywaytest", "spring.datasource.user=some-user",
"spring.datasource.password=some-password",
"spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver")
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
DataSource dataSource = flyway.getConfiguration().getDataSource();
assertThat(dataSource).isInstanceOf(SimpleDriverDataSource.class);
SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) dataSource;
assertThat(simpleDriverDataSource.getUrl())
.isEqualTo("jdbc:postgresql://database.example.com:12345/database-1");
assertThat(simpleDriverDataSource.getUsername()).isEqualTo("user-1");
assertThat(simpleDriverDataSource.getPassword()).isEqualTo("secret-1");
assertThat(simpleDriverDataSource.getDriver()).isInstanceOf(Driver.class);
});
}
@Test
void createDataSourceWithUser() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)

Loading…
Cancel
Save