Skip to content

Commit a1f2eb0

Browse files
Øyvind Johannessenodrotbohm
authored andcommitted
GH-804 - Support for schema initialization on Microsoft SQL Server.
Original pull request: GH-808.
1 parent 100c1ab commit a1f2eb0

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

spring-modulith-events/spring-modulith-events-jdbc/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@
104104
<scope>test</scope>
105105
</dependency>
106106

107+
<dependency>
108+
<groupId>org.testcontainers</groupId>
109+
<artifactId>mssqlserver</artifactId>
110+
<scope>test</scope>
111+
</dependency>
112+
113+
<dependency>
114+
<groupId>com.microsoft.sqlserver</groupId>
115+
<artifactId>mssql-jdbc</artifactId>
116+
<scope>test</scope>
117+
</dependency>
118+
107119
<dependency>
108120
<groupId>org.hsqldb</groupId>
109121
<artifactId>hsqldb</artifactId>

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,20 @@ UUID databaseToUUID(Object id) {
4545
}
4646
},
4747

48-
POSTGRES("postgresql", "PostgreSQL");
48+
POSTGRES("postgresql", "PostgreSQL"),
49+
50+
MSSQL("sqlserver", "Microsoft SQL Server"){
51+
52+
@Override
53+
Object uuidToDatabase(UUID id) {
54+
return id.toString();
55+
}
56+
57+
@Override
58+
UUID databaseToUUID(Object id) {
59+
return UUID.fromString(id.toString());
60+
}
61+
};
4962

5063
static final String SCHEMA_NOT_SUPPORTED = "Setting the schema name not supported on MySQL!";
5164

@@ -86,6 +99,7 @@ String getSetSchemaSql(String schema) {
8699
case MYSQL -> throw new IllegalArgumentException(SCHEMA_NOT_SUPPORTED);
87100
case H2, HSQLDB -> "SET SCHEMA " + schema;
88101
case POSTGRES -> "SET search_path TO " + schema;
102+
case MSSQL -> ""; // No equivalent schema-switching command in MSSQL
89103
};
90104
}
91105
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EVENT_PUBLICATION')
2+
CREATE TABLE EVENT_PUBLICATION
3+
(
4+
ID VARCHAR(36) NOT NULL,
5+
LISTENER_ID VARCHAR(512) NOT NULL,
6+
EVENT_TYPE VARCHAR(512) NOT NULL,
7+
SERIALIZED_EVENT VARCHAR(MAX) NOT NULL,
8+
PUBLICATION_DATE DATETIME2(6) NOT NULL,
9+
COMPLETION_DATE DATETIME2(6) NULL,
10+
PRIMARY KEY (ID),
11+
INDEX EVENT_PUBLICATION_BY_COMPLETION_DATE_IDX (COMPLETION_DATE)
12+
);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ class Postgres extends WithInitEnabled {}
122122
@Nested
123123
@ActiveProfiles("mysql")
124124
class MySQL extends WithInitEnabled {}
125+
126+
@Nested
127+
@ActiveProfiles("mssql")
128+
class MSSQL extends WithInitEnabled {}
125129
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ class PostgresWithEmptySchemaName extends WithEmptySchemaName {}
453453
@WithPostgres
454454
class PostgresWithDeleteCompletion extends WithDeleteCompletion {}
455455

456+
// MSSQL
457+
@WithMssql
458+
class MssqlWithNoDefinedSchemaName extends WithNoDefinedSchemaName {}
459+
460+
@WithMssql
461+
class MssqlWithEmptySchemaName extends WithEmptySchemaName {}
462+
463+
@WithMssql
464+
class MssqlWithDeleteCompletion extends WithDeleteCompletion {}
465+
456466
// MySQL
457467

458468
@WithMySql
@@ -489,4 +499,9 @@ private static final class Sample {}
489499
@ActiveProfiles("postgres")
490500
@Retention(RetentionPolicy.RUNTIME)
491501
@interface WithPostgres {}
502+
503+
@Nested
504+
@ActiveProfiles("mssql")
505+
@Retention(RetentionPolicy.RUNTIME)
506+
@interface WithMssql {}
492507
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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

0 commit comments

Comments
 (0)