Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,46 @@ public static void runInitScript(DatabaseDelegate databaseDelegate, String initS
}
}

/**
* Load script from classpath and apply it to the given database
*
* @param databaseDelegate database delegate for script execution
* @param initScriptPath the resource to load the init script from
* @param separator the statement separator to use
*/
public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath, String separator) {
try {
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
if (resource == null) {
resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath);
if (resource == null) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException(
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
);
}
}
String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8);
executeDatabaseScript(
databaseDelegate,
initScriptPath,
scripts,
false,
false,
DEFAULT_COMMENT_PREFIX,
separator,
DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER
);
} catch (IOException e) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e);
} catch (ScriptException e) {
LOGGER.error("Error while executing init script: {}", initScriptPath, e);
throw new UncategorizedScriptException("Error while executing init script: " + initScriptPath, e);
}
}

public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script)
throws ScriptException {
executeDatabaseScript(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,29 @@ protected void optionallyMapResourceParameterAsVolume(
}
}

/**
* Load init script content and apply it to the database if initScriptPath is set
*/
protected void runInitScriptIfRequired() {
initScriptPaths
.stream()
.filter(Objects::nonNull)
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
}

/**
* Load init script content and apply it to the database if initScriptPath is set
*/
protected void runInitScriptIfRequired() {
initScriptPaths
.stream()
.filter(Objects::nonNull)
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path, getStatementSeparator()));
}

/**
* Returns the statement separator for SQL scripts.
* Override this method to use a different separator (e.g. "GO" for MSSQL).
*
* @return the statement separator, defaults to {@link ScriptUtils#DEFAULT_STATEMENT_SEPARATOR}
*/
protected String getStatementSeparator() {
return ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
}




public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,11 @@ private void checkPasswordStrength(String password) {
"or percent (%)."
);
}

}
@Override
protected String getStatementSeparator() {
return "GO";
}

}