Skip to content

Commit

Permalink
Merge pull request #344 from IlyaTsoi/fix_create_database
Browse files Browse the repository at this point in the history
Fix for `UNKNOWN DATABASE` exception
  • Loading branch information
subkanthi authored Nov 1, 2023
2 parents 32df12d + 7876bdd commit 6b61c6e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public class ClickHouseCreateDatabase extends ClickHouseTableOperationsBase {
public void createNewDatabase(ClickHouseConnection conn, String dbName) throws SQLException {
this.runQuery(String.format("CREATE DATABASE %s", dbName), conn);
String query = String.format("USE system; CREATE DATABASE IF NOT EXISTS %s; USE %s", dbName, dbName);
this.runQuery(query, conn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.altinity.clickhouse.sink.connector.db.operations;

import com.altinity.clickhouse.sink.connector.ClickHouseSinkConnectorConfig;
import com.clickhouse.jdbc.ClickHouseConnection;
import com.altinity.clickhouse.sink.connector.db.DbWriter;
import com.altinity.clickhouse.sink.connector.db.operations.ClickHouseCreateDatabase;

import org.junit.Assert;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.junit.jupiter.Container;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.clickhouse.ClickHouseContainer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.ArrayList;
import java.util.HashMap;

@Testcontainers

public class ClickHouseCreateDatabaseTest {

static DbWriter dbWriter;
static DbWriter maintenanceDbWriter;
static String dbName;

@Container
private static ClickHouseContainer clickHouseContainer = new ClickHouseContainer("clickhouse/clickhouse-server:latest");
@BeforeAll
static void initialize() {

String hostName = clickHouseContainer.getHost();
Integer port = clickHouseContainer.getMappedPort(8123);
String userName = "default";
String password = "";
String systemDb = "system";
dbName = "test_create_db";

ClickHouseSinkConnectorConfig config= new ClickHouseSinkConnectorConfig(new HashMap<>());
dbWriter = new DbWriter(hostName, port, dbName, null, userName, password, config, null);
maintenanceDbWriter = new DbWriter(hostName, port, systemDb, null, userName, password, config, null);
}

@BeforeEach
void dropTestDatabase() throws SQLException {
Statement drop = maintenanceDbWriter.getConnection().createStatement();
drop.executeQuery(String.format("DROP DATABASE IF EXISTS %s", dbName));
}

@Test
public void testCreateNewDatabase() throws SQLException {
ClickHouseCreateDatabase act = new ClickHouseCreateDatabase();
ClickHouseConnection conn = dbWriter.getConnection();
try {
act.createNewDatabase(conn, dbName);
} catch(SQLException se) {
//System.out.println(se.getMessage());
Assert.assertTrue(false);
}
Statement stmt = dbWriter.getConnection().createStatement();
String query = String.format("SELECT name FROM system.databases WHERE name = '%s'", dbName);
ResultSet rs = stmt.executeQuery(query);
Assert.assertTrue(rs.next());
}
}

0 comments on commit 6b61c6e

Please sign in to comment.