Skip to content

Commit 7ac8e5f

Browse files
committed
Attempt to infer whether path was passed in
1 parent 58b78d0 commit 7ac8e5f

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

sqlmesh/core/context.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,15 @@ def get_model(
885885
if normalized_name in self._models:
886886
return self._models[normalized_name]
887887
except:
888-
if raise_if_missing:
889-
raise SQLMeshError(f"Cannot find model with name '{model_or_snapshot}'")
888+
pass
889+
890+
if raise_if_missing:
891+
if model_or_snapshot.endswith((".sql", ".py")):
892+
msg = "Resolving models by path is not supported, please pass in the model name instead."
893+
else:
894+
msg = f"Cannot find model with name '{model_or_snapshot}'"
895+
896+
raise SQLMeshError(msg)
890897

891898
return None
892899

tests/core/test_integration.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6154,7 +6154,14 @@ def test_render_path_instead_of_model(tmp_path: Path):
61546154
create_temp_file(tmp_path, Path("models/test.sql"), "MODEL (name test_model); SELECT 1 AS col")
61556155
ctx = Context(paths=tmp_path, config=Config())
61566156

6157-
with pytest.raises(SQLMeshError, match="Cannot find model with name 'models/test.sql'"):
6158-
ctx.render("models/test.sql")
6157+
# Case 1: Fail gracefully when the user is passing in a path instead of a model name
6158+
for test_model in ["models/test.sql", "models/test.py"]:
6159+
with pytest.raises(SQLMeshError, match="Resolving models by path is not supported, please pass in the model name instead."):
6160+
ctx.render(test_model)
61596161

6162+
# Case 2: Fail gracefully when the model name is not found
6163+
with pytest.raises(SQLMeshError, match="Cannot find model with name 'incorrect_model'"):
6164+
ctx.render("incorrect_model")
6165+
6166+
# Case 3: Render the model successfully
61606167
assert ctx.render("test_model").sql() == 'SELECT 1 AS "col"'

0 commit comments

Comments
 (0)