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

Added logic to CREATE on Cluster when replicated mode is enabled #880

Open
wants to merge 2 commits into
base: 2.5.0
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ public void enterCreateDatabase(MySqlParser.CreateDatabaseContext createDatabase

String databaseName = tree.getText();
if(!databaseName.isEmpty()) {
// Check if the database is overridden
Map<String, String> sourceToDestinationMap = new HashMap<>();

try {
if (this.config.getString(ClickHouseSinkConnectorConfigVariables.CLICKHOUSE_DATABASE_OVERRIDE_MAP.toString()) != null)
sourceToDestinationMap = Utils.parseSourceToDestinationDatabaseMap(this.config.
getString(ClickHouseSinkConnectorConfigVariables.CLICKHOUSE_DATABASE_OVERRIDE_MAP.toString()));
} catch(Exception e) {
log.error("enterCreateDatabase: Error parsing source to destination database map:" + e.toString());
}
// databaseName might contain backticks. Remove them.
if(databaseName.contains("`")) {
databaseName = databaseName.replace("`", "");
}
if(sourceToDestinationMap.containsKey(databaseName)) {
this.query.append(String.format(Constants.CREATE_DATABASE, sourceToDestinationMap.get(databaseName)));
} else {
this.query.append(String.format(Constants.CREATE_DATABASE, databaseName));
}

boolean isReplicatedReplacingMergeTree = config.getBoolean(ClickHouseSinkConnectorConfigVariables
.AUTO_CREATE_TABLES_REPLICATED.toString());
if(isReplicatedReplacingMergeTree) {
this.query.append(" ON CLUSTER `{cluster}`");
}


String overrideDatabaseName = overrideDatabaseName(tree.getText());
this.query.append(String.format(Constants.CREATE_DATABASE, overrideDatabaseName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,24 @@
String sql = "create database test_ddl";
mySQLDDLParserService.parseSql(sql, "table1", clickHouseQuery);

Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("create database if not exists test_ddl"));

Check failure on line 677 in sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test Report

MySqlDDLParserListenerImplTest.testCreateDatabase

java.lang.AssertionError at com.altinity.clickhouse.debezium.embedded.ddl.parser.MySqlDDLParserListenerImplTest.testCreateDatabase(MySqlDDLParserListenerImplTest.java:677)
Raw output
java.lang.AssertionError
	at com.altinity.clickhouse.debezium.embedded.ddl.parser.MySqlDDLParserListenerImplTest.testCreateDatabase(MySqlDDLParserListenerImplTest.java:677)
}

@Test
public void testCreateDatabaseReplicated() {
StringBuffer clickHouseQuery = new StringBuffer();

HashMap<String, String> map = new HashMap<>();
map.put(ClickHouseSinkConnectorConfigVariables.AUTO_CREATE_TABLES_REPLICATED.toString(), "true");
ClickHouseSinkConnectorConfig config = new ClickHouseSinkConnectorConfig(map);

MySQLDDLParserService mySQLDDLParserService = new MySQLDDLParserService(config, "test");
String sql = "create database if not exists repl_test_ddl";
mySQLDDLParserService.parseSql(sql, "table1", clickHouseQuery);

Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("create database if not exists repl_test_ddl on cluster `{cluster}`"));

Check failure on line 692 in sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test Report

MySqlDDLParserListenerImplTest.testCreateDatabaseReplicated

java.lang.AssertionError at com.altinity.clickhouse.debezium.embedded.ddl.parser.MySqlDDLParserListenerImplTest.testCreateDatabaseReplicated(MySqlDDLParserListenerImplTest.java:692)
Raw output
java.lang.AssertionError
	at com.altinity.clickhouse.debezium.embedded.ddl.parser.MySqlDDLParserListenerImplTest.testCreateDatabaseReplicated(MySqlDDLParserListenerImplTest.java:692)
}

@Test
public void testDropColumn() {
StringBuffer clickHouseQuery = new StringBuffer();
Expand Down
Loading