Skip to content

Commit 93d0d8a

Browse files
committed
fix: raise SQLMeshError when invalidating a nonexistent environment
The sqlmesh invalidate command previously accepted any environment name, even if the environment did not exist, silently reporting success. Now it checks that the environment exists first and raises a clear SQLMeshError if it does not. Fixes #5621 Signed-off-by: Nuzia Rijki <noezhiya@gmail.com>
1 parent fa46264 commit 93d0d8a

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

sqlmesh/core/context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,9 @@ def invalidate_environment(self, name: str, sync: bool = False) -> None:
18311831
be deleted asynchronously by the janitor process.
18321832
"""
18331833
name = Environment.sanitize_name(name)
1834+
stored_environment = self.state_sync.get_environment(name)
1835+
if not stored_environment:
1836+
raise SQLMeshError(f"Environment '{name}' does not exist.")
18341837
self.state_sync.invalidate_environment(name)
18351838
if sync:
18361839
self._cleanup_environments(name=name)

tests/core/test_context.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,22 @@ def test_invalidate_environment_no_sync_skips_cleanup(sushi_context, mocker: Moc
12021202
state_sync_mock.delete_expired_environments.assert_not_called()
12031203

12041204

1205+
@pytest.mark.slow
1206+
def test_invalidate_environment_nonexistent_raises(
1207+
sushi_context, mocker: MockerFixture
1208+
) -> None:
1209+
"""invalidate_environment should raise SQLMeshError if the environment does not exist."""
1210+
state_sync_mock = mocker.patch.object(
1211+
type(sushi_context), "state_sync", new_callable=mocker.PropertyMock
1212+
).return_value
1213+
state_sync_mock.get_environment.return_value = None
1214+
1215+
with pytest.raises(SQLMeshError, match="Environment 'doesnotexist' does not exist."):
1216+
sushi_context.invalidate_environment("doesnotexist")
1217+
1218+
state_sync_mock.invalidate_environment.assert_not_called()
1219+
1220+
12051221
@pytest.mark.slow
12061222
def test_plan_default_end(sushi_context_pre_scheduling: Context):
12071223
prod_plan_builder = sushi_context_pre_scheduling.plan_builder("prod")

0 commit comments

Comments
 (0)