parent
160cd0d2c5
commit
5758a5aa39
|
@ -83,7 +83,7 @@ class DataSourceInitializationConfiguration {
|
|||
ScriptDataSourceInitializer ddlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource,
|
||||
DataSourceProperties properties, ResourceLoader resourceLoader) {
|
||||
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.setSeparator(properties.getSeparator());
|
||||
settings.setEncoding(properties.getSqlScriptEncoding());
|
||||
|
@ -98,7 +98,7 @@ class DataSourceInitializationConfiguration {
|
|||
ScriptDataSourceInitializer dmlOnlyScriptDataSourceInitializer(ObjectProvider<DataSource> dataSource,
|
||||
DataSourceProperties properties, ResourceLoader resourceLoader) {
|
||||
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.setSeparator(properties.getSeparator());
|
||||
settings.setEncoding(properties.getSqlScriptEncoding());
|
||||
|
@ -138,8 +138,8 @@ class DataSourceInitializationConfiguration {
|
|||
ScriptDataSourceInitializer scriptDataSourceInitializer(DataSource dataSource, DataSourceProperties properties,
|
||||
ResourceLoader resourceLoader) {
|
||||
DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
|
||||
settings.setDdlScriptLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform()));
|
||||
settings.setDmlScriptLocations(scriptLocations(properties.getData(), "data", properties.getPlatform()));
|
||||
settings.setSchemaLocations(scriptLocations(properties.getSchema(), "schema", properties.getPlatform()));
|
||||
settings.setDataLocations(scriptLocations(properties.getData(), "data", properties.getPlatform()));
|
||||
settings.setContinueOnError(properties.isContinueOnError());
|
||||
settings.setSeparator(properties.getSeparator());
|
||||
settings.setEncoding(properties.getSqlScriptEncoding());
|
||||
|
|
|
@ -243,7 +243,7 @@ class DataSourceInitializationIntegrationTests {
|
|||
assertThat(context).hasFailed();
|
||||
assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class);
|
||||
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.getStartupFailure()).isInstanceOf(BeanCreationException.class);
|
||||
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 {
|
||||
|
||||
private List<String> ddlScriptLocations;
|
||||
private List<String> schemaLocations;
|
||||
|
||||
private List<String> dmlScriptLocations;
|
||||
private List<String> dataLocations;
|
||||
|
||||
private boolean continueOnError = false;
|
||||
|
||||
|
@ -40,43 +40,43 @@ public class DataSourceInitializationSettings {
|
|||
private Charset encoding;
|
||||
|
||||
/**
|
||||
* Returns the locations of the DDL (schema) scripts to apply to the database.
|
||||
* @return the locations of the DDL scripts
|
||||
* Returns the locations of the schema (DDL) scripts to apply to the database.
|
||||
* @return the locations of the schema scripts
|
||||
*/
|
||||
public List<String> getDdlScriptLocations() {
|
||||
return this.ddlScriptLocations;
|
||||
public List<String> getSchemaLocations() {
|
||||
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
|
||||
* 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) {
|
||||
this.ddlScriptLocations = ddlScriptLocations;
|
||||
public void setSchemaLocations(List<String> schemaLocations) {
|
||||
this.schemaLocations = schemaLocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the locations of the DML (data) scripts to apply to the database.
|
||||
* @return the locations of the DML scripts
|
||||
* Returns the locations of data (DML) scripts to apply to the database.
|
||||
* @return the locations of the data scripts
|
||||
*/
|
||||
public List<String> getDmlScriptLocations() {
|
||||
return this.dmlScriptLocations;
|
||||
public List<String> getDataLocations() {
|
||||
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
|
||||
* 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) {
|
||||
this.dmlScriptLocations = dmlScriptLocations;
|
||||
public void setDataLocations(List<String> dataLocations) {
|
||||
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.
|
||||
* @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
|
||||
* DDL or DML script.
|
||||
* schema or data script.
|
||||
* @param continueOnError whether to continue when an error occurs.
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public String getSeparator() {
|
||||
|
@ -102,15 +102,15 @@ public class DataSourceInitializationSettings {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the statement separator to use when reading the DDL and DML scripts.
|
||||
* @param separator statement separator used in DDL and DML scripts
|
||||
* Sets the statement separator to use when reading the schema and data scripts.
|
||||
* @param separator statement separator used in the schema and data scripts
|
||||
*/
|
||||
public void setSeparator(String 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
|
||||
*/
|
||||
public Charset getEncoding() {
|
||||
|
@ -118,8 +118,8 @@ public class DataSourceInitializationSettings {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the encoding to use when reading the DDL and DML scripts.
|
||||
* @param encoding 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 schema and data scripts
|
||||
*/
|
||||
public void setEncoding(Charset encoding) {
|
||||
this.encoding = encoding;
|
||||
|
|
|
@ -36,8 +36,8 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
|||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* {@link InitializingBean} that performs {@link DataSource} initialization using DDL and
|
||||
* DML scripts.
|
||||
* {@link InitializingBean} that performs {@link DataSource} initialization using schema
|
||||
* (DDL) and data (DML) scripts.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @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
|
||||
* {@code false}
|
||||
*/
|
||||
public boolean initializeDatabase() {
|
||||
ScriptLocationResolver locationResolver = new ScriptLocationResolver(this.resourceLoader);
|
||||
boolean initialized = applyDdlScripts(locationResolver);
|
||||
initialized = applyDmlScripts(locationResolver) || initialized;
|
||||
boolean initialized = applySchemaScripts(locationResolver);
|
||||
initialized = applyDataScripts(locationResolver) || initialized;
|
||||
return initialized;
|
||||
}
|
||||
|
||||
private boolean applyDdlScripts(ScriptLocationResolver locationResolver) {
|
||||
return applyScripts(this.settings.getDdlScriptLocations(), "DDL", locationResolver);
|
||||
private boolean applySchemaScripts(ScriptLocationResolver locationResolver) {
|
||||
return applyScripts(this.settings.getSchemaLocations(), "schema", locationResolver);
|
||||
}
|
||||
|
||||
private boolean applyDmlScripts(ScriptLocationResolver locationResolver) {
|
||||
return applyScripts(this.settings.getDmlScriptLocations(), "DML", locationResolver);
|
||||
private boolean applyDataScripts(ScriptLocationResolver locationResolver) {
|
||||
return applyScripts(this.settings.getDataLocations(), "data", locationResolver);
|
||||
}
|
||||
|
||||
private boolean applyScripts(List<String> locations, String type, ScriptLocationResolver locationResolver) {
|
||||
|
|
|
@ -46,10 +46,10 @@ class ScriptDataSourceInitializerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void whenDatabaseIsInitializedThenDdlAndDmlScriptsAreApplied() {
|
||||
void whenDatabaseIsInitializedThenSchemaAndDataScriptsAreApplied() {
|
||||
DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
|
||||
settings.setDdlScriptLocations(Arrays.asList("schema.sql"));
|
||||
settings.setDmlScriptLocations(Arrays.asList("data.sql"));
|
||||
settings.setSchemaLocations(Arrays.asList("schema.sql"));
|
||||
settings.setDataLocations(Arrays.asList("data.sql"));
|
||||
ScriptDataSourceInitializer initializer = createInitializer(settings);
|
||||
assertThat(initializer.initializeDatabase()).isTrue();
|
||||
assertThat(numberOfRows("SELECT COUNT(*) FROM EXAMPLE")).isEqualTo(1);
|
||||
|
@ -58,7 +58,7 @@ class ScriptDataSourceInitializerTests {
|
|||
@Test
|
||||
void whenContinueOnErrorIsFalseThenInitializationFailsOnError() {
|
||||
DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
|
||||
settings.setDmlScriptLocations(Arrays.asList("data.sql"));
|
||||
settings.setDataLocations(Arrays.asList("data.sql"));
|
||||
ScriptDataSourceInitializer initializer = createInitializer(settings);
|
||||
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> initializer.initializeDatabase());
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ class ScriptDataSourceInitializerTests {
|
|||
void whenContinueOnErrorIsTrueThenInitializationDoesNotFailOnError() {
|
||||
DataSourceInitializationSettings settings = new DataSourceInitializationSettings();
|
||||
settings.setContinueOnError(true);
|
||||
settings.setDmlScriptLocations(Arrays.asList("data.sql"));
|
||||
settings.setDataLocations(Arrays.asList("data.sql"));
|
||||
ScriptDataSourceInitializer initializer = createInitializer(settings);
|
||||
assertThat(initializer.initializeDatabase()).isTrue();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue