Skip to content

Commit f3ae35e

Browse files
authored
Allow TC_INITSCRIPT to support absolute/relative path via file: URI (#1329)
* TC_INITSCRIPT support absolute/relative path * TC_INITSCRIPT remove absolute path,relative project path use file: prefix * into a constant and use FILE_PATH_PREFIX * Simplify implementation by using URL directly * Update docs * Update modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java Co-Authored-By: rnorth <[email protected]>
1 parent 2a4c91e commit f3ae35e

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

docs/modules/databases/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database
6464

6565
`jdbc:tc:postgis:9.6://hostname/databasename`
6666

67-
## Using an init script
67+
## Using a classpath init script
6868

6969
Testcontainers can run an initscript after the database container is started, but before your code is given a connection to it. The script must be on the classpath, and is referenced as follows:
7070

7171
`jdbc:tc:mysql:5.7.22://hostname/databasename?TC_INITSCRIPT=somepath/init_mysql.sql`
7272

7373
This is useful if you have a fixed script for setting up database schema, etc.
7474

75+
## Using an init script from a file
76+
77+
If the init script path is prefixed `file:`, it will be loaded from a file (relative to the working directory, which will usually be the project root).
78+
79+
`jdbc:tc:mysql:5.7.22://hostname/databasename?TC_INITSCRIPT=file:src/main/resources/init_mysql.sql`
80+
7581
#### Using an init function
7682

7783
Instead of running a fixed script for DB setup, it may be useful to call a Java function that you define. This is intended to allow you to trigger database schema migration tools. To do this, add TC_INITFUNCTION to the URL as follows, passing a full path to the class name and method:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE bar (
2+
foo VARCHAR(255)
3+
);
4+
5+
INSERT INTO bar (foo) VALUES ('hello world');

modules/jdbc-test/src/test/java/org/testcontainers/jdbc/JDBCDriverTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static Iterable<Object[]> data() {
4646
{"jdbc:tc:mysql://hostname/databasename?user=someuser&TC_INITSCRIPT=somepath/init_mysql.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
4747
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
4848
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITSCRIPT=somepath/init_mysql.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
49+
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITSCRIPT=file:sql/init_mysql.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
4950
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
5051
{"jdbc:tc:mysql:5.5.43://hostname/databasename?TC_INITSCRIPT=somepath/init_unicode_mysql.sql&useUnicode=yes&characterEncoding=utf8", EnumSet.of(Options.CharacterSet)},
5152
{"jdbc:tc:mysql:5.5.43://hostname/databasename", EnumSet.noneOf(Options.class)},

modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ContainerDatabaseDriver implements Driver {
4141
private static final Map<String, Set<Connection>> containerConnections = new HashMap<>();
4242
private static final Map<String, JdbcDatabaseContainer> jdbcUrlContainerCache = new HashMap<>();
4343
private static final Set<String> initializedContainers = new HashSet<>();
44+
private static final String FILE_PATH_PREFIX = "file:";
4445

4546
static {
4647
load();
@@ -177,8 +178,14 @@ private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, Database
177178
if (connectionUrl.getInitScriptPath().isPresent()) {
178179
String initScriptPath = connectionUrl.getInitScriptPath().get();
179180
try {
180-
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
181-
181+
URL resource;
182+
if (initScriptPath.startsWith(FILE_PATH_PREFIX)) {
183+
//relative workdir path
184+
resource = new URL(initScriptPath);
185+
} else {
186+
//classpath resource
187+
resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
188+
}
182189
if (resource == null) {
183190
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
184191
throw new SQLException("Could not load classpath init script: " + initScriptPath + ". Resource not found.");

0 commit comments

Comments
 (0)