Skip to content

Commit 0509652

Browse files
Fix: Make the dbt graph available during parse time too
1 parent cd37002 commit 0509652

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
@@ -2189,7 +2189,7 @@ def test_on_run_start_end():
21892189
runtime_stage=RuntimeStage.BEFORE_ALL,
21902190
)
21912191

2192-
rendered_after_all = render_statements(
2192+
runtime_rendered_after_all = render_statements(
21932193
root_environment_statements.after_all,
21942194
dialect=sushi_context.default_dialect,
21952195
python_env=root_environment_statements.python_env,
@@ -2200,6 +2200,22 @@ def test_on_run_start_end():
22002200
engine_adapter=sushi_context.engine_adapter,
22012201
)
22022202

2203+
# not passing engine adapter simulates "parse-time" rendering
2204+
parse_time_rendered_after_all = render_statements(
2205+
root_environment_statements.after_all,
2206+
dialect=sushi_context.default_dialect,
2207+
python_env=root_environment_statements.python_env,
2208+
jinja_macros=root_environment_statements.jinja_macros,
2209+
snapshots=sushi_context.snapshots,
2210+
runtime_stage=RuntimeStage.AFTER_ALL,
2211+
environment_naming_info=EnvironmentNamingInfo(name="dev"),
2212+
)
2213+
2214+
# validate that the graph_table statement is the same between parse-time and runtime rendering
2215+
assert sorted(parse_time_rendered_after_all) == sorted(runtime_rendered_after_all)
2216+
graph_table_stmt = runtime_rendered_after_all[-1]
2217+
assert graph_table_stmt == parse_time_rendered_after_all[-1]
2218+
22032219
assert rendered_before_all == [
22042220
"CREATE TABLE IF NOT EXISTS analytic_stats (physical_table TEXT, evaluation_time TEXT)",
22052221
"CREATE TABLE IF NOT EXISTS to_be_executed_last (col TEXT)",
@@ -2212,10 +2228,9 @@ def test_on_run_start_end():
22122228
"CREATE OR REPLACE TABLE schema_table_sushi__dev AS SELECT 'sushi__dev' AS schema",
22132229
"DROP TABLE to_be_executed_last",
22142230
]
2215-
assert sorted(rendered_after_all[:-1]) == sorted(expected_statements)
2231+
assert sorted(runtime_rendered_after_all[:-1]) == sorted(expected_statements)
22162232

22172233
# Assert the models with their materialisations are present in the rendered graph_table statement
2218-
graph_table_stmt = rendered_after_all[-1]
22192234
assert "'model.sushi.simple_model_a' AS unique_id, 'table' AS materialized" in graph_table_stmt
22202235
assert "'model.sushi.waiters' AS unique_id, 'ephemeral' AS materialized" in graph_table_stmt
22212236
assert "'model.sushi.simple_model_b' AS unique_id, 'table' AS materialized" in graph_table_stmt

0 commit comments

Comments
 (0)