Skip to content

Commit

Permalink
rewrite UUID check before embading it to DDL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Moukavoztchik committed Jul 20, 2023
1 parent f7d6f6d commit e2701e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ch_backup/clickhouse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit e2701e9

Please sign in to comment.