Skip to content

Commit

Permalink
Support multiple init scripts in JdbcDatabaseContainer
Browse files Browse the repository at this point in the history
Inspired by PRs: #2578 and #1997
Closes #2232
  • Loading branch information
savinov committed Jan 22, 2024
1 parent 4ff8d1d commit a30f8ec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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 @@ -34,7 +37,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 @@ -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<String> initScriptPaths) {
this.initScriptPaths = new ArrayList<>();
initScriptPaths.forEach(this.initScriptPaths::add);
return self();
}

Expand Down Expand Up @@ -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<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');

0 comments on commit a30f8ec

Please sign in to comment.