Skip to content

Commit 7e50aec

Browse files
authored
Handle alter table add index (#193)
1 parent 6a49a42 commit 7e50aec

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

mysql_ch_replicator/converter.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,23 @@ def convert_alter_query(self, mysql_query, db_name):
688688
if op_name == 'add':
689689
if tokens[0].lower() in ('constraint', 'index', 'foreign', 'unique', 'key'):
690690
continue
691-
self.__convert_alter_table_add_column(db_name, table_name, tokens)
691+
# Handle cases where keywords are combined with parentheses: INDEX(col), CONSTRAINT(name), etc.
692+
for keyword in ('constraint', 'index', 'foreign', 'unique', 'key'):
693+
if tokens[0].lower().startswith(f'{keyword}('):
694+
break
695+
else:
696+
self.__convert_alter_table_add_column(db_name, table_name, tokens)
692697
continue
693698

694699
if op_name == 'drop':
695700
if tokens[0].lower() in ('constraint', 'index', 'foreign', 'unique', 'key'):
696701
continue
697-
self.__convert_alter_table_drop_column(db_name, table_name, tokens)
702+
# Handle cases where keywords are combined with parentheses: INDEX(col), CONSTRAINT(name), etc.
703+
for keyword in ('constraint', 'index', 'foreign', 'unique', 'key'):
704+
if tokens[0].lower().startswith(f'{keyword}('):
705+
break
706+
else:
707+
self.__convert_alter_table_drop_column(db_name, table_name, tokens)
698708
continue
699709

700710
if op_name == 'modify':

test_mysql_ch_replicator.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,16 @@ def test_add_column_first_after_and_drop_column(monkeypatch):
14391439
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="id=45")[0]['c1'] == 1111)
14401440
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="id=45")[0].get('c2') is None)
14411441

1442+
# Test add index to c1 column
1443+
mysql.execute(
1444+
f"ALTER TABLE `{TEST_TABLE_NAME}` ADD INDEX(c1)")
1445+
mysql.execute(
1446+
f"INSERT INTO `{TEST_TABLE_NAME}` (id, c1) VALUES (47, 5555)",
1447+
commit=True,
1448+
)
1449+
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME, where="id=47")) == 1)
1450+
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="id=47")[0]['c1'] == 5555)
1451+
14421452
db_replicator_runner.stop()
14431453
binlog_replicator_runner.stop()
14441454

0 commit comments

Comments
 (0)