From 9c3f492524fa8b21513a5bf0f9008f86ba114667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Michael?= Date: Fri, 29 Dec 2023 00:02:41 +0100 Subject: [PATCH] Support multiple SQL init scripts Close #2232 --- .../containers/JdbcDatabaseContainer.java | 34 ++++++------------- .../jdbc/ContainerDatabaseDriver.java | 25 +++++++------- .../jdbc/ConnectionUrlTest.java | 15 ++++++++ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java index cf6c995528f..8da09629818 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java +++ b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java @@ -17,8 +17,8 @@ import java.sql.Driver; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -39,7 +39,7 @@ public abstract class JdbcDatabaseContainer initScriptPaths = new ArrayList<>(); + private List initScriptPaths = Collections.emptyList(); protected Map parameters = new HashMap<>(); @@ -144,30 +144,17 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) { * @return self */ public SELF withInitScript(String initScriptPath) { - this.initScriptPaths = new ArrayList<>(); - this.initScriptPaths.add(initScriptPath); + this.initScriptPaths = Collections.singletonList(initScriptPath); return self(); } /** - * Sets an ordered array of scripts for initialization. + * Execute each init script in the given order * - * @param initScriptPaths paths to the script files - * @return self - */ - public SELF withInitScripts(String... initScriptPaths) { - return withInitScripts(Arrays.asList(initScriptPaths)); - } - - /** - * Sets an ordered collection of scripts for initialization. - * - * @param initScriptPaths paths to the script files - * @return self + * @since 1.20 */ - public SELF withInitScripts(Iterable initScriptPaths) { - this.initScriptPaths = new ArrayList<>(); - initScriptPaths.forEach(this.initScriptPaths::add); + public SELF withInitScript(String... initScriptPaths) { + this.initScriptPaths = Arrays.asList(initScriptPaths); return self(); } @@ -362,10 +349,9 @@ 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)); + for (String initScriptPath : initScriptPaths) { + ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath); + } } public void setParameters(Map parameters) { diff --git a/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java b/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java index 8f5f5f5a425..4181f6bafdf 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java +++ b/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java @@ -203,32 +203,33 @@ private Connection wrapConnection( */ private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, DatabaseDelegate databaseDelegate) throws SQLException { - if (connectionUrl.getInitScriptPath().isPresent()) { - String initScriptPath = connectionUrl.getInitScriptPath().get(); + if (!connectionUrl.getInitScriptPath().isPresent()) return; + + for (String scriptPath : connectionUrl.getInitScriptPath().get().split(",")) { try { URL resource; - if (initScriptPath.startsWith(FILE_PATH_PREFIX)) { + if (scriptPath.startsWith(FILE_PATH_PREFIX)) { //relative workdir path - resource = new URL(initScriptPath); + resource = new URL(scriptPath); } else { //classpath resource - resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath); + resource = Thread.currentThread().getContextClassLoader().getResource(scriptPath); } if (resource == null) { - LOGGER.warn("Could not load classpath init script: {}", initScriptPath); + LOGGER.warn("Could not load classpath init script: {}", scriptPath); throw new SQLException( - "Could not load classpath init script: " + initScriptPath + ". Resource not found." + "Could not load classpath init script: " + scriptPath + ". Resource not found." ); } String sql = IOUtils.toString(resource, StandardCharsets.UTF_8); - ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql); + ScriptUtils.executeDatabaseScript(databaseDelegate, scriptPath, sql); } catch (IOException e) { - LOGGER.warn("Could not load classpath init script: {}", initScriptPath); - throw new SQLException("Could not load classpath init script: " + initScriptPath, e); + LOGGER.warn("Could not load classpath init script: {}", scriptPath); + throw new SQLException("Could not load classpath init script: " + scriptPath, e); } catch (ScriptException e) { - LOGGER.error("Error while executing init script: {}", initScriptPath, e); - throw new SQLException("Error while executing init script: " + initScriptPath, e); + LOGGER.error("Error while executing init script: {}", scriptPath, e); + throw new SQLException("Error while executing init script: " + scriptPath, e); } } } diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java index 123690d95f6..c9a9e15c757 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java @@ -88,6 +88,21 @@ public void testInitScriptPathCapture() { url.getQueryParameters().remove("a"); } + @Test + public void testMultipleInitScriptPathCapture() { + String urlString = + "jdbc:tc:mysql:5.7.34://somehostname:3306/databasename?TC_INITSCRIPT=somepath/init_mysql.sql," + + "somepath/init_mysql_other.sql"; + ConnectionUrl url = ConnectionUrl.newInstance(urlString); + + assertThat(url.getInitScriptPath()) + .as("Database Type value is as expected") + .hasValue("somepath/init_mysql.sql,somepath/init_mysql_other.sql"); + assertThat(url.getContainerParameters()) + .as("INIT SCRIPT Path exists in Container Parameters") + .containsEntry("TC_INITSCRIPT", "somepath/init_mysql.sql,somepath/init_mysql_other.sql"); + } + @Test public void testInitFunctionCapture() { String urlString =