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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testcontainers.containers;

import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
Expand All @@ -25,7 +26,9 @@ 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 +40,8 @@ public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContain

private String password = "";

private boolean supportsNewDriver;

/**
* @deprecated use {@link ClickHouseContainer(DockerImageName)} instead
*/
Expand All @@ -52,6 +57,7 @@ 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,13 +75,22 @@ 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
return new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("20.7");
}

@Override
public String getJdbcUrl() {
return JDBC_URL_PREFIX + getHost() + ":" + getMappedPort(HTTP_PORT) + "/" + databaseName;
Expand Down