Skip to content

Commit 5d5c371

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

2 files changed

Lines changed: 22 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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6149,12 +6149,23 @@ def test_missing_connection_config():
61496149
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
61506150
)
61516151

6152+
61526153
@use_terminal_console
61536154
def test_render_path_instead_of_model(tmp_path: Path):
61546155
create_temp_file(tmp_path, Path("models/test.sql"), "MODEL (name test_model); SELECT 1 AS col")
61556156
ctx = Context(paths=tmp_path, config=Config())
61566157

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

6170+
# Case 3: Render the model successfully
61606171
assert ctx.render("test_model").sql() == 'SELECT 1 AS "col"'

0 commit comments

Comments
 (0)