diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 5f4bab8ad5..fe92c5db8a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -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 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 property, Supplier defaultValue) { String value = property.get(); return (value != null) ? value : defaultValue.get(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index f251b58758..7d0c94cd51 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -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"; diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc index 35253fd787..4699e34cc9 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc @@ -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.