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

Add r2dbc support for Clickhouse #8434

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Polish
  • Loading branch information
eddumelendez committed Mar 27, 2024
commit e6549dba9e9c8410c407cb974f2c3e72a4ee5cfa
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContain

private static final String NAME = "clickhouse";

private static final DockerImageName CLICKHOUSE_IMAGE_NAME = DockerImageName.parse("clickhouse/clickhouse-server");
static final String CLICKHOUSE_CLICKHOUSE_SERVER = "clickhouse/clickhouse-server";

public static final Integer HTTP_PORT = 8123;
private static final DockerImageName CLICKHOUSE_IMAGE_NAME = DockerImageName.parse(CLICKHOUSE_CLICKHOUSE_SERVER);

static final Integer HTTP_PORT = 8123;

static final Integer NATIVE_PORT = 9000;

Expand All @@ -35,11 +37,15 @@ public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContain

private static final String TEST_QUERY = "SELECT 1";

static final String DEFAULT_USER = "default";

static final String DEFAULT_PASSWORD = "";

private String databaseName = "default";

private String username = "default";
private String username = DEFAULT_USER;

private String password = "";
private String password = DEFAULT_PASSWORD;

public ClickHouseContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.testcontainers.clickhouse;

import io.r2dbc.spi.ConnectionFactoryOptions;
import org.testcontainers.containers.ClickHouseR2DBCDatabaseContainerProvider;
import org.testcontainers.r2dbc.R2DBCDatabaseContainer;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.testcontainers.containers;
package org.testcontainers.clickhouse;

import com.clickhouse.r2dbc.connection.ClickHouseConnectionFactoryProvider;
import io.r2dbc.spi.ConnectionFactoryMetadata;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.testcontainers.clickhouse.ClickHouseContainer;
import org.testcontainers.clickhouse.ClickHouseR2DBCDatabaseContainer;
import org.testcontainers.r2dbc.R2DBCDatabaseContainer;
import org.testcontainers.r2dbc.R2DBCDatabaseContainerProvider;

public class ClickHouseR2DBCDatabaseContainerProvider implements R2DBCDatabaseContainerProvider {
import javax.annotation.Nullable;

public static final String DRIVER = ClickHouseConnectionFactoryProvider.CLICKHOUSE_DRIVER;
public class ClickHouseR2DBCDatabaseContainerProvider implements R2DBCDatabaseContainerProvider {

private static final String IMAGE_NAME = "clickhouse/clickhouse-server";
static final String DRIVER = ClickHouseConnectionFactoryProvider.CLICKHOUSE_DRIVER;

@Override
public boolean supports(ConnectionFactoryOptions options) {
Expand All @@ -20,7 +19,8 @@ public boolean supports(ConnectionFactoryOptions options) {

@Override
public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options) {
String image = IMAGE_NAME + ":" + options.getRequiredValue(IMAGE_TAG_OPTION);
String image =
ClickHouseContainer.CLICKHOUSE_CLICKHOUSE_SERVER + ":" + options.getRequiredValue(IMAGE_TAG_OPTION);
ClickHouseContainer container = new ClickHouseContainer(image)
.withDatabaseName((String) options.getRequiredValue(ConnectionFactoryOptions.DATABASE));

Expand All @@ -29,4 +29,17 @@ public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options)
}
return new ClickHouseR2DBCDatabaseContainer(container);
}

@Nullable
@Override
public ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
ConnectionFactoryOptions.Builder builder = options.mutate();
if (!options.hasOption(ConnectionFactoryOptions.USER)) {
builder.option(ConnectionFactoryOptions.USER, ClickHouseContainer.DEFAULT_USER);
}
if (!options.hasOption(ConnectionFactoryOptions.PASSWORD)) {
builder.option(ConnectionFactoryOptions.PASSWORD, ClickHouseContainer.DEFAULT_PASSWORD);
}
return R2DBCDatabaseContainerProvider.super.getMetadata(builder.build());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.testcontainers.containers.ClickHouseR2DBCDatabaseContainerProvider
org.testcontainers.clickhouse.ClickHouseR2DBCDatabaseContainerProvider
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ public class ClickHouseR2DBCDatabaseContainerTest extends AbstractR2DBCDatabaseC

@Override
protected ConnectionFactoryOptions getOptions(ClickHouseContainer container) {
// spotless:off
// get_options {
ConnectionFactoryOptions options = ClickHouseR2DBCDatabaseContainer.getOptions(
container
);
// }
// spotless:on
ConnectionFactoryOptions options = ClickHouseR2DBCDatabaseContainer.getOptions(container);

return options;
}
Expand All @@ -25,10 +19,6 @@ protected String createR2DBCUrl() {

@Override
protected ClickHouseContainer createContainer() {
return new ClickHouseContainer("clickhouse/clickhouse-server:21.9.2-alpine")
.withExposedPorts(ClickHouseContainer.HTTP_PORT, ClickHouseContainer.NATIVE_PORT)
.withUsername("test")
.withPassword("test")
.withDatabaseName("db");
return new ClickHouseContainer("clickhouse/clickhouse-server:21.9.2-alpine");
}
}
Loading