Skip to content

Commit

Permalink
Support multiple SQL init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmi committed Dec 28, 2023
1 parent 36c8727 commit 55565af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 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.Arrays;
import java.util.Collections;
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 = Collections.emptyList();

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

Expand Down Expand Up @@ -133,7 +136,17 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) {
}

public SELF withInitScript(String initScriptPath) {
this.initScriptPath = initScriptPath;
this.initScriptPaths = Collections.singletonList(initScriptPath);
return self();
}

/**
* Execute each init script in the given order
*
* @since 1.20
*/
public SELF withInitScript(String... initScriptPaths) {
this.initScriptPaths = Arrays.asList(initScriptPaths);
return self();
}

Expand Down Expand Up @@ -328,7 +341,7 @@ protected void optionallyMapResourceParameterAsVolume(
* Load init script content and apply it to the database if initScriptPath is set
*/
protected void runInitScriptIfRequired() {
if (initScriptPath != null) {
for (String initScriptPath : initScriptPaths) {
ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,32 +203,33 @@ private Connection wrapConnection(
*/
private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, DatabaseDelegate databaseDelegate)
throws SQLException {
if (connectionUrl.getInitScriptPath().isPresent()) {
String initScriptPath = connectionUrl.getInitScriptPath().get();
if (!connectionUrl.getInitScriptPath().isPresent()) return;

for (String scriptPath : connectionUrl.getInitScriptPath().get().split(",")) {
try {
URL resource;
if (initScriptPath.startsWith(FILE_PATH_PREFIX)) {
if (scriptPath.startsWith(FILE_PATH_PREFIX)) {
//relative workdir path
resource = new URL(initScriptPath);
resource = new URL(scriptPath);
} else {
//classpath resource
resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
resource = Thread.currentThread().getContextClassLoader().getResource(scriptPath);
}
if (resource == null) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
LOGGER.warn("Could not load classpath init script: {}", scriptPath);
throw new SQLException(
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
"Could not load classpath init script: " + scriptPath + ". Resource not found."
);
}

String sql = IOUtils.toString(resource, StandardCharsets.UTF_8);
ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql);
ScriptUtils.executeDatabaseScript(databaseDelegate, scriptPath, sql);
} catch (IOException e) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
throw new SQLException("Could not load classpath init script: " + initScriptPath, e);
LOGGER.warn("Could not load classpath init script: {}", scriptPath);
throw new SQLException("Could not load classpath init script: " + scriptPath, e);
} catch (ScriptException e) {
LOGGER.error("Error while executing init script: {}", initScriptPath, e);
throw new SQLException("Error while executing init script: " + initScriptPath, e);
LOGGER.error("Error while executing init script: {}", scriptPath, e);
throw new SQLException("Error while executing init script: " + scriptPath, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public void testInitScriptPathCapture() {
url.getQueryParameters().remove("a");
}

@Test
public void testMultipleInitScriptPathCapture() {
String urlString =
"jdbc:tc:mysql:5.7.34://somehostname:3306/databasename?TC_INITSCRIPT=somepath/init_mysql.sql," +
"somepath/init_mysql_other.sql";
ConnectionUrl url = ConnectionUrl.newInstance(urlString);

assertThat(url.getInitScriptPath())
.as("Database Type value is as expected")
.hasValue("somepath/init_mysql.sql,somepath/init_mysql_other.sql");
assertThat(url.getContainerParameters())
.as("INIT SCRIPT Path exists in Container Parameters")
.containsEntry("TC_INITSCRIPT", "somepath/init_mysql.sql,somepath/init_mysql_other.sql");
}

@Test
public void testInitFunctionCapture() {
String urlString =
Expand Down

0 comments on commit 55565af

Please sign in to comment.