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 9533a4c7d10..efbc7783a6c 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java +++ b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java @@ -17,7 +17,10 @@ import java.sql.Driver; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.Future; @@ -34,7 +37,7 @@ public abstract class JdbcDatabaseContainer initScriptPaths = new ArrayList<>(); protected Map parameters = new HashMap<>(); @@ -132,8 +135,37 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) { return self(); } + /** + * Sets a script for initialization. + * + * @param initScriptPath path to the script file + * @return self + */ public SELF withInitScript(String initScriptPath) { - this.initScriptPath = initScriptPath; + this.initScriptPaths = new ArrayList<>(); + this.initScriptPaths.add(initScriptPath); + return self(); + } + + /** + * Sets an ordered array of scripts for initialization. + * + * @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 + */ + public SELF withInitScripts(Iterable initScriptPaths) { + this.initScriptPaths = new ArrayList<>(); + initScriptPaths.forEach(this.initScriptPaths::add); return self(); } @@ -328,9 +360,7 @@ protected void optionallyMapResourceParameterAsVolume( * Load init script content and apply it to the database if initScriptPath is set */ protected void runInitScriptIfRequired() { - if (initScriptPath != null) { - ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath); - } + initScriptPaths.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path)); } public void setParameters(Map parameters) { diff --git a/modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java b/modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java index 84beb779fa4..bc2fb0289e3 100644 --- a/modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java +++ b/modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java @@ -74,6 +74,27 @@ public void testExplicitInitScript() throws SQLException { } } + @Test + public void testExplicitInitScripts() throws SQLException { + try ( + PostgreSQLContainer postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE) + .withInitScripts("somepath/init_postgresql.sql", "somepath/init_postgresql_2.sql") + ) { + postgres.start(); + + ResultSet resultSet = performQuery( + postgres, + "SELECT foo AS value FROM bar UNION SELECT bar AS value FROM foo" + ); + + String columnValue1 = resultSet.getString(1); + resultSet.next(); + String columnValue2 = resultSet.getString(1); + assertThat(columnValue1).as("Value from init script 1 should equal real value").isEqualTo("hello world"); + assertThat(columnValue2).as("Value from init script 2 should equal real value").isEqualTo("hello world 2"); + } + } + @Test public void testWithAdditionalUrlParamInJdbcUrl() { try ( diff --git a/modules/postgresql/src/test/resources/somepath/init_postgresql_2.sql b/modules/postgresql/src/test/resources/somepath/init_postgresql_2.sql new file mode 100644 index 00000000000..f4ecf9bbfad --- /dev/null +++ b/modules/postgresql/src/test/resources/somepath/init_postgresql_2.sql @@ -0,0 +1,5 @@ +CREATE TABLE foo ( + bar VARCHAR(255) +); + +INSERT INTO foo (bar) VALUES ('hello world 2');