Skip to content

Commit 3420390

Browse files
committed
Fix: The resolution order for dbt variables
1 parent 0e9c8a0 commit 3420390

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

sqlmesh/dbt/project.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,20 @@ def load(cls, context: DbtContext, variables: t.Optional[t.Dict[str, t.Any]] = N
9999
package = package_loader.load(path.parent)
100100
packages[package.name] = package
101101

102+
# Variable resolution precedence:
103+
# 1. Variable overrides
104+
# 2. Package-scoped variables in the root project's dbt_project.yml
105+
# 3. Global project variables in the root project's dbt_project.yml
106+
# 4. Variables in the package's dbt_project.yml
102107
all_project_variables = {**(project_yaml.get("vars") or {}), **(variable_overrides or {})}
103108
for name, package in packages.items():
104-
package_vars = all_project_variables.get(name)
105-
106-
if isinstance(package_vars, dict):
107-
package.variables.update(package_vars)
108-
109-
if name == context.project_name:
110-
package.variables.update(all_project_variables)
109+
if isinstance(all_project_variables.get(name), dict):
110+
project_vars_copy = all_project_variables.copy()
111+
package_scoped_vars = project_vars_copy.pop(name)
112+
package.variables.update(project_vars_copy)
113+
package.variables.update(package_scoped_vars)
111114
else:
112-
package.variables.update(variable_overrides)
115+
package.variables.update(all_project_variables)
113116

114117
return Project(context, profile, packages)
115118

tests/dbt/test_config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,26 @@ def test_variables(assert_exp_eq, sushi_test_project):
343343
"customers:customer_id": "customer_id",
344344
"some_var": ["foo", "bar"],
345345
},
346+
"some_var": "should be overridden in customers package",
346347
}
347348
expected_customer_variables = {
348-
"some_var": ["foo", "bar"],
349+
"some_var": ["foo", "bar"], # Takes precedence over the root project variable
349350
"some_other_var": 5,
350-
"yet_another_var": 5,
351351
"customers:bla": False,
352352
"customers:customer_id": "customer_id",
353+
"yet_another_var": 1, # Make sure that the project variable takes precedence
354+
"top_waiters:limit": "{{ get_top_waiters_limit() }}",
355+
"top_waiters:revenue": "revenue",
356+
"customers:boo": ["a", "b"],
357+
"nested_vars": {
358+
"some_nested_var": 2,
359+
},
360+
"dynamic_test_var": 3,
361+
"list_var": [
362+
{"name": "item1", "value": 1},
363+
{"name": "item2", "value": 2},
364+
],
353365
}
354-
355366
assert sushi_test_project.packages["sushi"].variables == expected_sushi_variables
356367
assert sushi_test_project.packages["customers"].variables == expected_customer_variables
357368

tests/fixtures/dbt/sushi_test/dbt_project.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ vars:
5050

5151
yet_another_var: 1
5252
dynamic_test_var: 3
53+
some_var: 'should be overridden in customers package'
5354

5455
customers:
5556
some_var: ["foo", "bar"]
@@ -74,4 +75,4 @@ on-run-start:
7475
on-run-end:
7576
- '{{ create_tables(schemas) }}'
7677
- 'DROP TABLE to_be_executed_last;'
77-
- '{{ graph_usage() }}'
78+
- '{{ graph_usage() }}'

0 commit comments

Comments
 (0)