Create spring-boot-sql module

This commit is contained in:
Andy Wilkinson
2025-04-04 15:20:14 +01:00
committed by Phillip Webb
parent e288c81b7b
commit 837cf467e0
47 changed files with 76 additions and 37 deletions

View File

@@ -1,45 +0,0 @@
/*
* Copyright 2012-2025 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.boot.sql.init.AbstractScriptDatabaseInitializer;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.context.annotation.ImportRuntimeHints;
/**
* Marker interface for a script-based database initializer that initializes the
* application's database.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @since 4.0.0
* @see AbstractScriptDatabaseInitializer
*/
@ImportRuntimeHints(SqlInitializationScriptsRuntimeHints.class)
public interface ApplicationScriptDatabaseInitializer {
/**
* Adapts {@link SqlInitializationProperties} to
* {@link DatabaseInitializationSettings}.
* @param properties the properties to adapt
* @return the settings
*/
static DatabaseInitializationSettings getSettings(SqlInitializationProperties properties) {
return SettingsCreator.createFrom(properties);
}
}

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2012-2025 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 java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
import org.springframework.context.annotation.Conditional;
/**
* Condition that matches when {@code spring.sql.init.mode} is set to a value other than
* {@link DatabaseInitializationMode#NEVER}.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @since 4.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Conditional(OnSqlInitializationCondition.class)
public @interface ConditionalOnSqlInitialization {
}

View File

@@ -1,90 +0,0 @@
/*
* Copyright 2012-2025 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 java.util.Locale;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
/**
* Condition that checks if the database initialization of a particular component should
* be considered.
*
* @author Stephane Nicoll
* @since 2.6.2
* @see DatabaseInitializationMode
*/
public abstract class OnDatabaseInitializationCondition extends SpringBootCondition {
private final String name;
private final String[] propertyNames;
/**
* Create a new instance with the name of the component and the property names to
* check, in order. If a property is set, its value is used to determine the outcome
* and remaining properties are not tested.
* @param name the name of the component
* @param propertyNames the properties to check (in order)
*/
protected OnDatabaseInitializationCondition(String name, String... propertyNames) {
this.name = name;
this.propertyNames = propertyNames;
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String propertyName = getConfiguredProperty(environment);
DatabaseInitializationMode mode = getDatabaseInitializationMode(environment, propertyName);
boolean match = match(mode);
String messagePrefix = (propertyName != null) ? propertyName : "default value";
return new ConditionOutcome(match, ConditionMessage.forCondition(this.name + "Database Initialization")
.because(messagePrefix + " is " + mode));
}
private boolean match(DatabaseInitializationMode mode) {
return !mode.equals(DatabaseInitializationMode.NEVER);
}
private DatabaseInitializationMode getDatabaseInitializationMode(Environment environment, String propertyName) {
if (StringUtils.hasText(propertyName)) {
String candidate = environment.getProperty(propertyName, "embedded").toUpperCase(Locale.ENGLISH);
if (StringUtils.hasText(candidate)) {
return DatabaseInitializationMode.valueOf(candidate);
}
}
return DatabaseInitializationMode.EMBEDDED;
}
private String getConfiguredProperty(Environment environment) {
for (String propertyName : this.propertyNames) {
if (environment.containsProperty(propertyName)) {
return propertyName;
}
}
return null;
}
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright 2012-2025 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.context.annotation.Condition;
/**
* {@link Condition} implementation for {@link ConditionalOnSqlInitialization}.
*
* @author Andy Wilkinson
* @author Phillip Webb
*/
class OnSqlInitializationCondition extends OnDatabaseInitializationCondition {
OnSqlInitializationCondition() {
super("SQL Initialization", "spring.sql.init.mode");
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2012-2023 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 java.util.ArrayList;
import java.util.List;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
/**
* Helpers class for creating {@link DatabaseInitializationSettings} from
* {@link SqlInitializationProperties}.
*
* @author Andy Wilkinson
*/
final class SettingsCreator {
private SettingsCreator() {
}
static DatabaseInitializationSettings createFrom(SqlInitializationProperties properties) {
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
settings
.setSchemaLocations(scriptLocations(properties.getSchemaLocations(), "schema", properties.getPlatform()));
settings.setDataLocations(scriptLocations(properties.getDataLocations(), "data", properties.getPlatform()));
settings.setContinueOnError(properties.isContinueOnError());
settings.setSeparator(properties.getSeparator());
settings.setEncoding(properties.getEncoding());
settings.setMode(properties.getMode());
return settings;
}
private static List<String> scriptLocations(List<String> locations, String fallback, String platform) {
if (locations != null) {
return locations;
}
List<String> fallbackLocations = new ArrayList<>();
fallbackLocations.add("optional:classpath*:" + fallback + "-" + platform + ".sql");
fallbackLocations.add("optional:classpath*:" + fallback + ".sql");
return fallbackLocations;
}
}

View File

@@ -1,155 +0,0 @@
/*
* Copyright 2012-2021 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 java.nio.charset.Charset;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
/**
* {@link ConfigurationProperties Configuration properties} for initializing an SQL
* database.
*
* @author Andy Wilkinson
* @since 2.5.0
*/
@ConfigurationProperties("spring.sql.init")
public class SqlInitializationProperties {
/**
* Locations of the schema (DDL) scripts to apply to the database.
*/
private List<String> schemaLocations;
/**
* Locations of the data (DML) scripts to apply to the database.
*/
private List<String> dataLocations;
/**
* Platform to use in the default schema or data script locations,
* schema-${platform}.sql and data-${platform}.sql.
*/
private String platform = "all";
/**
* Username of the database to use when applying initialization scripts (if
* different).
*/
private String username;
/**
* Password of the database to use when applying initialization scripts (if
* different).
*/
private String password;
/**
* Whether initialization should continue when an error occurs.
*/
private boolean continueOnError = false;
/**
* Statement separator in the schema and data scripts.
*/
private String separator = ";";
/**
* Encoding of the schema and data scripts.
*/
private Charset encoding;
/**
* Mode to apply when determining whether initialization should be performed.
*/
private DatabaseInitializationMode mode = DatabaseInitializationMode.EMBEDDED;
public List<String> getSchemaLocations() {
return this.schemaLocations;
}
public void setSchemaLocations(List<String> schemaLocations) {
this.schemaLocations = schemaLocations;
}
public List<String> getDataLocations() {
return this.dataLocations;
}
public void setDataLocations(List<String> dataLocations) {
this.dataLocations = dataLocations;
}
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isContinueOnError() {
return this.continueOnError;
}
public void setContinueOnError(boolean continueOnError) {
this.continueOnError = continueOnError;
}
public String getSeparator() {
return this.separator;
}
public void setSeparator(String separator) {
this.separator = separator;
}
public Charset getEncoding() {
return this.encoding;
}
public void setEncoding(Charset encoding) {
this.encoding = encoding;
}
public DatabaseInitializationMode getMode() {
return this.mode;
}
public void setMode(DatabaseInitializationMode mode) {
this.mode = mode;
}
}

View File

@@ -1,35 +0,0 @@
/*
* 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");
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2021 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.
*/
/**
* Auto-configuration for basic script-based initialization of an SQL database.
*/
package org.springframework.boot.autoconfigure.sql.init;

View File

@@ -327,16 +327,6 @@
"messages"
]
},
{
"name": "spring.sql.init.enabled",
"type": "java.lang.Boolean",
"description": "Whether basic script-based initialization of an SQL database is enabled.",
"defaultValue": true,
"deprecation": {
"replacement": "spring.sql.init.mode",
"level": "warning"
}
},
{
"name": "spring.threads.virtual.enabled",
"type": "java.lang.Boolean",

View File

@@ -1,118 +0,0 @@
/*
* Copyright 2012-2025 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.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link OnDatabaseInitializationCondition}.
*
* @author Stephane Nicoll
*/
class OnDatabaseInitializationConditionTests {
@Test
void getMatchOutcomeWithPropertyNoSetMatches() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.another", "noise")), null);
assertThat(outcome.isMatch()).isTrue();
}
@Test
void getMatchOutcomeWithPropertySetToAlwaysMatches() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-mode=always")), null);
assertThat(outcome.isMatch()).isTrue();
}
@Test
void getMatchOutcomeWithPropertySetToEmbeddedMatches() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-mode=embedded")), null);
assertThat(outcome.isMatch()).isTrue();
}
@Test
void getMatchOutcomeWithPropertySetToNeverDoesNotMatch() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-mode=never")), null);
assertThat(outcome.isMatch()).isFalse();
}
@Test
void getMatchOutcomeWithPropertySetToEmptyStringIsIgnored() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-mode")), null);
assertThat(outcome.isMatch()).isTrue();
}
@Test
void getMatchOutcomeWithMultiplePropertiesUsesFirstSet() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode",
"test.schema-mode", "test.init-schema-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-schema-mode=embedded")), null);
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage()).isEqualTo("TestDatabase Initialization test.init-schema-mode is EMBEDDED");
}
@Test
void getMatchOutcomeHasDedicatedDescription() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(TestPropertyValues.of("test.init-mode=embedded")), null);
assertThat(outcome.getMessage()).isEqualTo("TestDatabase Initialization test.init-mode is EMBEDDED");
}
@Test
void getMatchOutcomeHasWhenPropertyIsNotSetHasDefaultDescription() {
OnDatabaseInitializationCondition condition = new OnTestDatabaseInitializationCondition("test.init-mode");
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(TestPropertyValues.empty()), null);
assertThat(outcome.getMessage()).isEqualTo("TestDatabase Initialization default value is EMBEDDED");
}
private ConditionContext mockConditionContext(TestPropertyValues propertyValues) {
MockEnvironment environment = new MockEnvironment();
propertyValues.applyTo(environment);
ConditionContext conditionContext = mock(ConditionContext.class);
given(conditionContext.getEnvironment()).willReturn(environment);
return conditionContext;
}
static class OnTestDatabaseInitializationCondition extends OnDatabaseInitializationCondition {
OnTestDatabaseInitializationCondition(String... properties) {
super("Test", properties);
}
}
}

View File

@@ -1,55 +0,0 @@
/*
* 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);
}
}