Polish "Add liquibase driver class name property"
See gh-23958
This commit is contained in:
parent
8a9b31aa69
commit
2db8e7eebe
|
|
@ -54,6 +54,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
|||
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
|
||||
|
|
@ -148,8 +149,19 @@ public class LiquibaseAutoConfiguration {
|
|||
String url = getProperty(this.properties::getUrl, dataSourceProperties::determineUrl);
|
||||
String user = getProperty(this.properties::getUser, dataSourceProperties::determineUsername);
|
||||
String password = getProperty(this.properties::getPassword, dataSourceProperties::determinePassword);
|
||||
String driverClassName = determineDriverClassName(dataSourceProperties, url);
|
||||
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() {
|
||||
|
|
@ -157,11 +169,6 @@ public class LiquibaseAutoConfiguration {
|
|||
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) {
|
||||
String value = property.get();
|
||||
return (value != null) ? value : defaultValue.get();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
void overrideUser() {
|
||||
String jdbcUrl = "jdbc:hsqldb:mem:normal";
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
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`.
|
||||
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`.
|
||||
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue