Rename DDL/DML script location properties

See gh-25756
pull/25815/head
Andy Wilkinson 4 years ago
parent 160cd0d2c5
commit 5758a5aa39

@ -83,7 +83,7 @@ class DataSourceInitializationConfiguration {
ScriptDataSourceInitializer ddlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource, ScriptDataSourceInitializer ddlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource,
DataSourceProperties properties, ResourceLoader resourceLoader) { DataSourceProperties properties, ResourceLoader resourceLoader) {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setDdlScriptLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform())); settings.setSchemaLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform()));
settings.setContinueOnError(properties.isContinueOnError()); settings.setContinueOnError(properties.isContinueOnError());
settings.setSeparator(properties.getSeparator()); settings.setSeparator(properties.getSeparator());
settings.setEncoding(properties.getSqlScriptEncoding()); settings.setEncoding(properties.getSqlScriptEncoding());
@ -98,7 +98,7 @@ class DataSourceInitializationConfiguration {
ScriptDataSourceInitializer dmlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource, ScriptDataSourceInitializer dmlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource,
DataSourceProperties properties, ResourceLoader resourceLoader) { DataSourceProperties properties, ResourceLoader resourceLoader) {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setDmlScriptLocations(scriptLocations(properties.getData(), "data", properties.getPlatform())); settings.setDataLocations(scriptLocations(properties.getData(), "data", properties.getPlatform()));
settings.setContinueOnError(properties.isContinueOnError()); settings.setContinueOnError(properties.isContinueOnError());
settings.setSeparator(properties.getSeparator()); settings.setSeparator(properties.getSeparator());
settings.setEncoding(properties.getSqlScriptEncoding()); settings.setEncoding(properties.getSqlScriptEncoding());
@ -138,8 +138,8 @@ class DataSourceInitializationConfiguration {
ScriptDataSourceInitializer scriptDataSourceInitializer(DataSource dataSource, DataSourceProperties properties, ScriptDataSourceInitializer scriptDataSourceInitializer(DataSource dataSource, DataSourceProperties properties,
ResourceLoader resourceLoader) { ResourceLoader resourceLoader) {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setDdlScriptLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform())); settings.setSchemaLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform()));
settings.setDmlScriptLocations(scriptLocations(properties.getData(), "data", properties.getPlatform())); settings.setDataLocations(scriptLocations(properties.getData(), "data", properties.getPlatform()));
settings.setContinueOnError(properties.isContinueOnError()); settings.setContinueOnError(properties.isContinueOnError());
settings.setSeparator(properties.getSeparator()); settings.setSeparator(properties.getSeparator());
settings.setEncoding(properties.getSqlScriptEncoding()); settings.setEncoding(properties.getSqlScriptEncoding());

@ -243,7 +243,7 @@ class DataSourceInitializationIntegrationTests {
assertThat(context).hasFailed(); assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class); assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class);
assertThat(context.getStartupFailure()) assertThat(context.getStartupFailure())
.hasMessageContaining("No DDL scripts found at location 'classpath:does/not/exist.sql'"); .hasMessageContaining("No schema scripts found at location 'classpath:does/not/exist.sql'");
}); });
} }
@ -255,7 +255,7 @@ class DataSourceInitializationIntegrationTests {
assertThat(context).hasFailed(); assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class); assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class);
assertThat(context.getStartupFailure()) assertThat(context.getStartupFailure())
.hasMessageContaining("No DML scripts found at location 'classpath:does/not/exist.sql'"); .hasMessageContaining("No data scripts found at location 'classpath:does/not/exist.sql'");
}); });
} }

