GH-460 - Move off deprecated DatabaseDriver.fromDataSource(…).

This commit is contained in:
Oliver Drotbohm
2024-01-17 16:30:09 +01:00
parent dd9efef067
commit 7c91e56c3b
3 changed files with 32 additions and 27 deletions

View File

@@ -15,10 +15,9 @@
*/
package org.springframework.modulith.events.jdbc;
import java.util.Map;
import java.util.Arrays;
import java.util.UUID;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.util.Assert;
/**
@@ -28,11 +27,11 @@ import org.springframework.util.Assert;
*/
enum DatabaseType {
HSQLDB("hsqldb"),
HSQLDB("hsqldb", "HSQL Database Engine"),
H2("h2"),
H2("h2", "H2"),
MYSQL("mysql") {
MYSQL("mysql", "MySQL") {
@Override
Object uuidToDatabase(UUID id) {
@@ -45,30 +44,21 @@ enum DatabaseType {
}
},
POSTGRES("postgresql");
POSTGRES("postgresql", "PostgreSQL");
private static final Map<DatabaseDriver, DatabaseType> DATABASE_DRIVER_TO_DATABASE_TYPE_MAP = //
Map.of( //
DatabaseDriver.H2, H2, //
DatabaseDriver.HSQLDB, HSQLDB, //
DatabaseDriver.POSTGRESQL, POSTGRES, //
DatabaseDriver.MYSQL, MYSQL);
static DatabaseType from(String productName) {
static DatabaseType from(DatabaseDriver databaseDriver) {
var databaseType = DATABASE_DRIVER_TO_DATABASE_TYPE_MAP.get(databaseDriver);
if (databaseType == null) {
throw new IllegalArgumentException("Unsupported database type: " + databaseDriver);
}
return databaseType;
return Arrays.stream(DatabaseType.values())
.filter(it -> it.fullName.equalsIgnoreCase(productName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unsupported database type: " + productName));
}
private final String value;
private final String value, fullName;
DatabaseType(String value) {
DatabaseType(String value, String fullName) {
this.value = value;
this.fullName = fullName;
}
Object uuidToDatabase(UUID id) {

View File

@@ -15,15 +15,17 @@
*/
package org.springframework.modulith.events.jdbc;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.modulith.events.config.EventPublicationAutoConfiguration;
import org.springframework.modulith.events.config.EventPublicationConfigurationExtension;
import org.springframework.modulith.events.core.EventSerializer;
@@ -39,7 +41,7 @@ class JdbcEventPublicationAutoConfiguration implements EventPublicationConfigura
@Bean
DatabaseType databaseType(DataSource dataSource) {
return DatabaseType.from(DatabaseDriver.fromDataSource(dataSource));
return DatabaseType.from(fromDataSource(dataSource));
}
@Bean
@@ -56,4 +58,18 @@ class JdbcEventPublicationAutoConfiguration implements EventPublicationConfigura
return new DatabaseSchemaInitializer(jdbcTemplate, resourceLoader, databaseType);
}
private static String fromDataSource(DataSource dataSource) {
String name = null;
try {
var metadata = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName);
name = JdbcUtils.commonDatabaseName(metadata);
} catch (Exception o_O) {}
return name == null ? "UNKNOWN" : name;
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.modulith.events.jdbc;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.jdbc.DatabaseDriver;
class DatabaseTypeUnitTests {
@@ -26,7 +25,7 @@ class DatabaseTypeUnitTests {
void shouldThrowExceptionOnUnsupportedDatabaseType() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> DatabaseType.from(DatabaseDriver.UNKNOWN))
.isThrownBy(() -> DatabaseType.from("UNKNOWN"))
.withMessageContaining("UNKNOWN");
}
}