Skip to content

Commit ee7a686

Browse files
committed
Merge pull request #23958 from evgeniycheban
* gh-23958: Polish "Add liquibase driver class name property" Add liquibase driver class name property Closes gh-23958
2 parents 97fbef6 + 2db8e7e commit ee7a686

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
4545
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4646
import org.springframework.boot.jdbc.DataSourceBuilder;
47+
import org.springframework.boot.jdbc.DatabaseDriver;
4748
import org.springframework.context.annotation.Bean;
4849
import org.springframework.context.annotation.Conditional;
4950
import org.springframework.context.annotation.Configuration;
@@ -53,6 +54,7 @@
5354
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
5455
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
5556
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
57+
import org.springframework.util.StringUtils;
5658

5759
/**
5860
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
@@ -66,6 +68,7 @@
6668
* @author Dan Zheng
6769
* @author András Deák
6870
* @author Ferenc Gratzer
71+
* @author Evgeniy Cheban
6972
* @since 1.1.0
7073
*/
7174
@Configuration(proxyBeanMethods = false)
@@ -146,8 +149,19 @@ private DataSource createNewDataSource(DataSourceProperties dataSourceProperties
146149
String url = getProperty(this.properties::getUrl, dataSourceProperties::determineUrl);
147150
String user = getProperty(this.properties::getUser, dataSourceProperties::determineUsername);
148151
String password = getProperty(this.properties::getPassword, dataSourceProperties::determinePassword);
152+
String driverClassName = determineDriverClassName(dataSourceProperties, url);
149153
return DataSourceBuilder.create().type(determineDataSourceType()).url(url).username(user).password(password)
150-
.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;
151165
}
152166

153167
private Class<? extends DataSource> determineDataSourceType() {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Marcel Overdijk
3131
* @author Eddú Meléndez
3232
* @author Ferenc Gratzer
33+
* @author Evgeniy Cheban
3334
* @since 1.1.0
3435
*/
3536
@ConfigurationProperties(prefix = "spring.liquibase", ignoreUnknownFields = false)
@@ -96,6 +97,11 @@ public class LiquibaseProperties {
9697
*/
9798
private String password;
9899

100+
/**
101+
* Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
102+
*/
103+
private String driverClassName;
104+
99105
/**
100106
* JDBC URL of the database to migrate. If not set, the primary configured data source
101107
* is used.
@@ -226,6 +232,14 @@ public void setPassword(String password) {
226232
this.password = password;
227233
}
228234

235+
public String getDriverClassName() {
236+
return this.driverClassName;
237+
}
238+
239+
public void setDriverClassName(String driverClassName) {
240+
this.driverClassName = driverClassName;
241+
}
242+
229243
public String getUrl() {
230244
return this.url;
231245
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author András Deák
7474
* @author Andrii Hrytsiuk
7575
* @author Ferenc Gratzer
76+
* @author Evgeniy Cheban
7677
*/
7778
@ExtendWith(OutputCaptureExtension.class)
7879
class LiquibaseAutoConfigurationTests {
@@ -222,6 +223,38 @@ void overrideDataSource() {
222223
DataSource dataSource = liquibase.getDataSource();
223224
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
224225
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).isEqualTo("jdbc:hsqldb:mem:liquibase");
226+
assertThat(((HikariDataSource) dataSource).getDriverClassName())
227+
.isEqualTo("org.hsqldb.jdbc.JDBCDriver");
228+
}));
229+
}
230+
231+
@Test
232+
void overrideDataSourceAndDriverClassName() {
233+
String jdbcUrl = "jdbc:hsqldb:mem:liquibase";
234+
String driverClassName = "org.hsqldb.jdbcDriver";
235+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
236+
.withPropertyValues("spring.liquibase.url:" + jdbcUrl,
237+
"spring.liquibase.driver-class-name:" + driverClassName)
238+
.run(assertLiquibase((liquibase) -> {
239+
DataSource dataSource = liquibase.getDataSource();
240+
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
241+
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).isEqualTo(jdbcUrl);
242+
assertThat(((HikariDataSource) dataSource).getDriverClassName()).isEqualTo(driverClassName);
243+
}));
244+
}
245+
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);
225258
}));
226259
}
227260

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)