Skip to content

Commit ca057c1

Browse files
authored
Columns with spaces (#93)
1 parent 174222a commit ca057c1

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

mysql_ch_replicator/converter.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,23 @@ def parse_mysql_table_structure(self, create_statement, required_table_name=None
710710

711711
continue
712712

713-
#print(" === processing line", line)
713+
line = line.strip()
714+
# print(" === processing line", line)
715+
716+
if line.startswith('`'):
717+
end_pos = line.find('`', 1)
718+
field_name = line[1:end_pos]
719+
line = line[end_pos+1:].strip()
720+
definition = line.split(' ')
721+
else:
722+
definition = line.split(' ')
723+
field_name = strip_sql_name(definition[0])
724+
definition = definition[1:]
714725

715-
definition = line.split(' ')
716-
field_name = strip_sql_name(definition[0])
717-
field_type = definition[1]
726+
field_type = definition[0]
718727
field_parameters = ''
719-
if len(definition) > 2:
720-
field_parameters = ' '.join(definition[2:])
728+
if len(definition) > 1:
729+
field_parameters = ' '.join(definition[1:])
721730

722731
additional_data = None
723732
if 'set(' in field_type.lower():

test_mysql_ch_replicator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ def test_e2e_regular(config_file):
100100
CREATE TABLE `{TEST_TABLE_NAME}` (
101101
id int NOT NULL AUTO_INCREMENT,
102102
name varchar(255) COMMENT 'Dân tộc, ví dụ: Kinh',
103-
age int COMMENT 'CMND Cũ',
103+
`age x` int COMMENT 'CMND Cũ',
104104
field1 text,
105105
field2 blob,
106106
PRIMARY KEY (id)
107107
);
108108
''')
109109

110110
mysql.execute(
111-
f"INSERT INTO `{TEST_TABLE_NAME}` (name, age, field1, field2) VALUES ('Ivan', 42, 'test1', 'test2');",
111+
f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`, field1, field2) VALUES ('Ivan', 42, 'test1', 'test2');",
112112
commit=True,
113113
)
114-
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, age) VALUES ('Peter', 33);", commit=True)
114+
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`) VALUES ('Peter', 33);", commit=True)
115115

116116
binlog_replicator_runner = BinlogReplicatorRunner(cfg_file=config_file)
117117
binlog_replicator_runner.run()
@@ -125,13 +125,13 @@ def test_e2e_regular(config_file):
125125
assert_wait(lambda: TEST_TABLE_NAME in ch.get_tables())
126126
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 2)
127127

128-
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, age) VALUES ('Filipp', 50);", commit=True)
128+
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`) VALUES ('Filipp', 50);", commit=True)
129129
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 3)
130-
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="name='Filipp'")[0]['age'] == 50)
130+
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="name='Filipp'")[0]['age x'] == 50)
131131

132132

133133
mysql.execute(f"ALTER TABLE `{TEST_TABLE_NAME}` ADD `last_name` varchar(255); ")
134-
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, age, last_name) VALUES ('Mary', 24, 'Smith');", commit=True)
134+
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`, last_name) VALUES ('Mary', 24, 'Smith');", commit=True)
135135

136136
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 4)
137137
assert_wait(lambda: ch.select(TEST_TABLE_NAME, where="name='Mary'")[0]['last_name'] == 'Smith')
@@ -146,7 +146,7 @@ def test_e2e_regular(config_file):
146146
)
147147

148148
mysql.execute(
149-
f"INSERT INTO `{TEST_TABLE_NAME}` (name, age, last_name, country) "
149+
f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`, last_name, country) "
150150
f"VALUES ('John', 12, 'Doe', 'USA');", commit=True,
151151
)
152152

0 commit comments

Comments
 (0)