testcontainers / testcontainers/testcontainers-java

`databasename` part of `jdbc:tc` URIs іs ignored when optional host:port pair is omitted

Open
#4,121 10 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

modules/jdbc resolution/acknowledged type/bug
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

Note that this issue is related to behavior mentioned in #1846.

When one uses "host-less URI" as specified on the JDBC support page, e.g.: jdbc:tc:mysql:5.7.12:///mydatabase, DB name is also ignored and a default test name is used instead.
This code fragment from org.testcontainers.jdbc.ConnectionUrl#parseUrl method clearly shows how it is handled:

//In case it matches to the default pattern
Matcher dbInstanceMatcher = Patterns.DB_INSTANCE_MATCHING_PATTERN.matcher(dbHostString);
if (dbInstanceMatcher.matches()) {
	databaseHost = Optional.of(dbInstanceMatcher.group(1));
        databasePort = Optional.ofNullable(dbInstanceMatcher.group(3)).map(value -> Integer.valueOf(value));
        databaseName = Optional.of(dbInstanceMatcher.group(4));
}

then org.testcontainers.containers.JdbcDatabaseContainerProvider#newInstanceFromConnectionUrl defaults DB name to test:

final String databaseName = connectionUrl.getDatabaseName().orElse("test");

In my particular case an init SQL script, which starts with the following instruction, fails with Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'test'@'%' to 'mydatabase' error:

CREATE DATABASE IF NOT EXISTS `mydatabase`;

Aforementioned documentation page says:

Note that the hostname, port and database name will be ignored; you can leave these as-is or set them to any value.

which apparently is not the case and /// syntax, which includes DB name only, is recommended and multiple examples are provided.

I suggest to improve "host-less" URI handling by honoring DB name and clarify the documentation on this matter. Will make a PR for this if project owners are OK with such resolution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with org.testcontainers.jdbc.ConnectionUrl#parseUrl and org.testcontainers.containers.JdbcDatabaseContainerProvider#newInstanceFromConnectionUrl, then review the JDBC support documentation page. Confirm that a host-less URI such as jdbc:tc:mysql:5.7.12:///mydatabase preserves the database name instead of defaulting to test, and clarify the documentation accordingly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mysql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.