Skip to content

Commit 9246187

Browse files
committed
fix: load materializations before destroy snapshot reads
Signed-off-by: lafirm <136463254+lafirm@users.noreply.github.com>
1 parent 8a7edf8 commit 9246187

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎sqlmesh/core/context.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ def run_janitor(
10191019

10201020
@python_api_analytics
10211021
def destroy(self) -> bool:
1022+
# Load materializations before any state writes so a failure aborts destroy without invalidating environments
1023+
self._load_materializations()
1024+
10221025
success = False
10231026

10241027
# Collect resources to be deleted
@@ -3158,9 +3161,6 @@ def _context_diff(
31583161
)
31593162

31603163
def _destroy(self) -> bool:
3161-
# Load materializations before any state writes so a failure aborts destroy without invalidating environments
3162-
self._load_materializations()
3163-
31643164
# Invalidate all environments, including prod
31653165
for environment in self.state_reader.get_environments():
31663166
self.state_sync.invalidate_environment(name=environment.name, protect_prod=False)

‎tests/core/integration/test_aux_commands.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_destroy_fails_before_invalidating_environments_on_materialization_error
125125
context._materializations_loaded = False
126126

127127
with pytest.raises(SQLMeshError, match="broken materialization"):
128-
context._destroy()
128+
context.destroy()
129129

130130
assert context.state_reader.get_environment("prod").expiration_ts is None
131131

‎tests/core/test_context.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,34 @@ def test_diff(sushi_context: Context, mocker: MockerFixture):
363363
assert success
364364

365365

366+
def test_destroy_loads_materializations_before_reading_state(mocker: MockerFixture):
367+
context = Context(config=Config(), load=False)
368+
calls = []
369+
environment = mocker.Mock(snapshots=[])
370+
371+
with (
372+
patch.object(
373+
context,
374+
"_load_materializations",
375+
side_effect=lambda: calls.append("materializations"),
376+
),
377+
patch.object(
378+
context.state_reader,
379+
"get_environments",
380+
side_effect=lambda: calls.append("environments") or [environment],
381+
),
382+
patch.object(
383+
context.state_reader,
384+
"get_snapshots",
385+
side_effect=lambda _: calls.append("snapshots") or {},
386+
),
387+
patch.object(context.console, "start_destroy", return_value=False),
388+
):
389+
context.destroy()
390+
391+
assert calls == ["materializations", "environments", "snapshots"]
392+
393+
366394
@pytest.mark.slow
367395
def test_evaluate_limit():
368396
context = Context(config=Config())

0 commit comments

Comments
 (0)