2020-07-17 08:24:21 +00:00
import time
2020-07-18 04:13:41 +00:00
def check_query ( clickhouse_node , query , result_set , retry_count = 3 , interval_seconds = 3 ) :
2020-07-17 08:24:21 +00:00
lastest_result = ' '
for index in range ( retry_count ) :
lastest_result = clickhouse_node . query ( query )
if result_set == lastest_result :
return
2020-07-17 10:27:34 +00:00
print lastest_result
2020-07-17 08:24:21 +00:00
time . sleep ( interval_seconds )
assert lastest_result == result_set
2020-07-20 17:44:38 +00:00
def dml_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
# existed before the mapping was created
mysql_node . query ( " CREATE TABLE test_database.test_table_1 ( "
" `key` INT NOT NULL PRIMARY KEY, "
" unsigned_tiny_int TINYINT UNSIGNED, tiny_int TINYINT, "
" unsigned_small_int SMALLINT UNSIGNED, small_int SMALLINT, "
" unsigned_medium_int MEDIUMINT UNSIGNED, medium_int MEDIUMINT, "
" unsigned_int INT UNSIGNED, _int INT, "
" unsigned_integer INTEGER UNSIGNED, _integer INTEGER, "
" unsigned_bigint BIGINT UNSIGNED, _bigint BIGINT, "
" /* Need ClickHouse support read mysql decimal unsigned_decimal DECIMAL(19, 10) UNSIGNED, _decimal DECIMAL(19, 10), */ "
" unsigned_float FLOAT UNSIGNED, _float FLOAT, "
" unsigned_double DOUBLE UNSIGNED, _double DOUBLE, "
" _varchar VARCHAR(10), _char CHAR(10), "
2020-07-21 01:35:25 +00:00
" /* Need ClickHouse support Enum( ' a ' , ' b ' , ' v ' ) _enum ENUM( ' a ' , ' b ' , ' c ' ), */ "
" _date Date, _datetime DateTime, _timestamp TIMESTAMP, _bool BOOLEAN) ENGINE = InnoDB; " )
2020-07-20 17:44:38 +00:00
# it already has some data
mysql_node . query (
" INSERT INTO test_database.test_table_1 VALUES(1, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 3.2, -3.2, 3.4, -3.4, ' varchar ' , ' char ' , "
2020-07-21 01:35:25 +00:00
" ' 2020-01-01 ' , ' 2020-01-01 00:00:00 ' , ' 2020-01-01 00:00:00 ' , true); " )
2020-07-20 17:44:38 +00:00
clickhouse_node . query ( " CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_1 ORDER BY key FORMAT TSV " ,
2020-07-21 08:16:15 +00:00
" 1 \t 1 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t varchar \t char \t 2020-01-01 \t "
2020-07-21 01:35:25 +00:00
" 2020-01-01 00:00:00 \t 2020-01-01 00:00:00 \t 1 \n " )
2020-07-20 17:44:38 +00:00
mysql_node . query (
" INSERT INTO test_database.test_table_1 VALUES(2, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 3.2, -3.2, 3.4, -3.4, ' varchar ' , ' char ' , "
2020-07-21 01:35:25 +00:00
" ' 2020-01-01 ' , ' 2020-01-01 00:00:00 ' , ' 2020-01-01 00:00:00 ' , false); " )
2020-07-20 17:44:38 +00:00
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_1 ORDER BY key FORMAT TSV " ,
2020-07-21 01:35:25 +00:00
" 1 \t 1 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t varchar \t char \t 2020-01-01 \t "
2020-07-21 08:16:15 +00:00
" 2020-01-01 00:00:00 \t 2020-01-01 00:00:00 \t 1 \n 2 \t 1 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t "
2020-07-21 01:35:25 +00:00
" varchar \t char \t 2020-01-01 \t 2020-01-01 00:00:00 \t 2020-01-01 00:00:00 \t 0 \n " )
2020-07-20 17:44:38 +00:00
mysql_node . query ( " UPDATE test_database.test_table_1 SET unsigned_tiny_int = 2 WHERE `key` = 1 " )
2020-07-21 08:16:15 +00:00
check_query ( clickhouse_node , " SELECT key, unsigned_tiny_int, tiny_int, unsigned_small_int, "
" small_int, unsigned_medium_int, medium_int, unsigned_int, _int, unsigned_integer, _integer, "
" unsigned_bigint, _bigint, unsigned_float, _float, unsigned_double, _double, _varchar, _char, "
" _date, _datetime, /* exclude it, because ON UPDATE CURRENT_TIMESTAMP _timestamp, */ "
" _bool FROM test_database.test_table_1 ORDER BY key FORMAT TSV " ,
2020-07-21 01:35:25 +00:00
" 1 \t 2 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t varchar \t char \t 2020-01-01 \t "
2020-07-21 08:16:15 +00:00
" 2020-01-01 00:00:00 \t 1 \n 2 \t 1 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t "
" varchar \t char \t 2020-01-01 \t 2020-01-01 00:00:00 \t 0 \n " )
2020-07-20 17:44:38 +00:00
# update primary key
2020-07-21 08:16:15 +00:00
mysql_node . query ( " UPDATE test_database.test_table_1 SET `key` = 3 WHERE `unsigned_tiny_int` = 2 " )
2020-07-20 17:44:38 +00:00
2020-07-21 08:16:15 +00:00
check_query ( clickhouse_node , " SELECT key, unsigned_tiny_int, tiny_int, unsigned_small_int, "
" small_int, unsigned_medium_int, medium_int, unsigned_int, _int, unsigned_integer, _integer, "
" unsigned_bigint, _bigint, unsigned_float, _float, unsigned_double, _double, _varchar, _char, "
" _date, _datetime, /* exclude it, because ON UPDATE CURRENT_TIMESTAMP _timestamp, */ "
" _bool FROM test_database.test_table_1 ORDER BY key FORMAT TSV " ,
2020-07-21 01:35:25 +00:00
" 2 \t 1 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t "
2020-07-21 08:16:15 +00:00
" varchar \t char \t 2020-01-01 \t 2020-01-01 00:00:00 \t 0 \n 3 \t 2 \t -1 \t 2 \t -2 \t 3 \t -3 \t "
" 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t varchar \t char \t 2020-01-01 \t 2020-01-01 00:00:00 \t 1 \n " )
2020-07-20 17:44:38 +00:00
mysql_node . query ( ' DELETE FROM test_database.test_table_1 WHERE `key` = 2 ' )
2020-07-21 08:16:15 +00:00
check_query ( clickhouse_node , " SELECT key, unsigned_tiny_int, tiny_int, unsigned_small_int, "
" small_int, unsigned_medium_int, medium_int, unsigned_int, _int, unsigned_integer, _integer, "
" unsigned_bigint, _bigint, unsigned_float, _float, unsigned_double, _double, _varchar, _char, "
" _date, _datetime, /* exclude it, because ON UPDATE CURRENT_TIMESTAMP _timestamp, */ "
" _bool FROM test_database.test_table_1 ORDER BY key FORMAT TSV " ,
2020-07-21 01:35:25 +00:00
" 3 \t 2 \t -1 \t 2 \t -2 \t 3 \t -3 \t 4 \t -4 \t 5 \t -5 \t 6 \t -6 \t 3.2 \t -3.2 \t 3.4 \t -3.4 \t varchar \t char \t 2020-01-01 \t "
2020-07-21 08:16:15 +00:00
" 2020-01-01 00:00:00 \t 1 \n " )
2020-07-20 17:44:38 +00:00
mysql_node . query ( ' DELETE FROM test_database.test_table_1 WHERE `unsigned_tiny_int` = 2 ' )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_1 ORDER BY key FORMAT TSV " , " " )
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-20 17:44:38 +00:00
2020-07-18 04:13:41 +00:00
def drop_table_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
mysql_node . query ( " DROP TABLE test_database.test_table_1; " )
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
mysql_node . query ( " TRUNCATE TABLE test_database.test_table_2; " )
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_2 \n " )
2020-07-17 10:05:46 +00:00
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " " )
2020-07-17 08:24:21 +00:00
2020-07-17 10:27:34 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1), (2), (3), (4), (5), (6) " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " DROP TABLE test_database.test_table_1; " )
mysql_node . query ( " TRUNCATE TABLE test_database.test_table_2; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_2 \n " )
2020-07-17 10:05:46 +00:00
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def create_table_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
# existed before the mapping was created
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
# it already has some data
2020-07-18 04:13:41 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_1 VALUES(1), (2), (3), (5), (6), (7); " )
2020-07-17 08:24:21 +00:00
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query ( " CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
# Check for pre-existing status
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_1 ORDER BY id FORMAT TSV " , " 1 \n 2 \n 3 \n 5 \n 6 \n 7 \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
2020-07-18 12:06:12 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1), (2), (3), (4), (5), (6); " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def rename_table_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
mysql_node . query ( " RENAME TABLE test_database.test_table_1 TO test_database.test_table_2 " )
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_2 \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " RENAME TABLE test_database.test_table_2 TO test_database.test_table_1 " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def alter_add_column_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 ADD COLUMN add_column_1 INT NOT NULL " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 ADD COLUMN add_column_2 INT NOT NULL FIRST " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 ADD COLUMN add_column_3 INT NOT NULL AFTER add_column_1 " )
2020-08-13 18:20:47 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_1 ADD COLUMN add_column_4 INT NOT NULL DEFAULT " + ( " 0 " if service_name == " mysql1 " else " (id) " ) )
2020-07-17 08:24:21 +00:00
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
2020-07-17 10:05:46 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_1 FORMAT TSV " ,
2020-08-11 13:35:50 +00:00
" add_column_2 \t Int32 \t \t \t \t \t \n id \t Int32 \t \t \t \t \t \n add_column_1 \t Int32 \t \t \t \t \t \n add_column_3 \t Int32 \t \t \t \t \t \n add_column_4 \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY) ENGINE = InnoDB; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 ADD COLUMN add_column_1 INT NOT NULL, ADD COLUMN add_column_2 INT NOT NULL FIRST " )
mysql_node . query (
2020-07-18 04:13:41 +00:00
" ALTER TABLE test_database.test_table_2 ADD COLUMN add_column_3 INT NOT NULL AFTER add_column_1, ADD COLUMN add_column_4 INT NOT NULL DEFAULT " + (
2020-08-13 18:20:47 +00:00
" 0 " if service_name == " mysql1 " else " (id) " ) )
2020-07-18 12:06:12 +00:00
2020-08-13 18:20:47 +00:00
default_expression = " DEFAULT \t 0 " if service_name == " mysql1 " else " DEFAULT \t id "
2020-07-17 10:05:46 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " ,
2020-08-11 13:35:50 +00:00
" add_column_2 \t Int32 \t \t \t \t \t \n id \t Int32 \t \t \t \t \t \n add_column_1 \t Int32 \t \t \t \t \t \n add_column_3 \t Int32 \t \t \t \t \t \n add_column_4 \t Int32 \t " + default_expression + " \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
2020-07-19 12:59:31 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1, 2, 3, 4, 5), (6, 7, 8, 9, 10) " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \t 2 \t 3 \t 4 \t 5 \n 6 \t 7 \t 8 \t 9 \t 10 \n " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def alter_drop_column_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY, drop_column INT) ENGINE = InnoDB; " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 DROP COLUMN drop_column " )
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_1 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY, drop_column INT NOT NULL) ENGINE = InnoDB; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n drop_column \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 DROP COLUMN drop_column " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
2020-07-19 12:59:31 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1), (2), (3), (4), (5) " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \n 2 \n 3 \n 4 \n 5 \n " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def alter_rename_column_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
# maybe should test rename primary key?
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY, rename_column INT NOT NULL) ENGINE = InnoDB; " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 RENAME COLUMN rename_column TO new_column_name " )
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_1 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n new_column_name \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY, rename_column INT NOT NULL) ENGINE = InnoDB; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n rename_column \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 RENAME COLUMN rename_column TO new_column_name " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n new_column_name \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
2020-07-19 12:59:31 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1, 2), (3, 4), (5, 6), (7, 8), (9, 10) " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \t 2 \n 3 \t 4 \n 5 \t 6 \n 7 \t 8 \n 9 \t 10 \n " )
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-07-18 04:13:41 +00:00
def alter_modify_column_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
# maybe should test rename primary key?
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY, modify_column INT NOT NULL) ENGINE = InnoDB; " )
mysql_node . query ( " ALTER TABLE test_database.test_table_1 MODIFY COLUMN modify_column INT " )
# create mapping
2020-07-18 04:13:41 +00:00
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
2020-07-17 08:24:21 +00:00
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_1 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n modify_column \t Nullable(Int32) \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_2 (id INT NOT NULL PRIMARY KEY, modify_column INT NOT NULL) ENGINE = InnoDB; " )
2020-07-17 10:27:34 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_2 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n modify_column \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-18 04:13:41 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 MODIFY COLUMN modify_column INT " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n modify_column \t Nullable(Int32) \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-18 12:25:45 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 MODIFY COLUMN modify_column INT FIRST " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " modify_column \t Nullable(Int32) \t \t \t \t \t \n id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-18 12:25:45 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_2 MODIFY COLUMN modify_column INT AFTER id " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_2 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n modify_column \t Nullable(Int32) \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-07-17 08:24:21 +00:00
2020-07-19 12:59:31 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_2 VALUES(1, 2), (3, NULL) " )
2020-07-19 13:20:16 +00:00
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_2 ORDER BY id FORMAT TSV " , " 1 \t 2 \n 3 \t \\ N \n " )
2020-07-19 12:59:31 +00:00
2020-07-17 08:24:21 +00:00
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )
2020-07-17 08:24:21 +00:00
2020-08-09 12:10:30 +00:00
2020-07-19 12:53:09 +00:00
# TODO: need ClickHouse support ALTER TABLE table_name ADD COLUMN column_name, RENAME COLUMN column_name TO new_column_name;
2020-07-17 08:24:21 +00:00
# def test_mysql_alter_change_column_for_materialize_mysql_database(started_cluster):
# pass
2020-08-09 12:10:30 +00:00
def alter_rename_table_with_materialize_mysql_database ( clickhouse_node , mysql_node , service_name ) :
mysql_node . query ( " CREATE DATABASE test_database DEFAULT CHARACTER SET ' utf8 ' " )
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY, drop_column INT) ENGINE = InnoDB; " )
2020-08-09 14:55:58 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_1 DROP COLUMN drop_column, RENAME TO test_database.test_table_2, RENAME TO test_database.test_table_3 " )
2020-08-09 12:10:30 +00:00
# create mapping
clickhouse_node . query (
" CREATE DATABASE test_database ENGINE = MaterializeMySQL( ' {} :3306 ' , ' test_database ' , ' root ' , ' clickhouse ' ) " . format ( service_name ) )
assert " test_database " in clickhouse_node . query ( " SHOW DATABASES " )
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_3 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_3 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-08-09 12:10:30 +00:00
mysql_node . query ( " CREATE TABLE test_database.test_table_1 (id INT NOT NULL PRIMARY KEY, drop_column INT NOT NULL) ENGINE = InnoDB; " )
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_1 \n test_table_3 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_1 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n drop_column \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-08-09 14:55:58 +00:00
mysql_node . query ( " ALTER TABLE test_database.test_table_1 DROP COLUMN drop_column, RENAME TO test_database.test_table_2, RENAME TO test_database.test_table_4 " )
2020-08-09 12:10:30 +00:00
check_query ( clickhouse_node , " SHOW TABLES FROM test_database FORMAT TSV " , " test_table_3 \n test_table_4 \n " )
2020-08-11 13:35:50 +00:00
check_query ( clickhouse_node , " DESC test_database.test_table_4 FORMAT TSV " , " id \t Int32 \t \t \t \t \t \n _sign \t Int8 \t MATERIALIZED \t 1 \t \t \t \n _version \t UInt64 \t MATERIALIZED \t 1 \t \t \t \n " )
2020-08-09 12:10:30 +00:00
mysql_node . query ( " INSERT INTO test_database.test_table_4 VALUES(1), (2), (3), (4), (5) " )
check_query ( clickhouse_node , " SELECT * FROM test_database.test_table_4 ORDER BY id FORMAT TSV " , " 1 \n 2 \n 3 \n 4 \n 5 \n " )
clickhouse_node . query ( " DROP DATABASE test_database " )
2020-08-17 05:13:56 +00:00
mysql_node . query ( " DROP DATABASE test_database " )