Skip to content

Commit

Permalink
Merge branch '3.0.x' into 3.1.x
Browse files Browse the repository at this point in the history
Closes gh-36759
  • Loading branch information
snicoll committed Aug 4, 2023
2 parents 8fc93f8 + f8a5aff commit ac1d8a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ public static DatabaseDriver fromProductName(String productName) {
* @param dataSource data source to inspect
* @return the database driver of {@link #UNKNOWN} if not found
* @since 2.6.0
* @deprecated since 2.7.15 for removal in 3.3.0 with no replacement
*/
@Deprecated(since = "2.7.15", forRemoval = true)
public static DatabaseDriver fromDataSource(DataSource dataSource) {
try {
String productName = JdbcUtils.commonDatabaseName(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.jdbc.init;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -26,6 +27,7 @@
import javax.sql.DataSource;

import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -89,7 +91,6 @@ public PlatformPlaceholderDatabaseDriverResolver withDriverPlatform(DatabaseDriv
* @param dataSource the DataSource from which the {@link DatabaseDriver} is derived
* @param values the values in which placeholders are resolved
* @return the values with their placeholders resolved
* @see DatabaseDriver#fromDataSource(DataSource)
*/
public List<String> resolveAll(DataSource dataSource, String... values) {
Assert.notNull(dataSource, "DataSource must not be null");
Expand Down Expand Up @@ -134,7 +135,14 @@ private String determinePlatform(DataSource dataSource) {
}

DatabaseDriver getDatabaseDriver(DataSource dataSource) {
return DatabaseDriver.fromDataSource(dataSource);
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName));
return DatabaseDriver.fromProductName(productName);
}
catch (Exception ex) {
throw new IllegalStateException("Failed to determine DatabaseDriver", ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -68,6 +69,24 @@ void resolveAllWithDataSourceWhenValueDoesNotContainPlaceholderShouldReturnValue
.containsExactly("schema.sql");
}

@Test
void resolveAllWithDataSourceWhenValueDoesNotContainPlaceholderShouldNotInteractWithDataSource() {
DataSource dataSource = mock(DataSource.class);
new PlatformPlaceholderDatabaseDriverResolver().resolveAll(mock(DataSource.class), "schema.sql");
then(dataSource).shouldHaveNoInteractions();
}

@Test
void resolveAllWithFailingDataSourceWhenValuesContainPlaceholdersShouldThrowNestedCause() throws SQLException {
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willThrow(new IllegalStateException("Test: invalid password"));
assertThatIllegalStateException()
.isThrownBy(() -> new PlatformPlaceholderDatabaseDriverResolver().resolveAll(dataSource, "schema.sql",
"schema-@@platform@@.sql", "data-@@platform@@.sql"))
.withMessage("Failed to determine DatabaseDriver")
.withStackTraceContaining("Test: invalid password");
}

@Test
void resolveAllWithDataSourceWhenValuesContainPlaceholdersShouldReturnValuesWithPlaceholdersReplaced()
throws SQLException {
Expand Down

0 comments on commit ac1d8a3

Please sign in to comment.