Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions sqlmesh/core/snapshot/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 60 additions & 0 deletions tests/core/test_cleanup_snapshot.py
Original file line number Diff line number Diff line change
@@ -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,
)