Skip to content

Commit 16719bd

Browse files
Fix try-exception issue in unskip function and add relevant unit test
1 parent 3669faf commit 16719bd

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ def unskip_table_or_view(
145145
table_name (str): The table name of the table to be unskipped.
146146
load_table (Callable[[str, str], Table | None]): A function that loads a table from the metastore.
147147
"""
148+
table = load_table(schema_name, table_name)
149+
if table is None:
150+
raise NotFound("[TABLE_OR_VIEW_NOT_FOUND]")
148151
try:
149-
table = load_table(schema_name, table_name)
150-
if table is None:
151-
raise NotFound("[TABLE_OR_VIEW_NOT_FOUND]")
152152
self._sql_backend.execute(
153153
f"ALTER {table.kind} {escape_sql_identifier(table.full_name)} UNSET TBLPROPERTIES IF EXISTS('{self.UCX_SKIP_PROPERTY}');"
154154
)

tests/unit/hive_metastore/test_mapping.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from databricks.labs.blueprint.parallel import ManyError
77
from databricks.labs.lsql.backends import MockBackend, SqlBackend
88
from databricks.sdk import WorkspaceClient
9-
from databricks.sdk.errors import NotFound
9+
from databricks.sdk.errors import NotFound, BadRequest
1010
from databricks.sdk.errors.platform import ResourceConflict
1111
from databricks.sdk.service.catalog import TableInfo
1212

@@ -254,15 +254,27 @@ def test_unskip_on_schema():
254254
)
255255

256256

257-
def test_unskip_missing_table(caplog):
257+
def test_unskip_missing_table():
258258
ws = create_autospec(WorkspaceClient)
259259
sbe = create_autospec(SqlBackend)
260-
installation = MockInstallation()
261260
sbe.execute.side_effect = NotFound("[TABLE_OR_VIEW_NOT_FOUND]")
261+
installation = MockInstallation()
262262
mapping = TableMapping(installation, ws, sbe)
263-
mapping.unskip_table_or_view(schema_name='foo', table_name="table", load_table=lambda schema, table: None)
264263
ws.tables.get.assert_not_called()
265-
assert [rec.message for rec in caplog.records if "table not found" in rec.message.lower()]
264+
with pytest.raises(NotFound, match="[TABLE_OR_VIEW_NOT_FOUND]"):
265+
mapping.unskip_table_or_view(schema_name='foo', table_name="table", load_table=lambda schema, table: None)
266+
267+
268+
def test_unskip_badrequest(caplog):
269+
ws = create_autospec(WorkspaceClient)
270+
sbe = create_autospec(SqlBackend)
271+
sbe.execute.side_effect = BadRequest("[Bad command]")
272+
installation = MockInstallation()
273+
mapping = TableMapping(installation, ws, sbe)
274+
table = Table(catalog="catalog", database="schema", name="table", object_type="table", table_format="csv")
275+
ws.tables.get.assert_not_called()
276+
mapping.unskip_table_or_view(schema_name="schema", table_name="table", load_table=lambda _schema, _table: table)
277+
assert [rec.message for rec in caplog.records if "bad command" in rec.message.lower()]
266278

267279

268280
def test_skip_missing_schema(caplog):

0 commit comments

Comments
 (0)