Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +38,7 @@ public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<S

private Driver driver;

private String initScriptPath;
private List<String> initScriptPaths = new ArrayList<>();

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

Expand Down Expand Up @@ -133,8 +136,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<String> initScriptPaths) {
this.initScriptPaths = new ArrayList<>();
initScriptPaths.forEach(this.initScriptPaths::add);
return self();
}

Expand Down Expand Up @@ -329,9 +361,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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we lost the null point checker here

}

public void setParameters(Map<String, String> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE foo (
bar VARCHAR(255)
);

INSERT INTO foo (bar) VALUES ('hello world 2');