Skip to content

Commit 07ddeac

Browse files
committed
GH-685 - Polishing.
Disable schema support for MySQL as it doesn't know about schemas per database. General polishing.
1 parent 75b3ff4 commit 07ddeac

File tree

8 files changed

+225
-128
lines changed

8 files changed

+225
-128
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
<artifactId>spring-boot-autoconfigure</artifactId>
4747
</dependency>
4848

49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-configuration-processor</artifactId>
52+
<optional>true</optional>
53+
</dependency>
54+
55+
4956
<!-- Testing -->
5057

5158
<dependency>
@@ -59,13 +66,13 @@
5966
<artifactId>spring-boot-starter-jdbc</artifactId>
6067
<scope>test</scope>
6168
</dependency>
62-
69+
6370
<dependency>
6471
<groupId>org.springframework</groupId>
6572
<artifactId>spring-web</artifactId>
6673
<scope>test</scope>
6774
</dependency>
68-
75+
6976
<dependency>
7077
<groupId>jakarta.servlet</groupId>
7178
<artifactId>jakarta.servlet-api</artifactId>

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import org.springframework.beans.factory.InitializingBean;
2323
import org.springframework.core.io.ResourceLoader;
24-
import org.springframework.jdbc.core.JdbcTemplate;
24+
import org.springframework.jdbc.core.JdbcOperations;
2525
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
26-
import org.springframework.util.ObjectUtils;
26+
import org.springframework.util.Assert;
2727

