Skip to content

Commit a4b1576

Browse files
authored
Fixed table named group (#76)
1 parent 50ba885 commit a4b1576

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

mysql_ch_replicator/mysql_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def reconnect_if_required(self, force=False):
4343
self.last_connect_time = curr_time
4444

4545
def drop_database(self, db_name):
46-
self.cursor.execute(f'DROP DATABASE IF EXISTS {db_name}')
46+
self.cursor.execute(f'DROP DATABASE IF EXISTS `{db_name}`')
47+
48+
def drop_table(self, table_name):
49+
self.cursor.execute(f'DROP TABLE IF EXISTS `{table_name}`')
4750

4851
def create_database(self, db_name):
4952
self.cursor.execute(f'CREATE DATABASE {db_name}')
@@ -85,7 +88,7 @@ def get_binlog_files(self):
8588

8689
def get_table_create_statement(self, table_name) -> str:
8790
self.reconnect_if_required()
88-
self.cursor.execute(f'SHOW CREATE TABLE {table_name}')
91+
self.cursor.execute(f'SHOW CREATE TABLE `{table_name}`')
8992
res = self.cursor.fetchall()
9093
create_statement = res[0][1].strip()
9194
return create_statement
@@ -97,7 +100,7 @@ def get_records(self, table_name, order_by, limit, start_value=None):
97100
if start_value is not None:
98101
start_value = ','.join(map(str, start_value))
99102
where = f'WHERE ({order_by}) > ({start_value}) '
100-
query = f'SELECT * FROM {table_name} {where}ORDER BY {order_by} LIMIT {limit}'
103+
query = f'SELECT * FROM `{table_name}` {where}ORDER BY {order_by} LIMIT {limit}'
101104
self.cursor.execute(query)
102105
res = self.cursor.fetchall()
103106
records = [x for x in res]

test_mysql_ch_replicator.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,37 @@ def test_runner():
328328
PRIMARY KEY (id),
329329
SPATIAL KEY `coordinate` (`coordinate`)
330330
) ENGINE=InnoDB AUTO_INCREMENT=2478808 DEFAULT CHARSET=latin1;
331-
''')
331+
''', commit=True)
332+
333+
334+
mysql.execute(f'''
335+
CREATE TABLE `group` (
336+
id int NOT NULL AUTO_INCREMENT,
337+
name varchar(255) NOT NULL,
338+
age int,
339+
rate decimal(10,4),
340+
PRIMARY KEY (id)
341+
);
342+
''', commit=True)
332343

333344

334345
mysql.execute(f"INSERT INTO {TEST_TABLE_NAME} (name, age, coordinate) VALUES ('Ivan', 42, POINT(10.0, 20.0));", commit=True)
335346
mysql.execute(f"INSERT INTO {TEST_TABLE_NAME} (name, age, coordinate) VALUES ('Peter', 33, POINT(10.0, 20.0));", commit=True)
336347

348+
mysql.execute(f"INSERT INTO `group` (name, age, rate) VALUES ('Peter', 33, 10.2);", commit=True)
349+
337350
run_all_runner = RunAllRunner()
338351
run_all_runner.run()
339352

340353
assert_wait(lambda: TEST_DB_NAME in ch.get_databases())
341354

342-
ch.execute_command(f'USE {TEST_DB_NAME}')
355+
ch.execute_command(f'USE {TEST_DB_NAME};')
356+
357+
assert_wait(lambda: 'group' in ch.get_tables())
358+
359+
mysql.drop_table('group')
360+
361+
assert_wait(lambda: 'group' not in ch.get_databases())
343362

344363
assert_wait(lambda: TEST_TABLE_NAME in ch.get_tables())
345364
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 2)
@@ -395,12 +414,11 @@ def test_runner():
395414
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 5)
396415
assert_wait(lambda: ch.select(TEST_TABLE_NAME, "age=1912")[0]['name'] == 'Hällo')
397416

398-
399417
mysql.create_database(TEST_DB_NAME_2)
400418
assert_wait(lambda: TEST_DB_NAME_2 in ch.get_databases())
401419

402420
mysql.execute(f'''
403-
CREATE TABLE test_table_with_index (
421+
CREATE TABLE `group` (
404422
id int NOT NULL AUTO_INCREMENT,
405423
name varchar(255) NOT NULL,
406424
age int,
@@ -409,9 +427,9 @@ def test_runner():
409427
);
410428
''')
411429

412-
assert_wait(lambda: 'test_table_with_index' in ch.get_tables())
430+
assert_wait(lambda: 'group' in ch.get_tables())
413431

414-
create_query = ch.show_create_table('test_table_with_index')
432+
create_query = ch.show_create_table('group')
415433
assert 'INDEX name_idx name TYPE ngrambf_v1' in create_query
416434

417435
run_all_runner.stop()

tests_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ check_db_updated_interval: 3
2222

2323
indexes:
2424
- databases: '*'
25-
tables: ['test_table_with_index']
25+
tables: ['group']
2626
index: 'INDEX name_idx name TYPE ngrambf_v1(5, 65536, 4, 0) GRANULARITY 1'
2727

2828
http_host: 'localhost'

0 commit comments

Comments
 (0)