diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlDataSourceScriptDatabaseInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlDataSourceScriptDatabaseInitializer.java index a4b0e69f7c..a56edaf6ad 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlDataSourceScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlDataSourceScriptDatabaseInitializer.java @@ -20,6 +20,7 @@ import javax.sql.DataSource; import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; import org.springframework.boot.sql.init.DatabaseInitializationSettings; +import org.springframework.context.annotation.ImportRuntimeHints; /** * {@link DataSourceScriptDatabaseInitializer} for the primary SQL database. May be @@ -29,6 +30,7 @@ import org.springframework.boot.sql.init.DatabaseInitializationSettings; * @author Phillip Webb * @since 2.6.0 */ +@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class) public class SqlDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer { /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHints.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHints.java new file mode 100644 index 0000000000..3469a49764 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHints.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.sql.init; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; + +/** + * {@link RuntimeHintsRegistrar} for SQL initialization scripts. + * + * @author Moritz Halbritter + */ +class SqlInitializationScriptsRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.resources().registerPattern("schema.sql").registerPattern("schema-*.sql"); + hints.resources().registerPattern("data.sql").registerPattern("data-*.sql"); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlR2dbcScriptDatabaseInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlR2dbcScriptDatabaseInitializer.java index a3730c466e..24416232dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlR2dbcScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/sql/init/SqlR2dbcScriptDatabaseInitializer.java @@ -21,6 +21,7 @@ import io.r2dbc.spi.ConnectionFactory; import org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer; import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer; import org.springframework.boot.sql.init.DatabaseInitializationSettings; +import org.springframework.context.annotation.ImportRuntimeHints; /** * {@link R2dbcScriptDatabaseInitializer} for the primary SQL database. May be registered @@ -30,6 +31,7 @@ import org.springframework.boot.sql.init.DatabaseInitializationSettings; * @author Phillip Webb * @since 2.6.0 */ +@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class) public class SqlR2dbcScriptDatabaseInitializer extends R2dbcScriptDatabaseInitializer { /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHintsTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHintsTests.java new file mode 100644 index 0000000000..bd38db406f --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sql/init/SqlInitializationScriptsRuntimeHintsTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.sql.init; + +import org.junit.jupiter.api.Test; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SqlInitializationScriptsRuntimeHints}. + * + * @author Moritz Halbritter + */ +class SqlInitializationScriptsRuntimeHintsTests { + + @Test + void shouldRegisterSchemaHints() { + RuntimeHints hints = new RuntimeHints(); + new SqlInitializationScriptsRuntimeHints().registerHints(hints, getClass().getClassLoader()); + assertThat(RuntimeHintsPredicates.resource().forResource("schema.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("schema-all.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("schema-mysql.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("schema-postgres.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("schema-oracle.sql")).accepts(hints); + } + + @Test + void shouldRegisterDataHints() { + RuntimeHints hints = new RuntimeHints(); + new SqlInitializationScriptsRuntimeHints().registerHints(hints, getClass().getClassLoader()); + assertThat(RuntimeHintsPredicates.resource().forResource("data.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("data-all.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("data-mysql.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("data-postgres.sql")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("data-oracle.sql")).accepts(hints); + } + +}