feat(mssqlserver): support GO batch separator in init scripts - #11960
Open
klouds27 wants to merge 1 commit into
Open
feat(mssqlserver): support GO batch separator in init scripts#11960klouds27 wants to merge 1 commit into
klouds27 wants to merge 1 commit into
Conversation
…ntainers#11231) adds ScriptUtils.normalizeGoSeparator() which replaces standalone GO lines with ; using a regex that matches GO case-insensitively on its own line, guarding against false matches on identifiers like GOOD or GOTO. introduces a preprocessInitScript hook on JdbcDatabaseContainer so subclasses can transform script content before execution. the default implementation is a no-op. MSSQLServerContainer (both the current and the deprecated legacy class) override it to call normalizeGoSeparator, enabling scripts exported from sqlcmd or SSMS to work without manual editing. Signed-off-by: klouds27 <adalwolf@gmail.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds an init-script preprocessing hook to the JDBC container flow and uses it to support MSSQL GO batch separators by normalizing them before execution.
Changes:
- Introduce
JdbcDatabaseContainer#preprocessInitScriptand wire it into init script execution. - Add
ScriptUtils.normalizeGoSeparator(...)plus a newrunInitScript(...)overload that accepts a script preprocessor. - Override preprocessing in both MSSQLServerContainer variants to normalize
GO, and add unit tests for normalization.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| modules/mssqlserver/src/main/java/org/testcontainers/mssqlserver/MSSQLServerContainer.java | Override init script preprocessing to normalize MSSQL GO separators. |
| modules/mssqlserver/src/main/java/org/testcontainers/containers/MSSQLServerContainer.java | Same override in the alternate package container implementation. |
| modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java | Add preprocess hook and pass it into init script execution. |
| modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java | Add GO normalization and a preprocessor-aware runInitScript overload. |
| modules/database-commons/src/test/java/org/testcontainers/ext/ScriptSplittingTest.java | Add tests validating GO normalization behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| */ | ||
| public static final String GO_STATEMENT_SEPARATOR = "GO"; | ||
|
|
||
| private static final Pattern GO_SEPARATOR_PATTERN = Pattern.compile("(?im)^[ \\t]*GO[ \\t]*$"); |
Comment on lines
+212
to
+214
| public static String normalizeGoSeparator(String script) { | ||
| return GO_SEPARATOR_PATTERN.matcher(script).replaceAll(";"); | ||
| } |
Comment on lines
+482
to
+488
| @Test | ||
| void testNormalizeGoSeparatorBasic() { | ||
| String script = "SELECT 1\nGO\nSELECT 2\nGO\n"; | ||
| String normalized = ScriptUtils.normalizeGoSeparator(script); | ||
| List<String> statements = doSplit(normalized, ScriptUtils.DEFAULT_STATEMENT_SEPARATOR); | ||
| assertThat(statements).containsExactly("SELECT 1", "SELECT 2"); | ||
| } |
Comment on lines
+223
to
+249
| public static void runInitScript( | ||
| DatabaseDelegate databaseDelegate, | ||
| String initScriptPath, | ||
| UnaryOperator<String> scriptPreprocessor | ||
| ) { | ||
| 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); | ||
| scripts = scriptPreprocessor.apply(scripts); | ||
| executeDatabaseScript(databaseDelegate, initScriptPath, scripts); | ||
| } 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); | ||
| } | ||
| } |
Comment on lines
+74
to
+79
| /** | ||
| * T-SQL batch separator used by Microsoft SQL Server tooling such as {@code sqlcmd} and SSMS. | ||
| * Pass this as the {@code separator} argument when executing scripts that use {@code GO} as a batch | ||
| * delimiter instead of {@code ;}. | ||
| */ | ||
| public static final String GO_STATEMENT_SEPARATOR = "GO"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #11231
Scripts exported from sqlcmd or SSMS use GO as a batch separator, which is not valid SQL and causes init script execution to fail. This change makes MSSQLServerContainer handle those scripts transparently.
ScriptUtils.normalizeGoSeparator() replaces standalone GO lines with ; using a regex that matches GO case-insensitively on its own line. The regex guards against false matches on identifiers like GOOD or GOTO_COL.
A preprocessInitScript hook is added to JdbcDatabaseContainer as a protected method returning the script unchanged by default. MSSQLServerContainer overrides it to call normalizeGoSeparator. Both the current and the deprecated legacy container class are updated.
Four unit tests cover basic replacement, case insensitivity, leading whitespace, and the inline identifier guard.