@ -29,9 +29,9 @@ import javax.sql.DataSource;
*/ */
public class DataSourceInitializationSettings { public class DataSourceInitializationSettings {
private List<String> ddlScriptLocations; private List<String> schemaLocations;
private List<String> dmlScriptLocations; private List<String> dataLocations;
private boolean continueOnError = false; private boolean continueOnError = false;
@ -40,43 +40,43 @@ public class DataSourceInitializationSettings {
private Charset encoding; private Charset encoding;
/** /**
* Returns the locations of the DDL (schema) scripts to apply to the database. * Returns the locations of the schema (DDL) scripts to apply to the database.
* @return the locations of the DDL scripts * @return the locations of the schema scripts
*/ */
public List<String> getDdlScriptLocations() { public List<String> getSchemaLocations() {
return this.ddlScriptLocations; return this.schemaLocations;
} }
/** /**
* Sets the locations of DDL (schema) scripts to apply to the database. By default, * Sets the locations of schema (DDL) scripts to apply to the database. By default,
* initialization will fail if a location does not exist. To prevent a failure, a * initialization will fail if a location does not exist. To prevent a failure, a
* location can be made optional by prefixing it with {@code optional:}. * location can be made optional by prefixing it with {@code optional:}.
* @param ddlScriptLocations locations of the DDL scripts * @param schemaLocations locations of the schema scripts
*/ */
public void setDdlScriptLocations(List<String> ddlScriptLocations) { public void setSchemaLocations(List<String> schemaLocations) {
this.ddlScriptLocations = ddlScriptLocations; this.schemaLocations = schemaLocations;
} }
/** /**
* Returns the locations of the DML (data) scripts to apply to the database. * Returns the locations of data (DML) scripts to apply to the database.
* @return the locations of the DML scripts * @return the locations of the data scripts
*/ */
public List<String> getDmlScriptLocations() { public List<String> getDataLocations() {
return this.dmlScriptLocations; return this.dataLocations;
} }
/** /**
* Sets the locations of DML (data) scripts to apply to the database. By default, * Sets the locations of data (DML) scripts to apply to the database. By default,
* initialization will fail if a location does not exist. To prevent a failure, a * initialization will fail if a location does not exist. To prevent a failure, a
* location can be made optional by prefixing it with {@code optional:}. * location can be made optional by prefixing it with {@code optional:}.
* @param dmlScriptLocations locations of the DML scripts * @param dataLocations locations of the data scripts
*/ */
public void setDmlScriptLocations(List<String> dmlScriptLocations) { public void setDataLocations(List<String> dataLocations) {
this.dmlScriptLocations = dmlScriptLocations; this.dataLocations = dataLocations;
} }
/** /**
* Returns whether to continue when an error occurs while applying a DDL or DML * Returns whether to continue when an error occurs while applying a schema or data
* script. * script.
* @return whether to continue on error * @return whether to continue on error
*/ */
@ -86,7 +86,7 @@ public class DataSourceInitializationSettings {
/** /**
* Sets whether initialization should continue when an error occurs when applying a * Sets whether initialization should continue when an error occurs when applying a
* DDL or DML script. * schema or data script.
* @param continueOnError whether to continue when an error occurs. * @param continueOnError whether to continue when an error occurs.
*/ */
public void setContinueOnError(boolean continueOnError) { public void setContinueOnError(boolean continueOnError) {
@ -94,7 +94,7 @@ public class DataSourceInitializationSettings {
} }
/** /**
* Returns the statement separator used in the DDL and DML scripts. * Returns the statement separator used in the schema and data scripts.
* @return the statement separator * @return the statement separator
*/ */
public String getSeparator() { public String getSeparator() {
@ -102,15 +102,15 @@ public class DataSourceInitializationSettings {
} }
/** /**
* Sets the statement separator to use when reading the DDL and DML scripts. * Sets the statement separator to use when reading the schema and data scripts.
* @param separator statement separator used in DDL and DML scripts * @param separator statement separator used in the schema and data scripts
*/ */
public void setSeparator(String separator) { public void setSeparator(String separator) {
this.separator = separator; this.separator = separator;
} }
/** /**
* Returns the encoding to use when reading the DDL and DML scripts. * Returns the encoding to use when reading the schema and data scripts.
* @return the script encoding * @return the script encoding
*/ */
public Charset getEncoding() { public Charset getEncoding() {
@ -118,8 +118,8 @@ public class DataSourceInitializationSettings {
} }
/** /**
* Sets the encoding to use when reading the DDL and DML scripts. * Sets the encoding to use when reading the schema and data scripts.
* @param encoding encoding to use when reading the DDL and DML scripts * @param encoding encoding to use when reading the schema and data scripts
*/ */
public void setEncoding(Charset encoding) { public void setEncoding(Charset encoding) {
this.encoding = encoding; this.encoding = encoding;

@ -36,8 +36,8 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
* {@link InitializingBean} that performs {@link DataSource} initialization using DDL and * {@link InitializingBean} that performs {@link DataSource} initialization using schema
* DML scripts. * (DDL) and data (DML) scripts.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.5.0 * @since 2.5.0
@ -82,23 +82,23 @@ public class ScriptDataSourceInitializer implements ResourceLoaderAware, Initial
} }
/** /**
* Initializes the database by running DDL and DML scripts. * Initializes the database by applying schema and data scripts.
* @return {@code true} if one or more scripts were applied to the database, otherwise * @return {@code true} if one or more scripts were applied to the database, otherwise
* {@code false} * {@code false}
*/ */
public boolean initializeDatabase() { public boolean initializeDatabase() {
ScriptLocationResolver locationResolver = new ScriptLocationResolver(this.resourceLoader); ScriptLocationResolver locationResolver = new ScriptLocationResolver(this.resourceLoader);
boolean initialized = applyDdlScripts(locationResolver); boolean initialized = applySchemaScripts(locationResolver);
initialized = applyDmlScripts(locationResolver) || initialized; initialized = applyDataScripts(locationResolver) || initialized;
return initialized; return initialized;
} }
private boolean applyDdlScripts(ScriptLocationResolver locationResolver) { private boolean applySchemaScripts(ScriptLocationResolver locationResolver) {
return applyScripts(this.settings.getDdlScriptLocations(), "DDL", locationResolver); return applyScripts(this.settings.getSchemaLocations(), "schema", locationResolver);
} }
private boolean applyDmlScripts(ScriptLocationResolver locationResolver) { private boolean applyDataScripts(ScriptLocationResolver locationResolver) {
return applyScripts(this.settings.getDmlScriptLocations(), "DML", locationResolver); return applyScripts(this.settings.getDataLocations(), "data", locationResolver);
} }
private boolean applyScripts(List<String> locations, String type, ScriptLocationResolver locationResolver) { private boolean applyScripts(List<String> locations, String type, ScriptLocationResolver locationResolver) {

@ -46,10 +46,10 @@ class ScriptDataSourceInitializerTests {
} }
@Test @Test
void whenDatabaseIsInitializedThenDdlAndDmlScriptsAreApplied() { void whenDatabaseIsInitializedThenSchemaAndDataScriptsAreApplied() {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setDdlScriptLocations(Arrays.asList("schema.sql")); settings.setSchemaLocations(Arrays.asList("schema.sql"));
settings.setDmlScriptLocations(Arrays.asList("data.sql")); settings.setDataLocations(Arrays.asList("data.sql"));
ScriptDataSourceInitializer initializer = createInitializer(settings); ScriptDataSourceInitializer initializer = createInitializer(settings);
assertThat(initializer.initializeDatabase()).isTrue(); assertThat(initializer.initializeDatabase()).isTrue();
assertThat(numberOfRows("SELECT COUNT(*) FROM EXAMPLE")).isEqualTo(1); assertThat(numberOfRows("SELECT COUNT(*) FROM EXAMPLE")).isEqualTo(1);
@ -58,7 +58,7 @@ class ScriptDataSourceInitializerTests {
@Test @Test
void whenContinueOnErrorIsFalseThenInitializationFailsOnError() { void whenContinueOnErrorIsFalseThenInitializationFailsOnError() {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setDmlScriptLocations(Arrays.asList("data.sql")); settings.setDataLocations(Arrays.asList("data.sql"));
ScriptDataSourceInitializer initializer = createInitializer(settings); ScriptDataSourceInitializer initializer = createInitializer(settings);
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> initializer.initializeDatabase()); assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> initializer.initializeDatabase());
} }
@ -67,7 +67,7 @@ class ScriptDataSourceInitializerTests {
void whenContinueOnErrorIsTrueThenInitializationDoesNotFailOnError() { void whenContinueOnErrorIsTrueThenInitializationDoesNotFailOnError() {
DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
settings.setContinueOnError(true); settings.setContinueOnError(true);
settings.setDmlScriptLocations(Arrays.asList("data.sql")); settings.setDataLocations(Arrays.asList("data.sql"));
ScriptDataSourceInitializer initializer = createInitializer(settings); ScriptDataSourceInitializer initializer = createInitializer(settings);
assertThat(initializer.initializeDatabase()).isTrue(); assertThat(initializer.initializeDatabase()).isTrue();
} }

Loading…
Cancel
Save