Skip to content

Commit 03a6700

Browse files
committed
GH-804 - Polishing.
Actively reject attempts to use a dedicated schema with SQL Server. Prevent schema reset being executed if no schema is used. Properly report exception during database name detection. Original pull request: GH-808.
1 parent a1f2eb0 commit 03a6700

File tree

9 files changed

+43
-17
lines changed

9 files changed

+43
-17
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public void afterPropertiesSet() throws Exception {
6767

6868
var initialSchema = connection.getSchema();
6969
var schemaName = properties.getSchema();
70+
var useSchema = schemaName != null && !schemaName.isEmpty();
7071

71-
if (schemaName != null && !schemaName.isEmpty()) { // A schema name has been specified.
72+
if (useSchema) { // A schema name has been specified.
7273

7374
if (eventPublicationTableExists(schemaName)) {
7475
return;
@@ -82,7 +83,7 @@ public void afterPropertiesSet() throws Exception {
8283
new ResourceDatabasePopulator(locator.getSchemaResource(databaseType)).execute(dataSource);
8384

8485
// Return to the initial schema.
85-
if (initialSchema != null) {
86+
if (initialSchema != null && useSchema) {
8687
jdbcOperations.execute(databaseType.getSetSchemaSql(initialSchema));
8788
}
8889
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ Object uuidToDatabase(UUID id) {
4343
UUID databaseToUUID(Object id) {
4444
return UUID.fromString(id.toString());
4545
}
46+
47+
@Override
48+
boolean isSchemaSupported() {
49+
return false;
50+
}
4651
},
4752

4853
POSTGRES("postgresql", "PostgreSQL"),
4954

50-
MSSQL("sqlserver", "Microsoft SQL Server"){
55+
MSSQL("sqlserver", "Microsoft SQL Server") {
5156

5257
@Override
5358
Object uuidToDatabase(UUID id) {
@@ -58,6 +63,11 @@ Object uuidToDatabase(UUID id) {
5863
UUID databaseToUUID(Object id) {
5964
return UUID.fromString(id.toString());
6065
}
66+
67+
@Override
68+
boolean isSchemaSupported() {
69+
return false;
70+
}
6171
};
6272

6373
static final String SCHEMA_NOT_SUPPORTED = "Setting the schema name not supported on MySQL!";
@@ -92,14 +102,21 @@ String getSchemaResourceFilename() {
92102
return "/schema-" + value + ".sql";
93103
}
94104

105+
boolean isSchemaSupported() {
106+
return true;
107+
}
108+
95109
String getSetSchemaSql(String schema) {
96110

111+
if (!isSchemaSupported()) {
112+
throw new IllegalArgumentException(SCHEMA_NOT_SUPPORTED);
113+
}
114+
97115
return switch (this) {
98116

99-
case MYSQL -> throw new IllegalArgumentException(SCHEMA_NOT_SUPPORTED);
100117
case H2, HSQLDB -> "SET SCHEMA " + schema;
101118
case POSTGRES -> "SET search_path TO " + schema;
102-
case MSSQL -> ""; // No equivalent schema-switching command in MSSQL
119+
default -> throw new IllegalArgumentException(SCHEMA_NOT_SUPPORTED);
103120
};
104121
}
105122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public String getSchema() {
6464

6565
void verify(DatabaseType databaseType) {
6666

67-
if (schema != null && databaseType.equals(DatabaseType.MYSQL)) {
67+
if (schema != null && !databaseType.isSchemaSupported()) {
6868
throw new IllegalStateException(DatabaseType.SCHEMA_NOT_SUPPORTED);
6969
}
7070
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ private static String fromDataSource(DataSource dataSource) {
7979
var metadata = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName);
8080
name = JdbcUtils.commonDatabaseName(metadata);
8181

82-
} catch (Exception o_O) {}
82+
} catch (Exception o_O) {
83+
throw new RuntimeException(o_O);
84+
}
8385

8486
return name == null ? "UNKNOWN" : name;
8587
}

spring-modulith-events/spring-modulith-events-jdbc/src/main/resources/schema-sqlserver.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ CREATE TABLE EVENT_PUBLICATION
99
COMPLETION_DATE DATETIME2(6) NULL,
1010
PRIMARY KEY (ID),
1111
INDEX EVENT_PUBLICATION_BY_COMPLETION_DATE_IDX (COMPLETION_DATE)
12-
);
12+
);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ class DatabaseSchemaInitializerUnitTests {
4444
@ParameterizedTest
4545
@ValueSource(strings = { "", "test" })
4646
void rejectsExplicitSchemaNameForMySql(String schema) {
47-
assertThatIllegalStateException().isThrownBy(() -> createInitializer(withSchema(schema)));
47+
assertThatIllegalStateException().isThrownBy(() -> createInitializer(withSchema(schema), DatabaseType.MYSQL));
4848
}
4949

50-
private DatabaseSchemaInitializer createInitializer(JdbcConfigurationProperties properties) {
51-
return new DatabaseSchemaInitializer(dataSource, resourceLoader, DatabaseType.MYSQL, jdbcTemplate, properties);
50+
// GH-804
51+
@ParameterizedTest
52+
@ValueSource(strings = { "", "test" })
53+
void rejectsExplicitSchemaNameForMSSql(String schema) {
54+
assertThatIllegalStateException().isThrownBy(() -> createInitializer(withSchema(schema), DatabaseType.MSSQL));
55+
}
56+
57+
private DatabaseSchemaInitializer createInitializer(JdbcConfigurationProperties properties, DatabaseType type) {
58+
return new DatabaseSchemaInitializer(dataSource, resourceLoader, type, jdbcTemplate, properties);
5259
}
5360

5461
private static JdbcConfigurationProperties withSchema(String schema) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.junit.jupiter.api.Test;
3737
import org.springframework.beans.factory.annotation.Autowired;
3838
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
39-
import org.springframework.test.context.bean.override.mockito.MockitoBean;
4039
import org.springframework.context.annotation.Import;
4140
import org.springframework.jdbc.core.JdbcOperations;
4241
import org.springframework.modulith.events.core.EventSerializer;
@@ -46,6 +45,7 @@
4645
import org.springframework.modulith.testapp.TestApplication;
4746
import org.springframework.test.context.ActiveProfiles;
4847
import org.springframework.test.context.ContextConfiguration;
48+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
4949
import org.testcontainers.junit.jupiter.Testcontainers;
5050

5151
/**
@@ -454,11 +454,9 @@ class PostgresWithEmptySchemaName extends WithEmptySchemaName {}
454454
class PostgresWithDeleteCompletion extends WithDeleteCompletion {}
455455

456456
// MSSQL
457-
@WithMssql
458-
class MssqlWithNoDefinedSchemaName extends WithNoDefinedSchemaName {}
459457

460458
@WithMssql
461-
class MssqlWithEmptySchemaName extends WithEmptySchemaName {}
459+
class MssqlWithNoDefinedSchemaName extends WithNoDefinedSchemaName {}
462460

463461
@WithMssql
464462
class MssqlWithDeleteCompletion extends WithDeleteCompletion {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
spring.datasource.url=jdbc:tc:sqlserver:2022-latest:///events;encrypt=false;TC_ACCEPT_LICENSE=accept
2-
spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver
1+
spring.datasource.url=jdbc:tc:sqlserver:2022-latest:///events;encrypt=false
2+
spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mcr.microsoft.com/mssql/server:2022-latest

0 commit comments

Comments
 (0)