Commit 2db8e7ee authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Add liquibase driver class name property"

See gh-23958
parent 8a9b31aa
...@@ -54,6 +54,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; ...@@ -54,6 +54,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase. * {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
...@@ -148,8 +149,19 @@ public class LiquibaseAutoConfiguration { ...@@ -148,8 +149,19 @@ public class LiquibaseAutoConfiguration {
String url = getProperty(this.properties::getUrl, dataSourceProperties::determineUrl); String url = getProperty(this.properties::getUrl, dataSourceProperties::determineUrl);
String user = getProperty(this.properties::getUser, dataSourceProperties::determineUsername); String user = getProperty(this.properties::getUser, dataSourceProperties::determineUsername);
String password = getProperty(this.properties::getPassword, dataSourceProperties::determinePassword); String password = getProperty(this.properties::getPassword, dataSourceProperties::determinePassword);
String driverClassName = determineDriverClassName(dataSourceProperties, url);
return DataSourceBuilder.create().type(determineDataSourceType()).url(url).username(user).password(password) return DataSourceBuilder.create().type(determineDataSourceType()).url(url).username(user).password(password)
.driverClassName(determineDriverClassName(url)).build(); .driverClassName(driverClassName).build();
}
private String determineDriverClassName(DataSourceProperties dataSourceProperties, String url) {
if (StringUtils.hasText(this.properties.getDriverClassName())) {
return this.properties.getDriverClassName();
}
if (StringUtils.hasText(dataSourceProperties.getDriverClassName())) {
return dataSourceProperties.getDriverClassName();
}
return StringUtils.hasText(url) ? DatabaseDriver.fromJdbcUrl(url).getDriverClassName() : null;
} }
private Class<? extends DataSource> determineDataSourceType() { private Class<? extends DataSource> determineDataSourceType() {
...@@ -157,11 +169,6 @@ public class LiquibaseAutoConfiguration { ...@@ -157,11 +169,6 @@ public class LiquibaseAutoConfiguration {
return (type != null) ? type : SimpleDriverDataSource.class; return (type != null) ? type : SimpleDriverDataSource.class;
} }
private String determineDriverClassName(String url) {
String driverClassName = this.properties.getDriverClassName();
return (driverClassName != null) ? driverClassName : DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
}
private String getProperty(Supplier<String> property, Supplier<String> defaultValue) { private String getProperty(Supplier<String> property, Supplier<String> defaultValue) {
String value = property.get(); String value = property.get();
return (value != null) ? value : defaultValue.get(); return (value != null) ? value : defaultValue.get();
......
...@@ -243,6 +243,21 @@ class LiquibaseAutoConfigurationTests { ...@@ -243,6 +243,21 @@ class LiquibaseAutoConfigurationTests {
})); }));
} }
@Test
void overrideDataSourceWithFallbackDriverClassName() {
String jdbcUrl = "jdbc:hsqldb:mem:liquibase";
String driverClassName = "org.hsqldb.jdbcDriver";
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.liquibase.url:" + jdbcUrl,
"spring.datasource.driver-class-name:" + driverClassName)
.run(assertLiquibase((liquibase) -> {
DataSource dataSource = liquibase.getDataSource();
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).isEqualTo(jdbcUrl);
assertThat(((HikariDataSource) dataSource).getDriverClassName()).isEqualTo(driverClassName);
}));
}
@Test @Test
void overrideUser() { void overrideUser() {
String jdbcUrl = "jdbc:hsqldb:mem:normal"; String jdbcUrl = "jdbc:hsqldb:mem:normal";
......
...@@ -2217,7 +2217,7 @@ In addition to YAML, Liquibase also supports JSON, XML, and SQL change log forma ...@@ -2217,7 +2217,7 @@ In addition to YAML, Liquibase also supports JSON, XML, and SQL change log forma
By default, Liquibase autowires the (`@Primary`) `DataSource` in your context and uses that for migrations. By default, Liquibase autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
If you need to use a different `DataSource`, you can create one and mark its `@Bean` as `@LiquibaseDataSource`. If you need to use a different `DataSource`, you can create one and mark its `@Bean` as `@LiquibaseDataSource`.
If you do so and you want two data sources, remember to create another one and mark it as `@Primary`. If you do so and you want two data sources, remember to create another one and mark it as `@Primary`.
Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[url,user,password]` in external properties. Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[driver-class-name,url,user,password]` in external properties.
Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`. Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`.
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used. If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment