Skip to content

Commit 50ba885

Browse files
authored
Fix create drop table if exists (#75)
* Fix "DROP TABLE IF EXISTS" * Fix "CREATE TABLE IF NOT EXISTS"
1 parent 6e1c851 commit 50ba885

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

mysql_ch_replicator/clickhouse_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
CREATE_TABLE_QUERY = '''
17-
CREATE TABLE {db_name}.{table_name}
17+
CREATE TABLE {if_not_exists} {db_name}.{table_name}
1818
(
1919
{fields},
2020
`_version` UInt64,
@@ -165,6 +165,7 @@ def create_table(self, structure: TableStructure, additional_indexes: list | Non
165165
primary_key = f'({primary_key})'
166166

167167
query = CREATE_TABLE_QUERY.format(**{
168+
'if_not_exists': 'IF NOT EXISTS' if structure.if_not_exists else '',
168169
'db_name': self.database,
169170
'table_name': structure.table_name,
170171
'fields': fields,

mysql_ch_replicator/converter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def convert_field_type(self, mysql_type, mysql_parameters):
262262
def convert_table_structure(self, mysql_structure: TableStructure) -> TableStructure:
263263
clickhouse_structure = TableStructure()
264264
clickhouse_structure.table_name = mysql_structure.table_name
265+
clickhouse_structure.if_not_exists = mysql_structure.if_not_exists
265266
for field in mysql_structure.fields:
266267
clickhouse_field_type = self.convert_field_type(field.field_type, field.parameters)
267268
clickhouse_structure.fields.append(TableField(

mysql_ch_replicator/db_replicator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,26 @@ def handle_drop_table_query(self, query, db_name):
499499
tokens = query.split()
500500
if tokens[0].lower() != 'drop' or tokens[1].lower() != 'table':
501501
raise Exception('wrong drop table query', query)
502+
503+
if_exists = (len(tokens) > 4 and
504+
tokens[2].lower() == 'if' and
505+
tokens[3].lower() == 'exists')
506+
if if_exists:
507+
del tokens[2:4] # Remove the 'IF', 'EXISTS' tokens
508+
502509
if len(tokens) != 3:
503510
raise Exception('wrong token count', query)
511+
504512
table_name = tokens[2]
505513
if '.' in table_name:
506514
db_name, table_name = table_name.split('.')
507515
if db_name == self.database:
508516
db_name = self.target_database
509517
table_name = strip_sql_name(table_name)
510518
db_name = strip_sql_name(db_name)
511-
self.state.tables_structure.pop(table_name)
512-
self.clickhouse_api.execute_command(f'DROP TABLE {db_name}.{table_name}')
519+
if table_name in self.state.tables_structure:
520+
self.state.tables_structure.pop(table_name)
521+
self.clickhouse_api.execute_command(f'DROP TABLE {"IF EXISTS" if if_exists else ""} {db_name}.{table_name}')
513522

514523
def log_stats_if_required(self):
515524
curr_time = time.time()

0 commit comments

Comments
 (0)