Skip to content

Commit 2db8e7e

Browse files
committed
Polish "Add liquibase driver class name property"
See gh-23958
1 parent 8a9b31a commit 2db8e7e

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
5555
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
5656
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
57+
import org.springframework.util.StringUtils;
5758

5859
/**
5960
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
@@ -148,20 +149,26 @@ private DataSource createNewDataSource(DataSourceProperties dataSourceProperties
148149
String url = getProperty(this.properties::getUrl, dataSourceProperties::determineUrl);
149150
String user = getProperty(this.properties::getUser, dataSourceProperties::determineUsername);
150151
String password = getProperty(this.properties::getPassword, dataSourceProperties::determinePassword);
152+
String driverClassName = determineDriverClassName(dataSourceProperties, url);
151153
return DataSourceBuilder.create().type(determineDataSourceType()).url(url).username(user).password(password)
152-
.driverClassName(determineDriverClassName(url)).build();
154+
.driverClassName(driverClassName).build();
155+
}
156+
157+
private String determineDriverClassName(DataSourceProperties dataSourceProperties, String url) {
158+
if (StringUtils.hasText(this.properties.getDriverClassName())) {
159+
return this.properties.getDriverClassName();
160+
}
161+
if (StringUtils.hasText(dataSourceProperties.getDriverClassName())) {
162+
return dataSourceProperties.getDriverClassName();
163+
}
164+
return StringUtils.hasText(url) ? DatabaseDriver.fromJdbcUrl(url).getDriverClassName() : null;
153165
}
154166

155167
private Class<? extends DataSource> determineDataSourceType() {
156168
Class<? extends DataSource> type = DataSourceBuilder.findType(null);
157169
return (type != null) ? type : SimpleDriverDataSource.class;
158170
}
159171

160-
private String determineDriverClassName(String url) {
161-
String driverClassName = this.properties.getDriverClassName();
162-
return (driverClassName != null) ? driverClassName : DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
163-
}
164-
165172
private String getProperty(Supplier<String> property, Supplier<String> defaultValue) {
166173
String value = property.get();
167174
return (value != null) ? value : defaultValue.get();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,21 @@ void overrideDataSourceAndDriverClassName() {
243243
}));
244244
}
245245

246+
@Test
247+
void overrideDataSourceWithFallbackDriverClassName() {
248+
String jdbcUrl = "jdbc:hsqldb:mem:liquibase";
249+
String driverClassName = "org.hsqldb.jdbcDriver";
250+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
251+
.withPropertyValues("spring.liquibase.url:" + jdbcUrl,
252+
"spring.datasource.driver-class-name:" + driverClassName)
253+
.run(assertLiquibase((liquibase) -> {
254+
DataSource dataSource = liquibase.getDataSource();
255+
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
256+
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).isEqualTo(jdbcUrl);
257+
assertThat(((HikariDataSource) dataSource).getDriverClassName()).isEqualTo(driverClassName);
258+
}));
259+
}
260+
246261
@Test
247262
void overrideUser() {
248263
String jdbcUrl = "jdbc:hsqldb:mem:normal";

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,7 @@ In addition to YAML, Liquibase also supports JSON, XML, and SQL change log forma
22172217
By default, Liquibase autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
22182218
If you need to use a different `DataSource`, you can create one and mark its `@Bean` as `@LiquibaseDataSource`.
22192219
If you do so and you want two data sources, remember to create another one and mark it as `@Primary`.
2220-
Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[url,user,password]` in external properties.
2220+
Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[driver-class-name,url,user,password]` in external properties.
22212221
Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`.
22222222
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
22232223

0 commit comments

Comments
 (0)