Skip to content

Commit 63dad19

Browse files
committed
GH-1450 - Register SQL schemas as native image resources.
1 parent 44cb375 commit 63dad19

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.jdbc;
17+
18+
import java.util.stream.Stream;
19+
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
22+
import org.springframework.lang.Nullable;
23+
24+
/**
25+
* AOT resource hints for the database schemas.
26+
*
27+
* @author Oliver Drotbohm
28+
*/
29+
class DatabaseSchemaHints implements RuntimeHintsRegistrar {
30+
31+
/*
32+
* (non-Javadoc)
33+
* @see org.springframework.aot.hint.RuntimeHintsRegistrar#registerHints(org.springframework.aot.hint.RuntimeHints, java.lang.ClassLoader)
34+
*/
35+
@Override
36+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
37+
38+
Stream.of(DatabaseType.values())
39+
.map(DatabaseType::name)
40+
.map(String::toLowerCase)
41+
.flatMap(it -> Stream.of("schema-" + it + ".sql", "schema-" + it + "-archive.sql"))
42+
.forEach(hints.resources()::registerPattern);
43+
}
44+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ boolean isSchemaSupported() {
108108
};
109109

110110
static final String SCHEMA_NOT_SUPPORTED = "Setting the schema name is not supported!";
111+
static final String SCHEMA_ROOT = "schema-";
111112

112113
static DatabaseType from(String productName) {
113114

@@ -136,10 +137,12 @@ UUID databaseToUUID(Object id) {
136137
}
137138

138139
String getSchemaResourceFilename() {
139-
return "/schema-" + value + ".sql";
140+
return SCHEMA_ROOT + value + ".sql";
140141
}
141142

142-
String getArchiveSchemaResourceFilename() { return "/schema-" + value + "-archive.sql"; }
143+
String getArchiveSchemaResourceFilename() {
144+
return SCHEMA_ROOT + value + "-archive.sql";
145+
}
143146

144147
boolean isSchemaSupported() {
145148
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.jdbc;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.function.Predicate;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.aot.hint.ResourcePatternHint;
24+
import org.springframework.aot.hint.ResourcePatternHints;
25+
import org.springframework.aot.hint.RuntimeHints;
26+
27+
/**
28+
* Unit tests for {@link DatabaseSchemaHints}.
29+
*
30+
* @author Oliver Drotbohm
31+
*/
32+
class DatabaseSchemaHintsUnitTests {
33+
34+
@Test // GH-1450
35+
void registersRuntimeHintsForSchemas() {
36+
37+
var registrar = new DatabaseSchemaHints();
38+
var hints = new RuntimeHints();
39+
40+
registrar.registerHints(hints, DatabaseSchemaHintsUnitTests.class.getClassLoader());
41+
42+
assertThat(hints.resources().resourcePatternHints())
43+
.flatExtracting(ResourcePatternHints::getIncludes)
44+
.extracting(ResourcePatternHint::getPattern)
45+
.filteredOn(Predicate.not("/"::equals))
46+
.hasSize(DatabaseType.values().length * 2)
47+
.allSatisfy(it -> {
48+
assertThat(it).matches("schema-.*\\.sql");
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)