From 67860f9917795f9768d617f54706b7c1ccf1136e Mon Sep 17 00:00:00 2001 From: boomesh11 Date: Mon, 16 Nov 2026 17:18:18 +0530 Subject: [PATCH] fix(janitor): handle get_data_object failures during snapshot cleanup --- sqlmesh/core/snapshot/evaluator.py | 21 +++++++--- tests/core/test_cleanup_snapshot.py | 60 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 tests/core/test_cleanup_snapshot.py diff --git a/sqlmesh/core/snapshot/evaluator.py b/sqlmesh/core/snapshot/evaluator.py index 345f8816e6..af4d072204 100644 --- a/sqlmesh/core/snapshot/evaluator.py +++ b/sqlmesh/core/snapshot/evaluator.py @@ -1404,16 +1404,25 @@ def _cleanup_snapshot( # this situation can happen when a snapshot expires but downstream view snapshots that reference it have not yet expired cascade=True, ) - except Exception: + except Exception as ex: # Use `get_data_object` to check if the table exists instead of `table_exists` since the former # is based on `INFORMATION_SCHEMA` and avoids touching the table directly. # This is important when the table name is malformed for some reason and running any statement # that touches the table would result in an error. - if adapter.get_data_object(table_name) is not None: - raise - logger.warning( - "Skipping cleanup of table '%s' because it does not exist", table_name - ) + try: + data_object = adapter.get_data_object(table_name) + except Exception: + logger.warning( + "Skipping cleanup of table '%s' after delete failure (%s); failed to verify existence", + table_name, + ex, + ) + else: + if data_object is not None: + raise + logger.warning( + "Skipping cleanup of table '%s' because it does not exist", table_name + ) if on_complete is not None: on_complete(table_name) diff --git a/tests/core/test_cleanup_snapshot.py b/tests/core/test_cleanup_snapshot.py new file mode 100644 index 0000000000..dfe1d2ee30 --- /dev/null +++ b/tests/core/test_cleanup_snapshot.py @@ -0,0 +1,60 @@ +from types import SimpleNamespace +import pytest + +from sqlmesh.core.snapshot.evaluator import SnapshotEvaluator + + +def test_cleanup_snapshot_skips_when_get_data_object_raises(monkeypatch): + completed = [] + + class _FakeStrategy: + def delete(self, *args, **kwargs): + raise RuntimeError("drop failed") + + class _FakeAdapter: + def get_data_object(self, _table_name): + raise RuntimeError("catalog switch failed") + + monkeypatch.setattr( + "sqlmesh.core.snapshot.evaluator._evaluation_strategy", + lambda _snap, _adapter: _FakeStrategy(), + ) + + fake_table_info = SimpleNamespace(table_name=lambda is_deployable=False: "cat.sch.tbl", physical_schema="sch") + fake_snapshot = SimpleNamespace(table_info=fake_table_info) + + evaluator = SnapshotEvaluator.__new__(SnapshotEvaluator) + evaluator._cleanup_snapshot( + fake_snapshot, + dev_table_only=True, + adapter=_FakeAdapter(), + on_complete=completed.append, + ) + assert completed == ["cat.sch.tbl"] + + +def test_cleanup_snapshot_reraises_when_table_still_exists(monkeypatch): + class _FakeStrategy: + def delete(self, *args, **kwargs): + raise RuntimeError("drop failed") + + class _FakeAdapter: + def get_data_object(self, _table_name): + return object() + + monkeypatch.setattr( + "sqlmesh.core.snapshot.evaluator._evaluation_strategy", + lambda _snap, _adapter: _FakeStrategy(), + ) + + fake_table_info = SimpleNamespace(table_name=lambda is_deployable=False: "cat.sch.tbl", physical_schema="sch") + fake_snapshot = SimpleNamespace(table_info=fake_table_info) + + evaluator = SnapshotEvaluator.__new__(SnapshotEvaluator) + with pytest.raises(RuntimeError, match="drop failed"): + evaluator._cleanup_snapshot( + fake_snapshot, + dev_table_only=True, + adapter=_FakeAdapter(), + on_complete=None, + )