Skip to content

Commit 0fe0645

Browse files
simen-strandclaude
andcommitted
Fix!: do not apply model_defaults statements to external models
External models inherited the model_defaults pre_statements, post_statements and on_virtual_update, but are built without the project's macros and never execute statements. When a default statement called a project macro and its text changed, the plan rendered the external model's statements to compare them and failed with "Macro '<name>' does not exist". Stop external models from inheriting these defaults, as is already done for the default gateway. This changes the fingerprint of external models in projects that use default statements; the empty migration bumps the state schema version so cached models are rebuilt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Simen Strand <simen.strand@netcheck.de>
1 parent c4dbac4 commit 0fe0645

5 files changed

Lines changed: 103 additions & 1 deletion

File tree

‎docs/reference/model_configuration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ You can also use the `@model_kind_name` variable to fine-tune control over `phys
136136
)
137137
```
138138

139-
You can aso define `pre_statements`, `post_statements` and `on_virtual_update` statements at the project level that will be applied to all models. These default statements are merged with any model-specific statements, with default statements executing first, followed by model-specific statements.
139+
You can aso define `pre_statements`, `post_statements` and `on_virtual_update` statements at the project level that will be applied to all models except [external models](../concepts/models/external_models.md). These default statements are merged with any model-specific statements, with default statements executing first, followed by model-specific statements.
140140

141141
=== "YAML"
142142

‎sqlmesh/core/model/definition.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,10 @@ def _create_model(
26142614
# external_models.yaml, so it must remain explicit rather than inheriting the
26152615
# gateway used to execute managed models in the project.
26162616
defaults.pop("gateway", None)
2617+
# External models are symbolic, so their statements are never executed. They are also
2618+
# built without the project's macros, so default statements could not be rendered.
2619+
for statement_field in ("pre_statements", "post_statements", "on_virtual_update"):
2620+
defaults.pop(statement_field, None)
26172621
if not issubclass(klass, SqlModel):
26182622
defaults.pop("optimize_query", None)
26192623

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
"""Exclude model_defaults statements from the fingerprint of external models."""
3+
4+
5+
def migrate_schemas(engine_adapter, schema, **kwargs): # type: ignore
6+
pass
7+
8+
9+
def migrate_rows(engine_adapter, schema, **kwargs): # type: ignore
10+
pass

‎tests/core/test_context.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,6 +4427,67 @@ def test_model_defaults_statements_with_on_virtual_update(tmp_path: Path):
44274427
assert model.on_virtual_update[1].sql() == "SELECT 'Model-specific update' AS message"
44284428

44294429

4430+
@pytest.mark.parametrize("statement_type", ["pre_statements", "post_statements"])
4431+
def test_model_defaults_statements_not_applied_to_external_models(
4432+
tmp_path: Path, statement_type: str
4433+
):
4434+
(tmp_path / "macros").mkdir()
4435+
(tmp_path / "macros" / "tag.py").write_text(
4436+
"""
4437+
from sqlmesh import macro
4438+
4439+
4440+
@macro()
4441+
def tag_query(evaluator, version):
4442+
return None
4443+
"""
4444+
)
4445+
(tmp_path / "external_models.yaml").write_text(
4446+
"""
4447+
- name: '"db"."ext_db"."ext_table"'
4448+
columns:
4449+
id: int
4450+
"""
4451+
)
4452+
(tmp_path / "models").mkdir()
4453+
(tmp_path / "models" / "test_model.sql").write_text(
4454+
"""
4455+
MODEL (name test.test_model, kind FULL);
4456+
4457+
SELECT id FROM db.ext_db.ext_table
4458+
"""
4459+
)
4460+
4461+
def write_config(version: str) -> None:
4462+
(tmp_path / "config.yaml").write_text(
4463+
f"""
4464+
gateways:
4465+
local:
4466+
connection:
4467+
type: duckdb
4468+
database: {tmp_path / "db.duckdb"}
4469+
default_gateway: local
4470+
model_defaults:
4471+
dialect: duckdb
4472+
{statement_type}:
4473+
- "@tag_query('{version}')"
4474+
"""
4475+
)
4476+
4477+
write_config("v1")
4478+
ctx = Context(paths=[tmp_path])
4479+
ctx.engine_adapter.execute("CREATE SCHEMA ext_db")
4480+
ctx.engine_adapter.execute("CREATE TABLE ext_db.ext_table AS SELECT 1 AS id")
4481+
ctx.plan(auto_apply=True, no_prompts=True)
4482+
4483+
write_config("v2")
4484+
ctx = Context(paths=[tmp_path])
4485+
plan = ctx.plan(no_prompts=True)
4486+
assert [s.name for s in plan.directly_modified] == ['"db"."test"."test_model"']
4487+
assert not ctx.get_model("db.ext_db.ext_table").pre_statements
4488+
assert not ctx.get_model("db.ext_db.ext_table").post_statements
4489+
4490+
44304491
def test_uppercase_gateway_external_models(tmp_path):
44314492
# Create a temporary SQLMesh project with uppercase gateway name
44324493
config_py = tmp_path / "config.py"

‎tests/core/test_model.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13552,6 +13552,33 @@ def test_external_model_does_not_inherit_model_defaults_gateway() -> None:
1355213552
assert explicit_external_model.gateway == "source_gateway"
1355313553

1355413554

13555+
def test_external_model_does_not_inherit_model_defaults_statements() -> None:
13556+
defaults = ModelDefaultsConfig(
13557+
dialect="duckdb",
13558+
pre_statements=["SET a = 1"],
13559+
post_statements=["ANALYZE"],
13560+
on_virtual_update=["SELECT 1"],
13561+
).dict()
13562+
13563+
external_model = create_external_model("source_schema.source", defaults=defaults)
13564+
assert not external_model.pre_statements
13565+
assert not external_model.post_statements
13566+
assert not external_model.on_virtual_update
13567+
13568+
# Statements passed explicitly are kept.
13569+
explicit_model = create_external_model(
13570+
"source_schema.explicit_source",
13571+
defaults=defaults,
13572+
pre_statements=[d.parse_one("SET b = 2")],
13573+
)
13574+
assert [s.sql() for s in explicit_model.pre_statements] == ["SET b = 2"]
13575+
13576+
sql_model = create_sql_model("a.b", d.parse_one("SELECT 1 AS x"), defaults=defaults)
13577+
assert [s.sql() for s in sql_model.pre_statements] == ["SET a = 1"]
13578+
assert [s.sql() for s in sql_model.post_statements] == ["ANALYZE"]
13579+
assert [s.sql() for s in sql_model.on_virtual_update] == ["SELECT 1"]
13580+
13581+
1355513582
def test_no_gateway_uses_global_default_catalog():
1355613583
"""
1355713584
Control test: when a model does NOT specify a gateway, the global

0 commit comments

Comments
 (0)