Skip to content

Commit

Permalink
Allow TC_INITSCRIPT to support absolute/relative path via file: URI (#…
Browse files Browse the repository at this point in the history
…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 <rich.north@gmail.com>
  • Loading branch information
rnorth committed Mar 20, 2019
1 parent 2a4c91e commit f3ae35e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/modules/databases/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database

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

## Using an init script
## Using a classpath init script

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:

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

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

## Using an init script from a file

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).

`jdbc:tc:mysql:5.7.22://hostname/databasename?TC_INITSCRIPT=file:src/main/resources/init_mysql.sql`

#### Using an init function

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:
Expand Down
5 changes: 5 additions & 0 deletions modules/jdbc-test/sql/init_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE bar (
foo VARCHAR(255)
);

INSERT INTO bar (foo) VALUES ('hello world');
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static Iterable<Object[]> data() {
{"jdbc:tc:mysql://hostname/databasename?user=someuser&TC_INITSCRIPT=somepath/init_mysql.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITSCRIPT=somepath/init_mysql.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
{"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)},
{"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)},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?TC_INITSCRIPT=somepath/init_unicode_mysql.sql&useUnicode=yes&characterEncoding=utf8", EnumSet.of(Options.CharacterSet)},
{"jdbc:tc:mysql:5.5.43://hostname/databasename", EnumSet.noneOf(Options.class)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ContainerDatabaseDriver implements Driver {
private static final Map<String, Set<Connection>> containerConnections = new HashMap<>();
private static final Map<String, JdbcDatabaseContainer> jdbcUrlContainerCache = new HashMap<>();
private static final Set<String> initializedContainers = new HashSet<>();
private static final String FILE_PATH_PREFIX = "file:";

static {
load();
Expand Down Expand Up @@ -177,8 +178,14 @@ private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, Database
if (connectionUrl.getInitScriptPath().isPresent()) {
String initScriptPath = connectionUrl.getInitScriptPath().get();
try {
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);

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

0 comments on commit f3ae35e

Please sign in to comment.