Skip to content

Commit f0295bd

Browse files
authored
[DBT] Support profile config field, target.target_name, and an additional variant of config field missing the "+" (#597)
1 parent 7f4f696 commit f0295bd

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

sqlmesh/dbt/common.py

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class DbtContext:
5454
project_root: Path = Path()
5555
target_name: t.Optional[str] = None
5656
project_name: t.Optional[str] = None
57+
profile_name: t.Optional[str] = None
5758
project_schema: t.Optional[str] = None
5859
jinja_macros: JinjaMacroRegistry = field(
5960
default_factory=lambda: JinjaMacroRegistry(create_builtins_module="sqlmesh.dbt")

sqlmesh/dbt/package.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def load_config(
121121
nested_config = {}
122122
fields = {}
123123
for key, value in data.items():
124-
if key.startswith("+") or key in config_fields:
124+
if key.startswith("+") or key in config_fields or not isinstance(value, dict):
125125
fields[key[1:]] = value
126126
else:
127127
nested_config[key] = value

sqlmesh/dbt/profile.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ def load(cls, context: DbtContext) -> Profile:
4343
Returns:
4444
The Profile for the specified project
4545
"""
46-
if not context.project_name:
46+
if not context.profile_name:
4747
project_file = Path(context.project_root, PROJECT_FILENAME)
4848
if not project_file.exists():
4949
raise ConfigError(f"Could not find {PROJECT_FILENAME} in {context.project_root}")
5050

51-
context.project_name = context.render(load_yaml(project_file).get("name", ""))
52-
if not context.project_name:
51+
project_yaml = load_yaml(project_file)
52+
context.profile_name = context.render(
53+
project_yaml.get("profile", "")
54+
) or context.render(project_yaml.get("name", ""))
55+
if not context.profile_name:
5356
raise ConfigError(f"{project_file.stem} must include project name.")
5457

5558
profile_filepath = cls._find_profile(context.project_root)
@@ -80,19 +83,19 @@ def _read_profile(
8083
source = file.read()
8184
contents = load_yaml(context.render(source))
8285

83-
project_data = contents.get(context.project_name)
86+
project_data = contents.get(context.profile_name)
8487
if not project_data:
85-
raise ConfigError(f"Project '{context.project_name}' does not exist in profiles.")
88+
raise ConfigError(f"Profile '{context.profile_name}' not found in profiles.")
8689

8790
outputs = project_data.get("outputs")
8891
if not outputs:
89-
raise ConfigError(f"No outputs exist in profiles for Project '{context.project_name}'.")
92+
raise ConfigError(f"No outputs exist in profiles for '{context.profile_name}'.")
9093

9194
targets = {name: TargetConfig.load(name, output) for name, output in outputs.items()}
9295
default_target = context.render(project_data.get("target"))
9396
if default_target not in targets:
9497
raise ConfigError(
95-
f"Default target '{default_target}' not specified in profiles for Project '{context.project_name}'."
98+
f"Default target '{default_target}' not specified in profiles for '{context.profile_name}'."
9699
)
97100

98101
return (targets, default_target)

sqlmesh/dbt/project.py

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def load(cls, context: DbtContext) -> Project:
5656
if not context.project_name:
5757
raise ConfigError(f"{project_file_path.stem} must include project name.")
5858

59+
context.profile_name = (
60+
context.render(project_yaml.get("profile", "")) or context.project_name
61+
)
62+
5963
profile = Profile.load(context)
6064
context.target = (
6165
profile.targets[context.target_name]

sqlmesh/dbt/target.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ class TargetConfig(abc.ABC, PydanticModel):
3131
threads: The number of threads to run on
3232
"""
3333

34-
# sqlmesh
35-
name: str = ""
36-
3734
# dbt
3835
type: str
36+
name: str = ""
3937
schema_: str = Field(alias="schema")
4038
threads: int = 1
4139
profile_name: t.Optional[str] = None
@@ -76,6 +74,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
7674
def target_jinja(self, profile_name: str) -> AttributeDict:
7775
fields = self.dict().copy()
7876
fields["profile_name"] = profile_name
77+
fields["target_name"] = self.name
7978
return AttributeDict(fields)
8079

8180

0 commit comments

Comments
 (0)