diff --git a/ch_backup/clickhouse/schema.py b/ch_backup/clickhouse/schema.py index 4f43e895..ee71d4c4 100644 --- a/ch_backup/clickhouse/schema.py +++ b/ch_backup/clickhouse/schema.py @@ -128,7 +128,10 @@ def rewrite_database_schema(db: Database, def _add_uuid(table: Table, inner_uuid: str = None) -> None: - if table.create_statement.find(f"UUID '{table.uuid}'") != -1: + mach_uuid = re.search(r"UUID '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}'", table.create_statement) + if mach_uuid is not None: + logging.info(f'{mach_uuid.group(0)} already present in table schema.' + f' Metadata UUID is {table.uuid}. return without adding UUID') return if is_view(table.engine): diff --git a/tests/unit/test_schema.py b/tests/unit/test_schema.py index 5201e689..a7b7f29f 100644 --- a/tests/unit/test_schema.py +++ b/tests/unit/test_schema.py @@ -243,6 +243,62 @@ def test_is_view(engine, result): 'result_table_engine': 'MergeTree', }, }, + { + 'id': 'add UUID', + 'args': { + 'table_schema': "CREATE TABLE `test_db`.`test_table` (partition_id Int32, n Int32)" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'table_engine': 'MergeTree', + 'force_non_replicated_engine': True, + 'add_uuid': True, + 'result_table_schema': f"CREATE TABLE `test_db`.`test_table` UUID '{UUID}' (partition_id Int32, n Int32)" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'result_table_engine': 'MergeTree', + }, + }, + { + 'id': 'add_uuid=True do not add UUID if schema already contains it', + 'args': { + 'table_schema': "CREATE TABLE _ (partition_id Int32, n Int32)" + f" UUID '{INNER_UUID}'" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'table_engine': 'MergeTree', + 'force_non_replicated_engine': True, + 'add_uuid': True, + 'result_table_schema': "CREATE TABLE `test_db`.`test_table` (partition_id Int32, n Int32)" + f" UUID '{INNER_UUID}'" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'result_table_engine': 'MergeTree', + }, + }, + { + 'id': 'attach statement add UUID', + 'args': { + 'table_schema': "ATTACH TABLE `test_db`.`test_table` (partition_id Int32, n Int32)" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'table_engine': 'MergeTree', + 'force_non_replicated_engine': True, + 'add_uuid': True, + 'result_table_schema': f"ATTACH TABLE `test_db`.`test_table` UUID '{UUID}' (partition_id Int32, n Int32)" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'result_table_engine': 'MergeTree', + }, + }, + { + 'id': 'attach table add_uuid=True do not add UUID if schema already contains it', + 'args': { + 'table_schema': "ATTACH TABLE _ (partition_id Int32, n Int32)" + f" UUID '{INNER_UUID}'" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'table_engine': 'MergeTree', + 'force_non_replicated_engine': True, + 'add_uuid': True, + 'result_table_schema': "ATTACH TABLE `test_db`.`test_table` (partition_id Int32, n Int32)" + f" UUID '{INNER_UUID}'" + " ENGINE = MergeTree PARTITION BY partition_id ORDER BY (partition_id, n)", + 'result_table_engine': 'MergeTree', + }, + }, ) def test_rewrite_table_schema(table_schema, table_engine, force_non_replicated_engine, add_uuid, result_table_schema, result_table_engine):