Skip to content

Commit

Permalink
GH-458 - Move off deprecated DatabaseDriver.fromDataSource(…).
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Jan 17, 2024
1 parent 48aa0b2 commit 6905fdd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -28,11 +27,11 @@
*/
enum DatabaseType {

HSQLDB("hsqldb"),
HSQLDB("hsqldb", "HSQL Database Engine"),

H2("h2"),
H2("h2", "H2"),

MYSQL("mysql") {
MYSQL("mysql", "MySQL") {

@Override
Object uuidToDatabase(UUID id) {
Expand All @@ -45,30 +44,21 @@ UUID databaseToUUID(Object id) {
}
},

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -56,4 +58,18 @@ DatabaseSchemaInitializer databaseSchemaInitializer(JdbcTemplate jdbcTemplate, R

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.boot.jdbc.DatabaseDriver;

class DatabaseTypeUnitTests {

@Test // GH-29
void shouldThrowExceptionOnUnsupportedDatabaseType() {

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> DatabaseType.from(DatabaseDriver.UNKNOWN))
.isThrownBy(() -> DatabaseType.from("UNKNOWN"))
.withMessageContaining("UNKNOWN");
}
}

0 comments on commit 6905fdd

Please sign in to comment.