Skip to content

Commit b1ec15c

Browse files
authored
Fix migrate tables when default catalog is set (#4012)
Fixes #3735
1 parent 6161f99 commit b1ec15c

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/databricks/labs/ucx/hive_metastore/mapping.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ def _get_database_in_scope_task(self, database: str) -> str | None:
241241
return database
242242
describe = {}
243243
try:
244-
for value in self._sql_backend.fetch(f"DESCRIBE SCHEMA EXTENDED {escape_sql_identifier(database)}"):
244+
for value in self._sql_backend.fetch(
245+
f"DESCRIBE SCHEMA EXTENDED hive_metastore.{escape_sql_identifier(database)}"
246+
):
245247
describe[value["database_description_item"]] = value["database_description_value"]
246248
except NotFound:
247249
logger.warning(
@@ -290,10 +292,12 @@ def _get_table_in_scope_task(self, table_to_migrate: TableToMigrate, check_uc_ta
290292
return table_to_migrate
291293

292294
def _get_table_properties(self, table: Table):
293-
table_identifier = f"{escape_sql_identifier(table.database)}.{escape_sql_identifier(table.name)}"
294295
if table.is_table_in_mount:
295296
table_identifier = f"delta.`{table.location}`"
296-
297+
else:
298+
table_identifier = (
299+
f"hive_metastore.{escape_sql_identifier(table.database)}.{escape_sql_identifier(table.name)}"
300+
)
297301
try:
298302
return self._sql_backend.fetch(f"SHOW TBLPROPERTIES {table_identifier}")
299303
except DatabricksError as err:

tests/unit/hive_metastore/test_mapping.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,18 @@ def test_skip_tables_marked_for_skipping_or_upgraded():
330330
["test_schema2"],
331331
["test_schema3"],
332332
],
333-
"SHOW TBLPROPERTIES `test_schema1`.`test_table1`": [
333+
"SHOW TBLPROPERTIES hive_metastore.`test_schema1`.`test_table1`": [
334334
{"key": "upgraded_to", "value": "fake_dest"},
335335
],
336-
"SHOW TBLPROPERTIES `test_schema1`.`test_view1`": [
336+
"SHOW TBLPROPERTIES hive_metastore.`test_schema1`.`test_view1`": [
337337
{"key": "databricks.labs.ucx.skip", "value": "true"},
338338
],
339-
"SHOW TBLPROPERTIES `test_schema1`.`test_table2`": [
339+
"SHOW TBLPROPERTIES hive_metastore.`test_schema1`.`test_table2`": [
340340
{"key": "upgraded_to", "value": "fake_dest"},
341341
],
342-
"DESCRIBE SCHEMA EXTENDED `test_schema1`": [],
343-
"DESCRIBE SCHEMA EXTENDED `test_schema2`": [],
344-
"DESCRIBE SCHEMA EXTENDED `test_schema3`": [
342+
"DESCRIBE SCHEMA EXTENDED hive_metastore.`test_schema1`": [],
343+
"DESCRIBE SCHEMA EXTENDED hive_metastore.`test_schema2`": [],
344+
"DESCRIBE SCHEMA EXTENDED hive_metastore.`test_schema3`": [
345345
{
346346
"database_description_item": "Properties",
347347
"database_description_value": "((databricks.labs.ucx.skip,true))",
@@ -418,7 +418,7 @@ def test_skip_tables_marked_for_skipping_or_upgraded():
418418
def test_table_with_no_target_reverted():
419419
errors = {}
420420
rows = {
421-
"SHOW TBLPROPERTIES `schema1`.`table1`": [
421+
"SHOW TBLPROPERTIES hive_metastore.`schema1`.`table1`": [
422422
{"key": "upgraded_to", "value": "non.existing.table"},
423423
],
424424
}
@@ -490,7 +490,7 @@ def test_skipping_rules_existing_targets():
490490
]
491491
table_mapping.get_tables_to_migrate(tables_crawler)
492492

493-
assert ["DESCRIBE SCHEMA EXTENDED `schema1`"] == backend.queries
493+
assert ["DESCRIBE SCHEMA EXTENDED hive_metastore.`schema1`"] == backend.queries
494494

495495

496496
def test_mismatch_from_table_raises_exception():
@@ -530,7 +530,7 @@ def test_mismatch_from_table_raises_exception():
530530
assert len(e.errs) == 1
531531
raise e.errs[0]
532532

533-
assert ["DESCRIBE SCHEMA EXTENDED `schema1`"] == backend.queries
533+
assert ["DESCRIBE SCHEMA EXTENDED hive_metastore.`schema1`"] == backend.queries
534534

535535

536536
def test_table_not_in_crawled_tables():
@@ -548,7 +548,7 @@ def test_table_not_in_crawled_tables():
548548
tables_crawler.snapshot.return_value = []
549549
table_mapping.get_tables_to_migrate(tables_crawler)
550550

551-
assert ["DESCRIBE SCHEMA EXTENDED `schema1`"] == backend.queries
551+
assert ["DESCRIBE SCHEMA EXTENDED hive_metastore.`schema1`"] == backend.queries
552552

553553

554554
def test_skipping_rules_database_skipped():
@@ -560,7 +560,7 @@ def test_skipping_rules_database_skipped():
560560
)
561561
errors = {}
562562
rows = {
563-
"DESCRIBE SCHEMA EXTENDED `schema2`": [
563+
"DESCRIBE SCHEMA EXTENDED hive_metastore.`schema2`": [
564564
{
565565
"database_description_item": "Properties",
566566
"database_description_value": "((databricks.labs.ucx.skip,true))",
@@ -594,8 +594,8 @@ def test_skipping_rules_database_skipped():
594594
]
595595
table_mapping.get_tables_to_migrate(tables_crawler)
596596

597-
assert "SHOW TBLPROPERTIES `schema1`.`table1`" in backend.queries
598-
assert "SHOW TBLPROPERTIES `schema2`.`table2`" not in backend.queries
597+
assert "SHOW TBLPROPERTIES hive_metastore.`schema1`.`table1`" in backend.queries
598+
assert "SHOW TBLPROPERTIES hive_metastore.`schema2`.`table2`" not in backend.queries
599599

600600

601601
def test_skip_missing_table_in_snapshot():
@@ -754,7 +754,7 @@ def test_tables_in_mounts():
754754
tables_crawler.snapshot.return_value = []
755755
table_mapping.get_tables_to_migrate(tables_crawler)
756756

757-
assert ["DESCRIBE SCHEMA EXTENDED `schema1`"] == backend.queries
757+
assert ["DESCRIBE SCHEMA EXTENDED hive_metastore.`schema1`"] == backend.queries
758758

759759

760760
def test_mapping_table_in_mount():
@@ -913,7 +913,7 @@ def test_mapping_broken_table(caplog):
913913
def test_table_with_no_target_reverted_failed(caplog):
914914
errors = {"ALTER TABLE": "ALTER_TABLE_FAILED"}
915915
rows = {
916-
"SHOW TBLPROPERTIES `schema1`.`table1`": [
916+
"SHOW TBLPROPERTIES hive_metastore.`schema1`.`table1`": [
917917
{"key": "upgraded_to", "value": "non.existing.table"},
918918
],
919919
}

tests/unit/recon/test_migration_recon.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ def test_migrate_recon_should_produce_proper_queries(
4242
(src_3.schema, src_3.table, tgt_3.catalog, tgt_3.schema, tgt_3.table, "2021-01-01T00:00:00Z"),
4343
("schema_none", "table_none", None, "schema_a", "table_a", "2021-01-01T00:00:00Z"),
4444
],
45-
f"SHOW TBLPROPERTIES {src.schema}.{src.table} \\('upgraded_to'\\)": UPGRADED_TO[("value", "fake_dest"),],
46-
f"SHOW TBLPROPERTIES {src_2.schema}.{src_2.table} \\('upgraded_to'\\)": UPGRADED_TO[("value", "fake_dest"),],
45+
f"SHOW TBLPROPERTIES hive_metastore.{src.schema}.{src.table} \\('upgraded_to'\\)": UPGRADED_TO[
46+
("value", "fake_dest"),
47+
],
48+
f"SHOW TBLPROPERTIES hive_metastore.{src_2.schema}.{src_2.table} \\('upgraded_to'\\)": UPGRADED_TO[
49+
("value", "fake_dest"),
50+
],
4751
"DESCRIBE TABLE": metadata_row_factory[
4852
("col1", "int"),
4953
("col2", "string"),

0 commit comments

Comments
 (0)