2828
/**
2929
* Initializes the DB schema used to store events
@@ -38,55 +38,66 @@ class DatabaseSchemaInitializer implements InitializingBean {
3838
private final DataSource dataSource;
3939
private final ResourceLoader resourceLoader;
4040
private final DatabaseType databaseType;
41-
private final JdbcTemplate jdbcTemplate;
41+
private final JdbcOperations jdbcOperations;
4242
private final JdbcConfigurationProperties properties;
4343

4444
DatabaseSchemaInitializer(DataSource dataSource, ResourceLoader resourceLoader, DatabaseType databaseType,
45-
JdbcTemplate jdbcTemplate, JdbcConfigurationProperties properties) {
45+
JdbcOperations jdbcOperations, JdbcConfigurationProperties properties) {
46+
47+
Assert.isTrue(properties.getSchemaInitialization().isEnabled(),
48+
"Schema initialization disabled! Initializer should not have been registered!");
4649

4750
this.dataSource = dataSource;
4851
this.resourceLoader = resourceLoader;
4952
this.databaseType = databaseType;
50-
this.jdbcTemplate = jdbcTemplate;
53+
this.jdbcOperations = jdbcOperations;
5154
this.properties = properties;
55+
56+
properties.verify(databaseType);
5257
}
5358

59+
/*
60+
* (non-Javadoc)
61+
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
62+
*/
5463
@Override
5564
public void afterPropertiesSet() throws Exception {
5665

57-
String initialSchema;
5866
try (Connection connection = dataSource.getConnection()) {
5967

60-
initialSchema = connection.getSchema();
68+
var initialSchema = connection.getSchema();
69+
var schemaName = properties.getSchema();
6170

62-
String schemaName = properties.getSchema();
63-
if (!ObjectUtils.isEmpty(schemaName)) { // A schema name has been specified.
71+
if (schemaName != null && !schemaName.isEmpty()) { // A schema name has been specified.
6472

65-
if (eventPublicationTableExists(jdbcTemplate, schemaName)) {
73+
if (eventPublicationTableExists(schemaName)) {
6674
return;
6775
}
6876

69-
jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS " + schemaName);
70-
jdbcTemplate.execute(databaseType.sqlStatementSetSchema(schemaName));
77+
jdbcOperations.execute("CREATE SCHEMA IF NOT EXISTS " + schemaName);
78+
jdbcOperations.execute(databaseType.getSetSchemaSql(schemaName));
7179
}
7280

7381
var locator = new DatabaseSchemaLocator(resourceLoader);
7482
new ResourceDatabasePopulator(locator.getSchemaResource(databaseType)).execute(dataSource);
7583

7684
// Return to the initial schema.
7785
if (initialSchema != null) {
78-
jdbcTemplate.execute(databaseType.sqlStatementSetSchema(initialSchema));
86+
jdbcOperations.execute(databaseType.getSetSchemaSql(initialSchema));
7987
}
8088
}
8189
}
8290

83-
private boolean eventPublicationTableExists(JdbcTemplate jdbcTemplate, String schema) {
84-
String query = """
91+
private boolean eventPublicationTableExists(String schema) {
92+
93+
var query = """
8594
SELECT COUNT(*)
8695
FROM information_schema.tables
8796
WHERE table_schema = ? AND table_name = 'EVENT_PUBLICATION'
8897
""";
89-
Integer count = jdbcTemplate.queryForObject(query, Integer.class, schema);
98+
99+
var count = jdbcOperations.queryForObject(query, Integer.class, schema);
100+
90101
return count != null && count > 0;
91102
}
92103
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ UUID databaseToUUID(Object id) {
4747

4848
POSTGRES("postgresql", "PostgreSQL");
4949

50+
static final String SCHEMA_NOT_SUPPORTED = "Setting the schema name not supported on MySQL!";
51+
5052
static DatabaseType from(String productName) {
5153

5254
return Arrays.stream(DatabaseType.values())
@@ -77,9 +79,12 @@ String getSchemaResourceFilename() {
7779
return "/schema-" + value + ".sql";
7880
}
7981

80-
String sqlStatementSetSchema(String schema) {
82+
String getSetSchemaSql(String schema) {
83+
8184
return switch (this) {
82-
case MYSQL, H2, HSQLDB -> "SET SCHEMA " + schema;
85+
86+
case MYSQL -> throw new IllegalArgumentException(SCHEMA_NOT_SUPPORTED);
87+
case H2, HSQLDB -> "SET SCHEMA " + schema;
8388
case POSTGRES -> "SET search_path TO " + schema;
8489
};
8590
}

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,39 +24,65 @@
2424
* Configuration properties for JDBC.
2525
*
2626
* @author Raed Ben Hamouda
27+
* @author Oliver Drotbohm
2728
*/
2829
@ConfigurationProperties(prefix = "spring.modulith.events.jdbc")
2930
class JdbcConfigurationProperties {
3031

31-
private final boolean schemaInitializationEnabled;
32+
private final SchemaInitialization schemaInitialization;
3233
private final String schema;
3334

3435
/**
3536
* Creates a new {@link JdbcConfigurationProperties} instance.
3637
*
37-
* @param schemaInitializationEnabled whether to initialize the JDBC event publication schema. Defaults to {@literal false}.
38+
* @param schemaInitialization whether to initialize the JDBC event publication schema. Defaults to {@literal false}.
3839
* @param schema the schema name of event publication table, can be {@literal null}.
3940
*/
4041
@ConstructorBinding
41-
JdbcConfigurationProperties(@DefaultValue("false") boolean schemaInitializationEnabled, @Nullable String schema) {
42+
JdbcConfigurationProperties(SchemaInitialization schemaInitialization, @Nullable String schema) {
4243

43-
this.schemaInitializationEnabled = schemaInitializationEnabled;
44+
this.schemaInitialization = schemaInitialization;
4445
this.schema = schema;
4546
}
4647

4748
/**
4849
* Whether to initialize the JDBC event publication schema.
4950
*/
50-
public boolean isSchemaInitializationEnabled() {
51-
return schemaInitializationEnabled;
51+
public SchemaInitialization getSchemaInitialization() {
52+
return schemaInitialization;
5253
}
5354

5455
/**
5556
* The name of the schema where the event publication table resides.
5657
*
5758
* @return can be {@literal null}.
5859
*/
60+
@Nullable
5961
public String getSchema() {
6062
return schema;
6163
}
64+
65+
void verify(DatabaseType databaseType) {
66+
67+
if (schema != null && databaseType.equals(DatabaseType.MYSQL)) {
68+
throw new IllegalStateException(DatabaseType.SCHEMA_NOT_SUPPORTED);
69+
}
70+
}
71+
72+
static class SchemaInitialization {
73+
74+
private final boolean enabled;
75+
76+
/**
77+
* @param enabled
78+
*/
79+
@ConstructorBinding
80+
SchemaInitialization(@DefaultValue("false") boolean enabled) {
81+
this.enabled = enabled;
82+
}
83+
84+
boolean isEnabled() {
85+
return enabled;
86+
}
87+
}
6288
}

0 commit comments

Comments
 (0)