Skip to content

Commit c53bd65

Browse files
authored
Fix IF NOT EXISTS statement handling (#62)
1 parent 2c1f5f4 commit c53bd65

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

mysql_ch_replicator/converter.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'utf32': 'utf_32',
5050
'utf8mb3': 'utf_8', # Both utf8mb3 and utf8mb4 can be mapped to UTF-8
5151
'utf8mb4': 'utf_8',
52+
'utf8': 'utf_8',
5253
}
5354

5455

@@ -558,6 +559,16 @@ def parse_mysql_table_structure(self, create_statement, required_table_name=None
558559
tokens = sqlparse.parse(create_statement.replace('\n', ' ').strip())[0].tokens
559560
tokens = [t for t in tokens if not t.is_whitespace and not t.is_newline]
560561

562+
# remove "IF NOT EXISTS"
563+
if (len(tokens) > 5 and
564+
tokens[0].normalized.upper() == 'CREATE' and
565+
tokens[1].normalized.upper() == 'TABLE' and
566+
tokens[2].normalized.upper() == 'IF' and
567+
tokens[3].normalized.upper() == 'NOT' and
568+
tokens[4].normalized.upper() == 'EXISTS'):
569+
del tokens[2:5] # Remove the 'IF', 'NOT', 'EXISTS' tokens
570+
structure.if_not_exists = True
571+
561572
if tokens[0].ttype != sqlparse.tokens.DDL:
562573
raise Exception('wrong create statement', create_statement)
563574
if tokens[0].normalized.lower() != 'create':

mysql_ch_replicator/table_structure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TableStructure:
1414
table_name: str = ''
1515
charset: str = ''
1616
charset_python: str = ''
17+
if_not_exists: bool = False
1718

1819
def preprocess(self):
1920
field_names = [f.name for f in self.fields]

test_mysql_ch_replicator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from mysql_ch_replicator import clickhouse_api
1111
from mysql_ch_replicator.binlog_replicator import State as BinlogState
1212
from mysql_ch_replicator.db_replicator import State as DbReplicatorState, DbReplicator
13+
from mysql_ch_replicator.converter import MysqlToClickhouseConverter
1314

1415
from mysql_ch_replicator.runner import ProcessRunner
1516

@@ -993,3 +994,13 @@ def test_string_primary_key(monkeypatch):
993994
commit=True,
994995
)
995996
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 3)
997+
998+
999+
def test_parse_mysql_table_structure():
1000+
query = "CREATE TABLE IF NOT EXISTS user_preferences_portal (\n\t\t\tid char(36) NOT NULL,\n\t\t\tcategory varchar(50) DEFAULT NULL,\n\t\t\tdeleted tinyint(1) DEFAULT 0,\n\t\t\tdate_entered datetime DEFAULT NULL,\n\t\t\tdate_modified datetime DEFAULT NULL,\n\t\t\tassigned_user_id char(36) DEFAULT NULL,\n\t\t\tcontents longtext DEFAULT NULL\n\t\t ) ENGINE=InnoDB DEFAULT CHARSET=utf8"
1001+
1002+
converter = MysqlToClickhouseConverter()
1003+
1004+
structure = converter.parse_mysql_table_structure(query)
1005+
1006+
assert structure.table_name == 'user_preferences_portal'

0 commit comments

Comments
 (0)