Skip to content

Commit 02bf280

Browse files
authored
Fix: Fail the plan command if the selector expression doesn't return any models (#3968)
1 parent 3cd5dc2 commit 02bf280

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

sqlmesh/core/context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,13 @@ def plan_builder(
13411341
if restate_models is not None:
13421342
expanded_restate_models = model_selector.expand_model_selections(restate_models)
13431343

1344+
if (restate_models is not None and not expanded_restate_models) or (
1345+
backfill_models is not None and not backfill_models
1346+
):
1347+
raise PlanError(
1348+
"Selector did not return any models. Please check your model selection and try again."
1349+
)
1350+
13441351
snapshots = self._snapshots(models_override)
13451352
context_diff = self._context_diff(
13461353
environment or c.PROD,

tests/core/test_context.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
to_timestamp,
4848
yesterday_ds,
4949
)
50-
from sqlmesh.utils.errors import ConfigError, SQLMeshError, LinterError
50+
from sqlmesh.utils.errors import ConfigError, SQLMeshError, LinterError, PlanError
5151
from sqlmesh.utils.metaprogramming import Executable
5252
from tests.utils.test_helpers import use_terminal_console
5353
from tests.utils.test_filesystem import create_temp_file
@@ -1793,3 +1793,23 @@ def model5_entrypoint(context, **kwargs):
17931793
)
17941794

17951795
sushi_context.upsert_model(model5)
1796+
1797+
1798+
def test_plan_selector_expression_no_match(sushi_context: Context) -> None:
1799+
with pytest.raises(
1800+
PlanError,
1801+
match="Selector did not return any models. Please check your model selection and try again.",
1802+
):
1803+
sushi_context.plan("dev", select_models=["*missing*"])
1804+
1805+
with pytest.raises(
1806+
PlanError,
1807+
match="Selector did not return any models. Please check your model selection and try again.",
1808+
):
1809+
sushi_context.plan("dev", backfill_models=["*missing*"])
1810+
1811+
with pytest.raises(
1812+
PlanError,
1813+
match="Selector did not return any models. Please check your model selection and try again.",
1814+
):
1815+
sushi_context.plan("prod", restate_models=["*missing*"])

tests/core/test_plan.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,17 @@ def test_restate_models(sushi_context_pre_scheduling: Context):
779779
'"memory"."sushi"."waiter_revenue_by_day"',
780780
}
781781

782-
plan = sushi_context_pre_scheduling.plan(restate_models=["unknown_model"], no_prompts=True)
783-
assert not plan.has_changes
784-
assert not plan.restatements
785-
assert plan.models_to_backfill is None
782+
with pytest.raises(
783+
PlanError,
784+
match="Selector did not return any models. Please check your model selection and try again.",
785+
):
786+
sushi_context_pre_scheduling.plan(restate_models=["unknown_model"], no_prompts=True)
786787

787-
plan = sushi_context_pre_scheduling.plan(restate_models=["tag:unknown_tag"], no_prompts=True)
788-
assert not plan.has_changes
789-
assert not plan.restatements
790-
assert plan.models_to_backfill is None
788+
with pytest.raises(
789+
PlanError,
790+
match="Selector did not return any models. Please check your model selection and try again.",
791+
):
792+
sushi_context_pre_scheduling.plan(restate_models=["tag:unknown_tag"], no_prompts=True)
791793

792794
plan = sushi_context_pre_scheduling.plan(restate_models=["raw.demographics"], no_prompts=True)
793795
assert not plan.has_changes

0 commit comments

Comments
 (0)