Skip to content

Commit

Permalink
Columns with spaces (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakwc authored Jan 24, 2025
1 parent 174222a commit ca057c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 15 additions & 6 deletions mysql_ch_replicator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,23 @@ def parse_mysql_table_structure(self, create_statement, required_table_name=None

continue

#print(" === processing line", line)
line = line.strip()
# print(" === processing line", line)

if line.startswith('`'):
end_pos = line.find('`', 1)
field_name = line[1:end_pos]
line = line[end_pos+1:].strip()
definition = line.split(' ')
else:
definition = line.split(' ')
field_name = strip_sql_name(definition[0])
definition = definition[1:]

definition = line.split(' ')
field_name = strip_sql_name(definition[0])
field_type = definition[1]
field_type = definition[0]
field_parameters = ''
if len(definition) > 2:
field_parameters = ' '.join(definition[2:])
if len(definition) > 1:
field_parameters = ' '.join(definition[1:])

additional_data = None
if 'set(' in field_type.lower():
Expand Down
14 changes: 7 additions & 7 deletions test_mysql_ch_replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ def test_e2e_regular(config_file):
CREATE TABLE `{TEST_TABLE_NAME}` (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) COMMENT 'Dân tộc, ví dụ: Kinh',
age int COMMENT 'CMND Cũ',
`age x` int COMMENT 'CMND Cũ',
field1 text,
field2 blob,
PRIMARY KEY (id)
);
''')

mysql.execute(
f"INSERT INTO `{TEST_TABLE_NAME}` (name, age, field1, field2) VALUES ('Ivan', 42, 'test1', 'test2');",
f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`, field1, field2) VALUES ('Ivan', 42, 'test1', 'test2');",
commit=True,
)
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, age) VALUES ('Peter', 33);", commit=True)
mysql.execute(f"INSERT INTO `{TEST_TABLE_NAME}` (name, `age x`) VALUES ('Peter', 33);", commit=True)

binlog_replicator_runner = BinlogReplicatorRunner(cfg_file=config_file)
binlog_replicator_runner.run()
Expand All @@ -125,13 +125,13 @@ def test_e2e_regular(config_file):
assert_wait(lambda: TEST_TABLE_NAME in ch.get_tables())
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 2)

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


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

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

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

Expand Down

0 comments on commit ca057c1

Please sign in to comment.