Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions mysql_ch_replicator/mysql_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def reconnect_if_required(self, force=False):
self.last_connect_time = curr_time

def drop_database(self, db_name):
self.cursor.execute(f'DROP DATABASE IF EXISTS {db_name}')
self.cursor.execute(f'DROP DATABASE IF EXISTS `{db_name}`')

def drop_table(self, table_name):
self.cursor.execute(f'DROP TABLE IF EXISTS `{table_name}`')

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

def get_table_create_statement(self, table_name) -> str:
self.reconnect_if_required()
self.cursor.execute(f'SHOW CREATE TABLE {table_name}')
self.cursor.execute(f'SHOW CREATE TABLE `{table_name}`')
res = self.cursor.fetchall()
create_statement = res[0][1].strip()
return create_statement
Expand All @@ -97,7 +100,7 @@ def get_records(self, table_name, order_by, limit, start_value=None):
if start_value is not None:
start_value = ','.join(map(str, start_value))
where = f'WHERE ({order_by}) > ({start_value}) '
query = f'SELECT * FROM {table_name} {where}ORDER BY {order_by} LIMIT {limit}'
query = f'SELECT * FROM `{table_name}` {where}ORDER BY {order_by} LIMIT {limit}'
self.cursor.execute(query)
res = self.cursor.fetchall()
records = [x for x in res]
Expand Down
30 changes: 24 additions & 6 deletions test_mysql_ch_replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,37 @@ def test_runner():
PRIMARY KEY (id),
SPATIAL KEY `coordinate` (`coordinate`)
) ENGINE=InnoDB AUTO_INCREMENT=2478808 DEFAULT CHARSET=latin1;
''')
''', commit=True)


mysql.execute(f'''
CREATE TABLE `group` (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
age int,
rate decimal(10,4),
PRIMARY KEY (id)
);
''', commit=True)


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

mysql.execute(f"INSERT INTO `group` (name, age, rate) VALUES ('Peter', 33, 10.2);", commit=True)

run_all_runner = RunAllRunner()
run_all_runner.run()

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

ch.execute_command(f'USE {TEST_DB_NAME}')
ch.execute_command(f'USE {TEST_DB_NAME};')

assert_wait(lambda: 'group' in ch.get_tables())

mysql.drop_table('group')

assert_wait(lambda: 'group' not in ch.get_databases())

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


mysql.create_database(TEST_DB_NAME_2)
assert_wait(lambda: TEST_DB_NAME_2 in ch.get_databases())

mysql.execute(f'''
CREATE TABLE test_table_with_index (
CREATE TABLE `group` (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
age int,
Expand All @@ -409,9 +427,9 @@ def test_runner():
);
''')

assert_wait(lambda: 'test_table_with_index' in ch.get_tables())
assert_wait(lambda: 'group' in ch.get_tables())

create_query = ch.show_create_table('test_table_with_index')
create_query = ch.show_create_table('group')
assert 'INDEX name_idx name TYPE ngrambf_v1' in create_query

run_all_runner.stop()
Expand Down
2 changes: 1 addition & 1 deletion tests_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ check_db_updated_interval: 3

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

http_host: 'localhost'
Expand Down
Loading