Skip to content

Commit

Permalink
Allow inferring target name and add tests for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
aranke committed Feb 19, 2025
1 parent 5e0db17 commit ca7dc4d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
10 changes: 9 additions & 1 deletion core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def render_profile(
profile_name: str,
target_override: Optional[str],
renderer: ProfileRenderer,
infer_target_name: bool = False,
) -> Tuple[str, Dict[str, Any]]:
"""This is a containment zone for the hateful way we're rendering
profiles.
Expand All @@ -275,6 +276,9 @@ def render_profile(
elif "target" in raw_profile:
# render the target if it was parsed from yaml
target_name = renderer.render_value(raw_profile["target"])
elif infer_target_name and len(raw_profile["outputs"]) == 1:
target_name = next(iter(raw_profile["outputs"]))
fire_event(MissingProfileTarget(profile_name=profile_name, target_name=target_name))
else:
target_name = "default"
fire_event(MissingProfileTarget(profile_name=profile_name, target_name=target_name))
Expand Down Expand Up @@ -345,7 +349,11 @@ def from_raw_profile_info(
)

secondary_target_name, secondary_profile_data = cls.render_profile(
secondary_raw_profile, secondary_profile_name, target_override, renderer
secondary_raw_profile,
secondary_profile_name,
target_override,
renderer,
infer_target_name=True,
)

if secondary_profile_data.get("secondary_profiles"):
Expand Down
91 changes: 88 additions & 3 deletions tests/unit/config/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,46 @@ def test_secondary_profiles_override_threads(self):
profile.threads, 10
) # primary profile should also have overridden threads

def test_secondary_profiles_multiple_outputs(self):
profile_data_with_multiple_outputs = deepcopy(self.default_profile_data)
profile_data_with_multiple_outputs["default"]["outputs"]["postgres"][
"secondary_profiles"
] = [
{
"secondary_profile_1": {
"target": "secondary_target_2",
"outputs": {
"secondary_target_1": {
"type": "postgres",
"host": "secondary-host",
"port": 1234,
"user": "secondary_user",
"password": "secondary_password",
"schema": "secondary_schema",
"database": "secondary_db",
},
"secondary_target_2": {
"type": "postgres",
"host": "another-secondary-host",
"port": 5678,
"user": "another_secondary_user",
"password": "another_secondary_password",
"schema": "another_secondary_schema",
"database": "another_secondary_db",
},
},
}
}
]
renderer = empty_profile_renderer()
profile = dbt.config.Profile.from_raw_profiles(
profile_data_with_multiple_outputs, "default", renderer
)
self.assertIn("secondary_profile_1", profile.secondary_profiles)
secondary_profile = profile.secondary_profiles["secondary_profile_1"]
self.assertEqual(secondary_profile.profile_name, "secondary_profile_1")
self.assertEqual(secondary_profile.target_name, "secondary_target_2")

def test_secondary_profiles_duplicate_names(self):
profile_data_with_duplicate_secondary = deepcopy(self.default_profile_data)
profile_data_with_duplicate_secondary["default"]["outputs"]["postgres"][
Expand Down Expand Up @@ -475,7 +515,7 @@ def test_secondary_profiles_no_outputs(self):
self.assertIn("outputs not specified", str(exc.exception))
self.assertIn("secondary_profile_1", str(exc.exception))

def test_secondary_profiles_no_target(self):
def test_secondary_profiles_no_target_single_output(self):
profile_data_with_no_target = deepcopy(self.default_profile_data)
profile_data_with_no_target["default"]["outputs"]["postgres"]["secondary_profiles"] = [
{
Expand All @@ -495,9 +535,54 @@ def test_secondary_profiles_no_target(self):
}
]
renderer = empty_profile_renderer()

profile = dbt.config.Profile.from_raw_profiles(
profile_data_with_no_target, "default", renderer
)
self.assertIn("secondary_profile_1", profile.secondary_profiles)
secondary_profile = profile.secondary_profiles["secondary_profile_1"]
self.assertEqual(secondary_profile.profile_name, "secondary_profile_1")
self.assertEqual(secondary_profile.target_name, "secondary_target_1")

def test_secondary_profiles_no_target_multiple_outputs(self):
profile_data_with_duplicate_secondary = deepcopy(self.default_profile_data)
profile_data_with_duplicate_secondary["default"]["outputs"]["postgres"][
"secondary_profiles"
] = [
{
"secondary_profile_1": {
"outputs": {
"secondary_target_1": {
"type": "postgres",
"host": "secondary-host",
"port": 1234,
"user": "secondary_user",
"password": "secondary_password",
"schema": "secondary_schema",
"database": "secondary_db",
},
"secondary_target_2": {
"type": "postgres",
"host": "another-secondary-host",
"port": 5678,
"user": "another_secondary_user",
"password": "another_secondary_password",
"schema": "another_secondary_schema",
"database": "another_secondary_db",
},
}
}
}
]
renderer = empty_profile_renderer()
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
dbt.config.Profile.from_raw_profiles(profile_data_with_no_target, "default", renderer)
self.assertIn("does not have a target named 'default'", str(exc.exception))
dbt.config.Profile.from_raw_profiles(
profile_data_with_duplicate_secondary, "default", renderer
)
self.assertIn(
"The profile 'secondary_profile_1' does not have a target named 'default'",
str(exc.exception),
)

def test_secondary_profiles_no_host(self):
profile_data_with_no_host = deepcopy(self.default_profile_data)
Expand Down

0 comments on commit ca7dc4d

Please sign in to comment.