Skip to content

Commit 7ed378e

Browse files
authored
Fix: Only include used refs into jinja globals when converting dbt models (#3033)
1 parent 8c2f9e3 commit 7ed378e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

sqlmesh/dbt/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def context_for_dependencies(self, dependencies: Dependencies) -> DbtContext:
273273
dependency_context.seeds = seeds
274274
dependency_context.models = models
275275
dependency_context.variables = variables
276+
dependency_context._refs = {**dependency_context._seeds, **dependency_context._models} # type: ignore
276277

277278
return dependency_context
278279

tests/dbt/test_transformation.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,23 @@ def test_snowflake_dynamic_table():
11681168
for required_property in ["target_lag", "snowflake_warehouse"]:
11691169
with pytest.raises(ConfigError, match=r".*must be set for dynamic tables"):
11701170
model.copy(update={required_property: None}).to_sqlmesh(context)
1171+
1172+
1173+
@pytest.mark.xdist_group("dbt_manifest")
1174+
def test_refs_in_jinja_globals(sushi_test_project: Project, mocker: MockerFixture):
1175+
context = sushi_test_project.context
1176+
1177+
sqlmesh_model = t.cast(
1178+
SqlModel,
1179+
sushi_test_project.packages["sushi"].models["simple_model_b"].to_sqlmesh(context),
1180+
)
1181+
assert set(sqlmesh_model.jinja_macros.global_objs["refs"].keys()) == {"simple_model_a"} # type: ignore
1182+
1183+
sqlmesh_model = t.cast(
1184+
SqlModel,
1185+
sushi_test_project.packages["sushi"].models["top_waiters"].to_sqlmesh(context),
1186+
)
1187+
assert set(sqlmesh_model.jinja_macros.global_objs["refs"].keys()) == { # type: ignore
1188+
"waiter_revenue_by_day",
1189+
"sushi.waiter_revenue_by_day",
1190+
}

0 commit comments

Comments
 (0)