Skip to content

Commit 58b78d0

Browse files
committed
Chore: Ensure Context::get_model() fails gracefully
1 parent 76f52e6 commit 58b78d0

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

sqlmesh/core/context.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,12 @@ def get_model(
869869
Returns:
870870
The expected model.
871871
"""
872-
if isinstance(model_or_snapshot, str):
872+
if isinstance(model_or_snapshot, Snapshot):
873+
return model_or_snapshot.model
874+
if not isinstance(model_or_snapshot, str):
875+
return model_or_snapshot
876+
877+
try:
873878
# We should try all dialects referenced in the project for cases when models use mixed dialects.
874879
for dialect in self._all_dialects:
875880
normalized_name = normalize_model_name(
@@ -879,13 +884,9 @@ def get_model(
879884
)
880885
if normalized_name in self._models:
881886
return self._models[normalized_name]
882-
elif isinstance(model_or_snapshot, Snapshot):
883-
return model_or_snapshot.model
884-
else:
885-
return model_or_snapshot
886-
887-
if raise_if_missing:
888-
raise SQLMeshError(f"Cannot find model for '{model_or_snapshot}'")
887+
except:
888+
if raise_if_missing:
889+
raise SQLMeshError(f"Cannot find model with name '{model_or_snapshot}'")
889890

890891
return None
891892

tests/core/test_integration.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6148,3 +6148,13 @@ def test_missing_connection_config():
61486148
ctx = Context(
61496149
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
61506150
)
6151+
6152+
@use_terminal_console
6153+
def test_render_path_instead_of_model(tmp_path: Path):
6154+
create_temp_file(tmp_path, Path("models/test.sql"), "MODEL (name test_model); SELECT 1 AS col")
6155+
ctx = Context(paths=tmp_path, config=Config())
6156+
6157+
with pytest.raises(SQLMeshError, match="Cannot find model with name 'models/test.sql'"):
6158+
ctx.render("models/test.sql")
6159+
6160+
assert ctx.render("test_model").sql() == 'SELECT 1 AS "col"'

0 commit comments

Comments
 (0)