Skip to content

Commit c67876f

Browse files
committed
Add resource hints for schema and data scripts
This only registers the default locations, not the one users can provide via 'spring.sql.init.schema-locations' and 'spring.sql.init.data-locations'. Closes gh-31533
1 parent f29eed8 commit c67876f

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlDataSourceScriptDatabaseInitializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
2222
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
23+
import org.springframework.context.annotation.ImportRuntimeHints;
2324

2425
/**
2526
* {@link DataSourceScriptDatabaseInitializer} for the primary SQL database. May be
@@ -29,6 +30,7 @@
2930
* @author Phillip Webb
3031
* @since 2.6.0
3132
*/
33+
@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class)
3234
public class SqlDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer {
3335

3436
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2022 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+
17+
package org.springframework.boot.autoconfigure.sql.init;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
22+
/**
23+
* {@link RuntimeHintsRegistrar} for SQL initialization scripts.
24+
*
25+
* @author Moritz Halbritter
26+
*/
27+
class SqlInitializationScriptsRuntimeHints implements RuntimeHintsRegistrar {
28+
29+
@Override
30+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
31+
hints.resources().registerPattern("schema.sql").registerPattern("schema-*.sql");
32+
hints.resources().registerPattern("data.sql").registerPattern("data-*.sql");
33+
}
34+
35+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlR2dbcScriptDatabaseInitializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer;
2222
import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer;
2323
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
24+
import org.springframework.context.annotation.ImportRuntimeHints;
2425

2526
/**
2627
* {@link R2dbcScriptDatabaseInitializer} for the primary SQL database. May be registered
@@ -30,6 +31,7 @@
3031
* @author Phillip Webb
3132
* @since 2.6.0
3233
*/
34+
@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class)
3335
public class SqlR2dbcScriptDatabaseInitializer extends R2dbcScriptDatabaseInitializer {
3436

3537
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2022 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+
17+
package org.springframework.boot.autoconfigure.sql.init;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link SqlInitializationScriptsRuntimeHints}.
28+
*
29+
* @author Moritz Halbritter
30+
*/
31+
class SqlInitializationScriptsRuntimeHintsTests {
32+
33+
@Test
34+
void shouldRegisterSchemaHints() {
35+
RuntimeHints hints = new RuntimeHints();
36+
new SqlInitializationScriptsRuntimeHints().registerHints(hints, getClass().getClassLoader());
37+
assertThat(RuntimeHintsPredicates.resource().forResource("schema.sql")).accepts(hints);
38+
assertThat(RuntimeHintsPredicates.resource().forResource("schema-all.sql")).accepts(hints);
39+
assertThat(RuntimeHintsPredicates.resource().forResource("schema-mysql.sql")).accepts(hints);
40+
assertThat(RuntimeHintsPredicates.resource().forResource("schema-postgres.sql")).accepts(hints);
41+
assertThat(RuntimeHintsPredicates.resource().forResource("schema-oracle.sql")).accepts(hints);
42+
}
43+
44+
@Test
45+
void shouldRegisterDataHints() {
46+
RuntimeHints hints = new RuntimeHints();
47+
new SqlInitializationScriptsRuntimeHints().registerHints(hints, getClass().getClassLoader());
48+
assertThat(RuntimeHintsPredicates.resource().forResource("data.sql")).accepts(hints);
49+
assertThat(RuntimeHintsPredicates.resource().forResource("data-all.sql")).accepts(hints);
50+
assertThat(RuntimeHintsPredicates.resource().forResource("data-mysql.sql")).accepts(hints);
51+
assertThat(RuntimeHintsPredicates.resource().forResource("data-postgres.sql")).accepts(hints);
52+
assertThat(RuntimeHintsPredicates.resource().forResource("data-oracle.sql")).accepts(hints);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)