Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClickHouse uses new driver if it is available and version is compatible #6236

Merged
merged 3 commits into from Feb 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,7 +25,8 @@ public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContain

public static final Integer NATIVE_PORT = 9000;

private static final String DRIVER_CLASS_NAME = "ru.yandex.clickhouse.ClickHouseDriver";
private static final String LEGACY_DRIVER_CLASS_NAME = "ru.yandex.clickhouse.ClickHouseDriver";
private static final String DRIVER_CLASS_NAME = "com.clickhouse.jdbc.ClickHouseDriver";

private static final String JDBC_URL_PREFIX = "jdbc:" + NAME + "://";

Expand All @@ -37,6 +38,8 @@ public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContain

private String password = "";

private boolean supportsNewDriver;

/**
* @deprecated use {@link ClickHouseContainer(DockerImageName)} instead
*/
Expand All @@ -51,7 +54,9 @@ public ClickHouseContainer(String dockerImageName) {

public ClickHouseContainer(final DockerImageName dockerImageName) {
super(dockerImageName);

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME, CLICKHOUSE_IMAGE_NAME);
supportsNewDriver = isNewDriverSupported(dockerImageName);

addExposedPorts(HTTP_PORT, NATIVE_PORT);
this.waitStrategy =
Expand All @@ -69,10 +74,43 @@ public Set<Integer> getLivenessCheckPortNumbers() {
@Override
public String getDriverClassName() {
try {
Class.forName(DRIVER_CLASS_NAME);
return DRIVER_CLASS_NAME;
if (supportsNewDriver) {
Class.forName(DRIVER_CLASS_NAME);
return DRIVER_CLASS_NAME;
} else {
return LEGACY_DRIVER_CLASS_NAME;
}
} catch (ClassNotFoundException e) {
return "com.clickhouse.jdbc.ClickHouseDriver";
return LEGACY_DRIVER_CLASS_NAME;
}
}

private static boolean isNewDriverSupported(DockerImageName dockerImageName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may want to use ComparableVersion. See

new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("8.0.0");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddumelendez Oh, that's great, thanks! Will fix it as soon as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with ComparableVersion.

// New driver supports versions 20.7+. Check the version part of the tag
String[] versionParts = dockerImageName.getVersionPart().split(".");
// if no version part found (unknown version), return false for compatibility
if (versionParts.length == 0) {
return false;
}
try {
int major = Integer.parseInt(versionParts[0]);
// v21+ support the new driver
if (major >= 21) return true;
if (major == 20) {
if (versionParts.length == 1) {
// tag "20" points to the latest 20.x tag
return true;
}
int minor = Integer.parseInt(versionParts[1]);
// versions 20.7 and beyond support the latest driver
return minor >= 7;
}
// 19.x and older versions do not support
return false;
}
catch (NumberFormatException e) {
// non-numbered tags are "head-*", which point to the latest available version
return true;
}
}

Expand Down