Skip to content

Commit 6a07650

Browse files
authored
Support multiple init scripts in JdbcDatabaseContainer (#7680)
Fixes #2232
1 parent 1442b1c commit 6a07650

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import java.sql.Driver;
1818
import java.sql.SQLException;
1919
import java.sql.Statement;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
2022
import java.util.HashMap;
23+
import java.util.List;
2124
import java.util.Map;
2225
import java.util.Properties;
2326
import java.util.concurrent.Future;
@@ -35,7 +38,7 @@ public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<S
3538

3639
private Driver driver;
3740

38-
private String initScriptPath;
41+
private List<String> initScriptPaths = new ArrayList<>();
3942

4043
protected Map<String, String> parameters = new HashMap<>();
4144

@@ -133,8 +136,37 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) {
133136
return self();
134137
}
135138

139+
/**
140+
* Sets a script for initialization.
141+
*
142+
* @param initScriptPath path to the script file
143+
* @return self
144+
*/
136145
public SELF withInitScript(String initScriptPath) {
137-
this.initScriptPath = initScriptPath;
146+
this.initScriptPaths = new ArrayList<>();
147+
this.initScriptPaths.add(initScriptPath);
148+
return self();
149+
}
150+
151+
/**
152+
* Sets an ordered array of scripts for initialization.
153+
*
154+
* @param initScriptPaths paths to the script files
155+
* @return self
156+
*/
157+
public SELF withInitScripts(String... initScriptPaths) {
158+
return withInitScripts(Arrays.asList(initScriptPaths));
159+
}
160+
161+
/**
162+
* Sets an ordered collection of scripts for initialization.
163+
*
164+
* @param initScriptPaths paths to the script files
165+
* @return self
166+
*/
167+
public SELF withInitScripts(Iterable<String> initScriptPaths) {
168+
this.initScriptPaths = new ArrayList<>();
169+
initScriptPaths.forEach(this.initScriptPaths::add);
138170
return self();
139171
}
140172

@@ -329,9 +361,7 @@ protected void optionallyMapResourceParameterAsVolume(
329361
* Load init script content and apply it to the database if initScriptPath is set
330362
*/
331363
protected void runInitScriptIfRequired() {
332-
if (initScriptPath != null) {
333-
ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath);
334-
}
364+
initScriptPaths.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
335365
}
336366

337367
public void setParameters(Map<String, String> parameters) {

modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ public void testExplicitInitScript() throws SQLException {
7474
}
7575
}
7676

77+
@Test
78+
public void testExplicitInitScripts() throws SQLException {
79+
try (
80+
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
81+
.withInitScripts("somepath/init_postgresql.sql", "somepath/init_postgresql_2.sql")
82+
) {
83+
postgres.start();
84+
85+
ResultSet resultSet = performQuery(
86+
postgres,
87+
"SELECT foo AS value FROM bar UNION SELECT bar AS value FROM foo"
88+
);
89+
90+
String columnValue1 = resultSet.getString(1);
91+
resultSet.next();
92+
String columnValue2 = resultSet.getString(1);
93+
assertThat(columnValue1).as("Value from init script 1 should equal real value").isEqualTo("hello world");
94+
assertThat(columnValue2).as("Value from init script 2 should equal real value").isEqualTo("hello world 2");
95+
}
96+
}
97+
7798
@Test
7899
public void testWithAdditionalUrlParamInJdbcUrl() {
79100
try (
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE foo (
2+
bar VARCHAR(255)
3+
);
4+
5+
INSERT INTO foo (bar) VALUES ('hello world 2');

0 commit comments

Comments
 (0)