Skip to content

Commit

Permalink
Support multiple init scripts in JdbcDatabaseContainer (#7680)
Browse files Browse the repository at this point in the history
Fixes #2232
  • Loading branch information
savinov authored Jul 17, 2024
1 parent 1442b1c commit 6a07650
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 @@ -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));
}

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 6a07650

Please sign in to comment.