Skip to content

Commit 5d98263

Browse files
committed
Fix: properly handle empty vars mapping in dbt_project.yml
1 parent 1c4d0a8 commit 5d98263

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

sqlmesh/dbt/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ 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-
all_project_variables = {**project_yaml.get("vars", {}), **(variable_overrides or {})}
102+
all_project_variables = {**(project_yaml.get("vars") or {}), **(variable_overrides or {})}
103103
for name, package in packages.items():
104104
package_vars = all_project_variables.get(name)
105105

tests/dbt/test_config.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,61 @@ def test_sqlmesh_model_kwargs_columns_override():
10901090
{"c": ColumnConfig(name="c", data_type="uinteger")},
10911091
)
10921092
assert kwargs.get("columns") == {"c": exp.DataType.build(exp.DataType.Type.UINT)}
1093+
1094+
1095+
def test_empty_vars_config(tmp_path):
1096+
"""Test that a dbt project can be loaded with an empty vars config."""
1097+
dbt_project_dir = tmp_path / "test_project"
1098+
dbt_project_dir.mkdir()
1099+
1100+
# Create a minimal dbt_project.yml with empty vars
1101+
dbt_project_yml = dbt_project_dir / "dbt_project.yml"
1102+
dbt_project_yml.write_text("""
1103+
name: test_empty_vars
1104+
1105+
version: "1.0.0"
1106+
config-version: 2
1107+
1108+
profile: test_empty_vars
1109+
1110+
models:
1111+
+start: Jan 1 2022
1112+
1113+
# Empty vars section - various ways to specify empty
1114+
vars:
1115+
""")
1116+
1117+
# Create a minimal profiles.yml
1118+
profiles_yml = dbt_project_dir / "profiles.yml"
1119+
profiles_yml.write_text("""
1120+
test_empty_vars:
1121+
outputs:
1122+
dev:
1123+
type: duckdb
1124+
schema: test
1125+
target: dev
1126+
""")
1127+
1128+
1129+
# Create a simple model
1130+
model = dbt_project_dir / "models" / "some_model.sql"
1131+
model.parent.mkdir(parents=True, exist_ok=True)
1132+
model.write_text("SELECT 1 as id")
1133+
1134+
# Load the project
1135+
from sqlmesh.dbt.context import DbtContext
1136+
from sqlmesh.dbt.project import Project
1137+
from sqlmesh.core.config import Config
1138+
1139+
context = DbtContext(project_root=dbt_project_dir, sqlmesh_config=Config())
1140+
1141+
# This should not raise an error even with empty vars
1142+
project = Project.load(context)
1143+
1144+
# Verify the project loaded successfully
1145+
assert project.packages["test_empty_vars"] is not None
1146+
assert project.packages["test_empty_vars"].name == "test_empty_vars"
1147+
1148+
# Verify the variables are empty (not causing any issues)
1149+
assert project.packages["test_empty_vars"].variables == {}
1150+
assert project.context.variables == {}

0 commit comments

Comments
 (0)