Skip to content

Commit 6905fdd

Browse files
committed
GH-458 - Move off deprecated DatabaseDriver.fromDataSource(…).
1 parent 48aa0b2 commit 6905fdd

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

spring-modulith-events/spring-modulith-events-jdbc/src/main/java/org/springframework/modulith/events/jdbc/DatabaseType.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
*/
1616
package org.springframework.modulith.events.jdbc;
1717

18-
import java.util.Map;
18+
import java.util.Arrays;
1919
import java.util.UUID;
2020

21-
import org.springframework.boot.jdbc.DatabaseDriver;
2221
import org.springframework.util.Assert;
2322

2423
/**
@@ -28,11 +27,11 @@
2827
*/
2928
enum DatabaseType {
3029

31-
HSQLDB("hsqldb"),
30+
HSQLDB("hsqldb", "HSQL Database Engine"),
3231

33-
H2("h2"),
32+
H2("h2", "H2"),
3433

35-
MYSQL("mysql") {
34+
MYSQL("mysql", "MySQL") {
3635

3736
@Override
3837
Object uuidToDatabase(UUID id) {
@@ -45,30 +44,21 @@ UUID databaseToUUID(Object id) {
4544
}
4645
},
4746

48-
POSTGRES("postgresql");
47+
POSTGRES("postgresql", "PostgreSQL");
4948

50-
private static final Map<DatabaseDriver, DatabaseType> DATABASE_DRIVER_TO_DATABASE_TYPE_MAP = //
51-
Map.of( //
52-
DatabaseDriver.H2, H2, //
53-
DatabaseDriver.HSQLDB, HSQLDB, //
54-
DatabaseDriver.POSTGRESQL, POSTGRES, //
55-
DatabaseDriver.MYSQL, MYSQL);
49+
static DatabaseType from(String productName) {
5650

57-
static DatabaseType from(DatabaseDriver databaseDriver) {
58-
59-
var databaseType = DATABASE_DRIVER_TO_DATABASE_TYPE_MAP.get(databaseDriver);
60-
61-
if (databaseType == null) {
62-
throw new IllegalArgumentException("Unsupported database type: " + databaseDriver);
63-
}
64-
65-
return databaseType;
51+
return Arrays.stream(DatabaseType.values())
52+
.filter(it -> it.fullName.equalsIgnoreCase(productName))
53+
.findFirst()
54+
.orElseThrow(() -> new IllegalArgumentException("Unsupported database type: " + productName));
6655
}
6756

68-
private final String value;
57+
private final String value, fullName;
6958

70-
DatabaseType(String value) {
59+
DatabaseType(String value, String fullName) {
7160
this.value = value;
61+
this.fullName = fullName;
7262
}
7363

7464
Object uuidToDatabase(UUID id) {

spring-modulith-events/spring-modulith-events-jdbc/src/main/java/org/springframework/modulith/events/jdbc/JdbcEventPublicationAutoConfiguration.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.springframework.modulith.events.jdbc;
1717

18+
import java.sql.DatabaseMetaData;
19+
1820
import javax.sql.DataSource;
1921

2022
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
22-
import org.springframework.boot.jdbc.DatabaseDriver;
2324
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Configuration;
2526
import org.springframework.core.io.ResourceLoader;
2627
import org.springframework.jdbc.core.JdbcTemplate;
28+
import org.springframework.jdbc.support.JdbcUtils;
2729
import org.springframework.modulith.events.config.EventPublicationAutoConfiguration;
2830
import org.springframework.modulith.events.config.EventPublicationConfigurationExtension;
2931
import org.springframework.modulith.events.core.EventSerializer;
@@ -39,7 +41,7 @@ class JdbcEventPublicationAutoConfiguration implements EventPublicationConfigura
3941

4042
@Bean
4143
DatabaseType databaseType(DataSource dataSource) {
42-
return DatabaseType.from(DatabaseDriver.fromDataSource(dataSource));
44+
return DatabaseType.from(fromDataSource(dataSource));
4345
}
4446

4547
@Bean
@@ -56,4 +58,18 @@ DatabaseSchemaInitializer databaseSchemaInitializer(JdbcTemplate jdbcTemplate, R
5658

5759
return new DatabaseSchemaInitializer(jdbcTemplate, resourceLoader, databaseType);
5860
}
61+
62+
private static String fromDataSource(DataSource dataSource) {
63+
64+
String name = null;
65+
66+
try {
67+
68+
var metadata = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName);
69+
name = JdbcUtils.commonDatabaseName(metadata);
70+
71+
} catch (Exception o_O) {}
72+
73+
return name == null ? "UNKNOWN" : name;
74+
}
5975
}

spring-modulith-events/spring-modulith-events-jdbc/src/test/java/org/springframework/modulith/events/jdbc/DatabaseTypeUnitTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.jupiter.api.Test;
21-
import org.springframework.boot.jdbc.DatabaseDriver;
2221

2322
class DatabaseTypeUnitTests {
2423

2524
@Test // GH-29
2625
void shouldThrowExceptionOnUnsupportedDatabaseType() {
2726

2827
assertThatExceptionOfType(IllegalArgumentException.class)
29-
.isThrownBy(() -> DatabaseType.from(DatabaseDriver.UNKNOWN))
28+
.isThrownBy(() -> DatabaseType.from("UNKNOWN"))
3029
.withMessageContaining("UNKNOWN");
3130
}
3231
}

0 commit comments

Comments
 (0)