Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rel/example_project
.concrete/DEV_MODE
.rebar
target/
usage/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
Expand Down
22 changes: 16 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<packaging>jar</packaging>

<artifactId>testcontainers-annotations</artifactId>
<version>4.2.2</version>
<version>5.0.0</version>

<name>testcontainers-annotations</name>
<description>testcontainers-annotations</description>
Expand Down Expand Up @@ -99,7 +99,6 @@
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>dev.vality.woody</groupId>
Expand Down Expand Up @@ -152,10 +151,6 @@
<artifactId>testcontainers-junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -174,6 +169,16 @@
<artifactId>opensearch-rest-client</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>9.0.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-jvm</artifactId>
<version>5.3.2</version>
</dependency>

<!-- exclusion-->
<dependency>
Expand Down Expand Up @@ -203,6 +208,11 @@
<artifactId>commons-compress</artifactId>
<version>1.26.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.22.0</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions renovate.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.testcontainers.clickhouse.ClickHouseContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.utility.DockerImageName;

import java.io.FileNotFoundException;
Expand All @@ -13,9 +12,11 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import java.util.regex.Pattern;

import static dev.vality.testcontainers.annotations.util.SpringApplicationPropertiesLoader.loadDefaultLibraryProperty;

Expand All @@ -24,44 +25,45 @@ public class ClickhouseContainerExtension extends ClickHouseContainer {

private static final String CLICKHOUSE_IMAGE_NAME = "clickhouse/clickhouse-server";
private static final String TAG_PROPERTY = "testcontainers.clickhouse.tag";
private static final Pattern SAFE_IDENTIFIER = Pattern.compile("[A-Za-z_][A-Za-z0-9_]*");

private final String[] migrations;
private final String databaseName;

public ClickhouseContainerExtension(String databaseName, String[] migrations) {
super(DockerImageName
.parse(CLICKHOUSE_IMAGE_NAME)
super(DockerImageName.parse(CLICKHOUSE_IMAGE_NAME)
.withTag(loadDefaultLibraryProperty(TAG_PROPERTY)));
withNetworkAliases("clickhouse-" + UUID.randomUUID());
withNetwork(Network.SHARED);
this.databaseName = databaseName;
this.migrations = migrations;
this.databaseName = validateIdentifier(databaseName);
this.migrations = migrations == null ? new String[0] : Arrays.copyOf(migrations, migrations.length);
}

public void appliedMigrations() {
try {
if (migrations != null) {
for (var migration : migrations) {
try (var connection = getSystemConn()) {
executeMigration(connection, migration);
}
}
log.info("Successfully applied {} migrations", migrations.length);
public void applyMigrations() {
try (var connection = getSystemConn()) {
for (var migration : migrations) {
executeMigration(connection, migration);
}
log.info("Successfully applied {} ClickHouse migrations", migrations.length);
} catch (SQLException ex) {
throw new ClickhouseStartingException(
"Error then applied " + migrations.length + " migrations, ",
"Error while applying " + migrations.length + " ClickHouse migrations",
ex);
}
}

/**
* @deprecated use {@link #applyMigrations()}.
*/
@Deprecated(forRemoval = false)
public void appliedMigrations() {
applyMigrations();
}

public void dropDatabase() {
try (var connection = getSystemConn()) {
try (var statement = connection.createStatement()) {
statement.execute(String.format("DROP DATABASE IF EXISTS %s", databaseName));
}
log.info("Successfully DROP DATABASE IF EXISTS {}", databaseName);
try (var connection = getSystemConn(); var statement = connection.createStatement()) {
statement.execute("DROP DATABASE IF EXISTS `" + databaseName + "`");
log.info("Successfully dropped ClickHouse database if it existed: {}", databaseName);
} catch (SQLException ex) {
throw new ClickhouseStartingException("Error then drop database dbName=" + databaseName + ", ", ex);
throw new ClickhouseStartingException("Error while dropping ClickHouse database " + databaseName, ex);
}
}

Expand All @@ -73,19 +75,20 @@ private Connection getSystemConn() throws SQLException {
}

private void executeMigration(Connection connection, String path) {
final List<String> statements;
try {
var sql = getFile(path);
var split = sql.split(";");
for (var exec : split) {
if (exec != null && !exec.trim().isEmpty()) {
try (var statement = connection.createStatement()) {
statement.execute(exec);
}
}
statements = SqlScriptParser.splitStatements(getFile(path));
} catch (IllegalArgumentException ex) {
throw new ClickhouseStartingException("Invalid SQL migration " + path, ex);
}
for (int index = 0; index < statements.size(); index++) {
try (var statement = connection.createStatement()) {
statement.execute(statements.get(index));
} catch (SQLException ex) {
throw new ClickhouseStartingException(
"Error while executing statement " + (index + 1) + " from migration " + path,
ex);
}
} catch (SQLException e) {
log.error("Error when execAllInFile path: {}", path);
throw new ClickhouseStartingException(String.format("Error when execAllInFile path: %s", path), e);
}
}

Expand All @@ -96,9 +99,17 @@ private String getFile(String fileName) {
"Migration file not found: " + fileName,
new FileNotFoundException(fileName)))) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("Error when getFile e: ", e);
throw new ClickhouseStartingException("Error when reading migration file: " + fileName, e);
} catch (IOException ex) {
throw new ClickhouseStartingException("Error while reading migration file " + fileName, ex);
}
}

private static String validateIdentifier(String identifier) {
if (identifier == null || !SAFE_IDENTIFIER.matcher(identifier).matches()) {
throw new IllegalArgumentException(
"Unsafe ClickHouse database identifier: " + identifier +
". Only letters, digits and underscores are supported");
}
return identifier;
}
}
Loading
Loading