Skip to content

Commit 575fd26

Browse files
committed
feat(mssqlserver): support GO as default batch separator
Signed-off-by: dahyvuun <dahyvuun@gmail.com>
1 parent 990a7dc commit 575fd26

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,46 @@ public static void runInitScript(DatabaseDelegate databaseDelegate, String initS
220220
}
221221
}
222222

223+
/**
224+
* Load script from classpath and apply it to the given database
225+
*
226+
* @param databaseDelegate database delegate for script execution
227+
* @param initScriptPath the resource to load the init script from
228+
* @param separator the statement separator to use
229+
*/
230+
public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath, String separator) {
231+
try {
232+
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
233+
if (resource == null) {
234+
resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath);
235+
if (resource == null) {
236+
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
237+
throw new ScriptLoadException(
238+
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
239+
);
240+
}
241+
}
242+
String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8);
243+
executeDatabaseScript(
244+
databaseDelegate,
245+
initScriptPath,
246+
scripts,
247+
false,
248+
false,
249+
DEFAULT_COMMENT_PREFIX,
250+
separator,
251+
DEFAULT_BLOCK_COMMENT_START_DELIMITER,
252+
DEFAULT_BLOCK_COMMENT_END_DELIMITER
253+
);
254+
} catch (IOException e) {
255+
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
256+
throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e);
257+
} catch (ScriptException e) {
258+
LOGGER.error("Error while executing init script: {}", initScriptPath, e);
259+
throw new UncategorizedScriptException("Error while executing init script: " + initScriptPath, e);
260+
}
261+
}
262+
223263
public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script)
224264
throws ScriptException {
225265
executeDatabaseScript(

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,29 @@ protected void optionallyMapResourceParameterAsVolume(
358358
}
359359
}
360360

361-
/**
362-
* Load init script content and apply it to the database if initScriptPath is set
363-
*/
364-
protected void runInitScriptIfRequired() {
365-
initScriptPaths
366-
.stream()
367-
.filter(Objects::nonNull)
368-
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
369-
}
361+
362+
/**
363+
* Load init script content and apply it to the database if initScriptPath is set
364+
*/
365+
protected void runInitScriptIfRequired() {
366+
initScriptPaths
367+
.stream()
368+
.filter(Objects::nonNull)
369+
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path, getStatementSeparator()));
370+
}
371+
372+
/**
373+
* Returns the statement separator for SQL scripts.
374+
* Override this method to use a different separator (e.g. "GO" for MSSQL).
375+
*
376+
* @return the statement separator, defaults to {@link ScriptUtils#DEFAULT_STATEMENT_SEPARATOR}
377+
*/
378+
protected String getStatementSeparator() {
379+
return ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
380+
}
381+
382+
383+
370384

371385
public void setParameters(Map<String, String> parameters) {
372386
this.parameters = parameters;

modules/mssqlserver/src/main/java/org/testcontainers/mssqlserver/MSSQLServerContainer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,11 @@ private void checkPasswordStrength(String password) {
152152
"or percent (%)."
153153
);
154154
}
155+
156+
}
157+
@Override
158+
protected String getStatementSeparator() {
159+
return "GO";
155160
}
161+
156162
}

0 commit comments

Comments
 (0)