Skip to content

Commit 0c8dcb5

Browse files
Fix: Make the dbt graph available during parse time too (#5329)
1 parent 12c568e commit 0c8dcb5

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

sqlmesh/dbt/adapter.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def compare_dbr_version(self, major: int, minor: int) -> int:
168168

169169
@property
170170
def graph(self) -> t.Any:
171-
return AttributeDict(
171+
flat_graph = self.jinja_globals.get("flat_graph", None)
172+
return flat_graph or AttributeDict(
172173
{
173174
"exposures": {},
174175
"groups": {},
@@ -276,10 +277,6 @@ def __init__(
276277
**table_mapping,
277278
}
278279

279-
@property
280-
def graph(self) -> t.Any:
281-
return self.jinja_globals.get("flat_graph", super().graph)
282-
283280
def get_relation(
284281
self, database: t.Optional[str], schema: str, identifier: str
285282
) -> t.Optional[BaseRelation]:

tests/dbt/test_transformation.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@ def test_on_run_start_end():
21972197
runtime_stage=RuntimeStage.BEFORE_ALL,
21982198
)
21992199

2200-
rendered_after_all = render_statements(
2200+
runtime_rendered_after_all = render_statements(
22012201
root_environment_statements.after_all,
22022202
dialect=sushi_context.default_dialect,
22032203
python_env=root_environment_statements.python_env,
@@ -2208,6 +2208,22 @@ def test_on_run_start_end():
22082208
engine_adapter=sushi_context.engine_adapter,
22092209
)
22102210

2211+
# not passing engine adapter simulates "parse-time" rendering
2212+
parse_time_rendered_after_all = render_statements(
2213+
root_environment_statements.after_all,
2214+
dialect=sushi_context.default_dialect,
2215+
python_env=root_environment_statements.python_env,
2216+
jinja_macros=root_environment_statements.jinja_macros,
2217+
snapshots=sushi_context.snapshots,
2218+
runtime_stage=RuntimeStage.AFTER_ALL,
2219+
environment_naming_info=EnvironmentNamingInfo(name="dev"),
2220+
)
2221+
2222+
# validate that the graph_table statement is the same between parse-time and runtime rendering
2223+
assert sorted(parse_time_rendered_after_all) == sorted(runtime_rendered_after_all)
2224+
graph_table_stmt = runtime_rendered_after_all[-1]
2225+
assert graph_table_stmt == parse_time_rendered_after_all[-1]
2226+
22112227
assert rendered_before_all == [
22122228
"CREATE TABLE IF NOT EXISTS analytic_stats (physical_table TEXT, evaluation_time TEXT)",
22132229
"CREATE TABLE IF NOT EXISTS to_be_executed_last (col TEXT)",
@@ -2220,10 +2236,9 @@ def test_on_run_start_end():
22202236
"CREATE OR REPLACE TABLE schema_table_sushi__dev AS SELECT 'sushi__dev' AS schema",
22212237
"DROP TABLE to_be_executed_last",
22222238
]
2223-
assert sorted(rendered_after_all[:-1]) == sorted(expected_statements)
2239+
assert sorted(runtime_rendered_after_all[:-1]) == sorted(expected_statements)
22242240

22252241
# Assert the models with their materialisations are present in the rendered graph_table statement
2226-
graph_table_stmt = rendered_after_all[-1]
22272242
assert "'model.sushi.simple_model_a' AS unique_id, 'table' AS materialized" in graph_table_stmt
22282243
assert "'model.sushi.waiters' AS unique_id, 'ephemeral' AS materialized" in graph_table_stmt
22292244
assert "'model.sushi.simple_model_b' AS unique_id, 'table' AS materialized" in graph_table_stmt

0 commit comments

Comments
 (0)