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
pull/31828/head
Moritz Halbritter 2 years ago
parent f29eed89ca
commit c67876f913

@ -20,6 +20,7 @@ import javax.sql.DataSource;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer; import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.sql.init.DatabaseInitializationSettings; import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.context.annotation.ImportRuntimeHints;
/** /**
* {@link DataSourceScriptDatabaseInitializer} for the primary SQL database. May be * {@link DataSourceScriptDatabaseInitializer} for the primary SQL database. May be
@ -29,6 +30,7 @@ import org.springframework.boot.sql.init.DatabaseInitializationSettings;
* @author Phillip Webb * @author Phillip Webb
* @since 2.6.0 * @since 2.6.0
*/ */
@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class)
public class SqlDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer { public class SqlDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer {
/** /**

@ -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");
}
}

@ -21,6 +21,7 @@ import io.r2dbc.spi.ConnectionFactory;
import org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer; import org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer;
import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer; import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer;
import org.springframework.boot.sql.init.DatabaseInitializationSettings; import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.context.annotation.ImportRuntimeHints;
/** /**
* {@link R2dbcScriptDatabaseInitializer} for the primary SQL database. May be registered * {@link R2dbcScriptDatabaseInitializer} for the primary SQL database. May be registered
@ -30,6 +31,7 @@ import org.springframework.boot.sql.init.DatabaseInitializationSettings;
* @author Phillip Webb * @author Phillip Webb
* @since 2.6.0 * @since 2.6.0
*/ */
@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class)
public class SqlR2dbcScriptDatabaseInitializer extends R2dbcScriptDatabaseInitializer { public class SqlR2dbcScriptDatabaseInitializer extends R2dbcScriptDatabaseInitializer {
/** /**

@ -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);
}
}
Loading…
Cancel
Save