Skip to content

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

Open

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions