Skip to content

Commit 3917ab9

Browse files
committed
feat: support jinja in dbt vars
1 parent db58405 commit 3917ab9

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

sqlmesh/dbt/context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ def set_and_render_variables(self, variables: t.Dict[str, t.Any], package: str)
105105

106106
def _render_var(value: t.Any) -> t.Any:
107107
if isinstance(value, str):
108-
return jinja_environment.from_string(value).render()
108+
return jinja_environment.from_string(
109+
value,
110+
jinja_environment.globals.get(package), # type: ignore
111+
).render()
109112
if isinstance(value, list):
110113
return [_render_var(v) for v in value]
111114
if isinstance(value, dict):

tests/dbt/test_config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from pytest_mock import MockerFixture
99

1010
from sqlglot import exp
11+
12+
from sqlmesh import Context
1113
from sqlmesh.core.audit import StandaloneAudit
1214
from sqlmesh.core.config import Config, ModelDefaultsConfig
1315
from sqlmesh.core.dialect import jinja_query
@@ -360,7 +362,7 @@ def test_variables(assert_exp_eq, sushi_test_project):
360362
# Finally, check that variable scoping & overwriting (some_var) works as expected
361363
expected_sushi_variables = {
362364
"yet_another_var": 1,
363-
"top_waiters:limit": 10,
365+
"top_waiters:limit": "{{ get_top_waiters_limit() }}",
364366
"top_waiters:revenue": "revenue",
365367
"customers:boo": ["a", "b"],
366368
"nested_vars": {
@@ -389,6 +391,11 @@ def test_variables(assert_exp_eq, sushi_test_project):
389391
assert sushi_test_project.packages["customers"].variables == expected_customer_variables
390392

391393

394+
@pytest.mark.slow
395+
def test_jinja_in_dbt_variables(sushi_test_dbt_context: Context):
396+
assert sushi_test_dbt_context.render("sushi.top_waiters").sql().endswith("LIMIT 10")
397+
398+
392399
@pytest.mark.slow
393400
def test_nested_variables(sushi_test_project):
394401
model_config = ModelConfig(

tests/fixtures/dbt/sushi_test/dbt_project.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sources:
3939
identifier: false
4040

4141
vars:
42-
top_waiters:limit: 10
42+
top_waiters:limit: "{{ get_top_waiters_limit() }}"
4343
'top_waiters:revenue': "revenue"
4444

4545
# The following are only used for testing purposes
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro get_top_waiters_limit() %}
2+
10
3+
{% endmacro %}

0 commit comments

Comments
 (0)