Skip to content

Commit b114e1c

Browse files
authored
fix: Support config() method in jinja. (#962)
1 parent b448cfa commit b114e1c

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

sqlmesh/utils/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def __deepcopy__(self, memo: t.Dict[t.Any, AttributeDict]) -> AttributeDict:
7676
copy[k] = deepcopy(v, memo)
7777
return copy
7878

79+
def __call__(self, **kwargs: t.Dict[str, t.Any]) -> str:
80+
self.update(**kwargs)
81+
# Return an empty string, so that this method can be used within Jinja
82+
return ""
83+
7984

8085
class registry_decorator:
8186
"""A decorator that registers itself."""

tests/dbt/test_transformation.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import typing as t
12
from pathlib import Path
23

34
import agate
@@ -11,6 +12,7 @@
1112
IncrementalByUniqueKeyKind,
1213
ModelKind,
1314
ModelKindName,
15+
SqlModel,
1416
ViewKind,
1517
)
1618
from sqlmesh.dbt.column import (
@@ -211,6 +213,21 @@ def test_schema_jinja(sushi_test_project: Project):
211213
model_config.to_sqlmesh(context).render_query().sql() == "SELECT 1 AS one FROM sushi AS sushi"
212214

213215

216+
def test_config_jinja(sushi_test_project: Project):
217+
hook = "{{ config(alias='bar') }} {{ config.alias }}"
218+
model_config = ModelConfig(
219+
name="model",
220+
package_name="package",
221+
schema="sushi",
222+
sql="""SELECT 1 AS one FROM foo""",
223+
**{"pre-hook": hook},
224+
)
225+
context = sushi_test_project.context
226+
model = t.cast(SqlModel, model_config.to_sqlmesh(context))
227+
assert model.pre_statements[0].sql() == hook
228+
assert model.render_pre_statements()[0].sql() == "bar"
229+
230+
214231
def test_this(assert_exp_eq, sushi_test_project: Project):
215232
model_config = ModelConfig(
216233
name="model",

0 commit comments

Comments
 (0)