Skip to content

Commit 28f2ea9

Browse files
authored
Fixed an issue with source table deleted after migration (#2927)
closes #2901
1 parent c8c0428 commit 28f2ea9

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,20 @@ def get_seen_tables(self) -> dict[str, str]:
108108
return seen_tables
109109

110110
def is_migrated(self, schema: str, table: str) -> bool:
111-
results = self._backend.fetch(
112-
f"SHOW TBLPROPERTIES {escape_sql_identifier(schema + '.' + table)} ('upgraded_to')"
113-
)
114-
for result in results:
115-
if "does not have property" in result.value:
116-
continue
117-
logger.info(f"{schema}.{table} is set as migrated")
111+
try:
112+
results = self._backend.fetch(
113+
f"SHOW TBLPROPERTIES {escape_sql_identifier(schema + '.' + table)} ('upgraded_to')"
114+
)
115+
for result in results:
116+
if "does not have property" in result.value:
117+
continue
118+
logger.info(f"{schema}.{table} is set as migrated")
119+
return True
120+
logger.info(f"{schema}.{table} is set as not migrated")
121+
except NotFound:
122+
# If the source table doesn't exist, it will not be shown as migrated
123+
logger.warning(f"failed-to-migrate: {schema}.{table} set as a source does no longer exist")
118124
return True
119-
logger.info(f"{schema}.{table} is set as not migrated")
120125
return False
121126

122127
def _crawl(self) -> Iterable[TableMigrationStatus]:

tests/integration/hive_metastore/test_migrate.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,32 @@ def test_migrate_external_tables_with_spn_azure(
859859
match = True
860860
break
861861
assert match
862+
863+
864+
def test_migration_index_deleted_source(make_table, runtime_ctx, sql_backend, make_catalog, make_schema, caplog):
865+
src_table = make_table()
866+
dst_catalog = make_catalog()
867+
dst_schema = make_schema(catalog_name=dst_catalog.name)
868+
# Create table in the destination schema
869+
sql_backend.execute(f"CREATE TABLE {dst_schema.full_name}.fake_table (id INT)")
870+
871+
# Set the target table with non-existing source
872+
sql_backend.execute(
873+
f"ALTER TABLE {dst_schema.full_name}.fake_table SET "
874+
f"TBLPROPERTIES('upgraded_from' = '{src_table.full_name}');"
875+
)
876+
# Get the latest migration index
877+
tables = runtime_ctx.tables_crawler.snapshot()
878+
879+
# drop the source table
880+
sql_backend.execute(f"DROP TABLE {src_table.full_name}")
881+
# Assert tables contains the source table
882+
assert src_table.full_name in [table.full_name for table in tables]
883+
884+
migration_index = runtime_ctx.tables_migrator.index(force_refresh=True)
885+
assert migration_index
886+
# Assert that an error message was recorded containing a line with the text "which does no longer exist"
887+
expected_message = (
888+
f"failed-to-migrate: {src_table.schema_name}.{src_table.name} set as a source does no longer exist"
889+
)
890+
assert any(expected_message in record.message for record in caplog.records)

0 commit comments

Comments
 (0)