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 fixes for cluster operation #17

Merged
Merged
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
2 changes: 1 addition & 1 deletion flyway-database-clickhouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.7.0</version>
<version>10.7.1</version>
</parent>

<artifactId>flyway-database-clickhouse</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public class ClickHouseConnection extends Connection<ClickHouseDatabase> {

@Override
protected String getCurrentSchemaNameOrSearchPath() throws SQLException {
return Optional.ofNullable(getJdbcTemplate().getConnection().getSchema()).map(database::unQuote).orElse(null);
return Optional.ofNullable(getJdbcTemplate().getConnection().getCatalog()).map(database::unQuote).orElse(null);
}

@Override
public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException {
getJdbcTemplate().getConnection().setSchema(schema);
// databaseTerm is catalog since driver version 0.5.0
// https://github.com/ClickHouse/clickhouse-java/issues/1273 & https://github.com/dbeaver/dbeaver/issues/19383
// For compatibility with old libraries, ((ClickHouseConnection) getJdbcConnection()).useCatalog() should be checked
getJdbcTemplate().getConnection().setCatalog(schema);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.exception.FlywaySqlException;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;
import org.flywaydb.core.internal.util.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class ClickHouseDatabase extends Database<ClickHouseConnection> {

private ClickHouseConnection systemConnection;

@Override
public boolean useSingleConnection() {
return true;
Expand All @@ -42,6 +47,25 @@ public String getZookeeperPath() {
return configuration.getPluginRegister().getPlugin(ClickHouseConfigurationExtension.class).getZookeeperPath();
}

public ClickHouseConnection getSystemConnection() {
// Queries on system.XX fail with "Code: 81. DB::Exception: Database the_database doesn't exist. (UNKNOWN_DATABASE) (version 23.7.1.2470 (official build))"
// in case the current catalog (database) is not yet created.
// For this reason, we switch to an existing DB before execution. The database might not have been created yet, so we cannot reliably switch back the Schema.
// * mainConnection cannot be used, as this would change the location of the schema history table.
// * jdbcTemplate cannot be used, as this would change the location of the new tables.
// We had to introduce a separate connection, reserved to system database access.
if (systemConnection == null) {
Connection connection = jdbcConnectionFactory.openConnection();
try {
systemConnection = doGetConnection(connection);
systemConnection.doChangeCurrentSchemaOrSearchPathTo("system");
} catch (SQLException e) {
throw new FlywaySqlException("Unable to switch connection to read-only", e);
}
}
return systemConnection;
}

@Override
protected ClickHouseConnection doGetConnection(Connection connection) {
return new ClickHouseConnection(this, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.util.StringUtils;

import java.sql.SQLException;
import java.util.Optional;

public class ClickHouseSchema extends Schema<ClickHouseDatabase, ClickHouseTable> {
/**
Expand All @@ -32,27 +34,34 @@ public ClickHouseSchema(JdbcTemplate jdbcTemplate, ClickHouseDatabase database,

@Override
protected boolean doExists() throws SQLException {
int i = jdbcTemplate.queryForInt("SELECT COUNT() FROM system.databases WHERE name = ?", name);
ClickHouseConnection systemConnection = database.getSystemConnection();
int i = systemConnection.getJdbcTemplate().queryForInt("SELECT COUNT() FROM system.databases WHERE name = ?", name);
return i > 0;
}

@Override
protected boolean doEmpty() throws SQLException {
int i = jdbcTemplate.queryForInt("SELECT COUNT() FROM system.tables WHERE database = ?", name);
ClickHouseConnection systemConnection = database.getSystemConnection();
int i = systemConnection.getJdbcTemplate().queryForInt("SELECT COUNT() FROM system.tables WHERE database = ?", name);
return i == 0;
}

@Override
protected void doCreate() throws SQLException {
jdbcTemplate.executeStatement("CREATE DATABASE " + database.quote(name));
ClickHouseConnection systemConnection = database.getSystemConnection();
String clusterName = database.getClusterName();
boolean isClustered = StringUtils.hasText(clusterName);
systemConnection.getJdbcTemplate().executeStatement("CREATE DATABASE " + database.quote(name) + (isClustered ? (" ON CLUSTER " + clusterName) : ""));
}

@Override
protected void doDrop() throws SQLException {
if (jdbcTemplate.getConnection().getSchema().equals(name)) {
jdbcTemplate.getConnection().setSchema("default");
if (jdbcTemplate.getConnection().getCatalog().equals(name)) {
jdbcTemplate.getConnection().setCatalog(Optional.ofNullable(database.getConfiguration().getDefaultSchema()).orElse("default"));
}
jdbcTemplate.executeStatement("DROP DATABASE " + database.quote(name));
String clusterName = database.getClusterName();
boolean isClustered = StringUtils.hasText(clusterName);
jdbcTemplate.executeStatement("DROP DATABASE " + database.quote(name) + (isClustered ? (" ON CLUSTER " + clusterName) : ""));
}

@Override
Expand All @@ -64,7 +73,8 @@ protected void doClean() throws SQLException {

@Override
protected ClickHouseTable[] doAllTables() throws SQLException {
return jdbcTemplate.queryForStringList("SELECT name FROM system.tables WHERE database = ?", name)
ClickHouseConnection systemConnection = database.getSystemConnection();
return systemConnection.getJdbcTemplate().queryForStringList("SELECT name FROM system.tables WHERE database = ?", name)
.stream()
.map(this::getTable)
.toArray(ClickHouseTable[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ protected void doDrop() throws SQLException {

@Override
protected boolean doExists() throws SQLException {
int count = jdbcTemplate.queryForInt("SELECT COUNT() FROM system.tables WHERE database = ? AND name = ?", schema.getName(), name);
ClickHouseConnection systemConnection = database.getSystemConnection();
int count = systemConnection.getJdbcTemplate().queryForInt("SELECT COUNT() FROM system.tables WHERE database = ? AND name = ?", schema.getName(), name);
return count > 0;
}

Expand Down
2 changes: 1 addition & 1 deletion flyway-database-ignite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.7.0</version>
<version>10.7.1</version>
</parent>

<artifactId>flyway-database-ignite</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions flyway-database-oceanbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.7.0</version>
<version>10.7.1</version>
</parent>

<artifactId>flyway-database-oceanbase</artifactId>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-mysql</artifactId>
<version>10.6.0</version>
<version>10.7.1</version>
<exclusions>
<exclusion>
<groupId>org.flywaydb</groupId>
Expand Down
4 changes: 2 additions & 2 deletions flyway-database-tidb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.7.0</version>
<version>10.7.1</version>
</parent>

<artifactId>flyway-database-tidb</artifactId>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-mysql</artifactId>
<version>10.6.0</version>
<version>10.7.1</version>
<exclusions>
<exclusion>
<groupId>org.flywaydb</groupId>
Expand Down
4 changes: 2 additions & 2 deletions flyway-database-yugabytedb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.7.0</version>
<version>10.7.1</version>
</parent>

<artifactId>flyway-database-yugabytedb</artifactId>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>10.6.0</version>
<version>10.7.1</version>
<exclusions>
<exclusion>
<groupId>org.flywaydb</groupId>
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-parent</artifactId>
<version>10.6.0</version>
<version>10.7.1</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>flyway-community-db-support</artifactId>
<packaging>pom</packaging>
<version>10.7.0</version>
<version>10.7.1</version>
<name>${project.artifactId}</name>

<modules>
Expand All @@ -44,7 +44,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
<version>10.6.0</version>
<version>10.7.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Loading