Skip to content

Commit 4866840

Browse files
refactor: simplify _get_adapter_type — remove broad try/except, streamline logic
Addresses Itamar's review feedback: - Removed the over-defensive try..except wrapper - Simplified flow: parse profiles.yml directly, then dbt_project.yml for profile name - Each missing-key case returns None with a debug log (no silent exception swallowing) Co-Authored-By: Itamar Hartstein <haritamar@gmail.com>
1 parent 91e41b3 commit 4866840

1 file changed

Lines changed: 55 additions & 66 deletions

File tree

elementary/clients/dbt/command_line_dbt_runner.py

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -93,77 +93,66 @@ def __init__(
9393
self._run_deps_if_needed()
9494

9595
def _get_adapter_type(self) -> Optional[str]:
96-
"""Resolve the adapter type from dbt profile configuration.
96+
"""Resolve the adapter type from ``profiles.yml``.
9797
98-
Reads ``dbt_project.yml`` to find the profile name, then looks up
99-
the selected target in ``profiles.yml`` to extract its ``type``
100-
field (e.g. ``"bigquery"``, ``"snowflake"``).
98+
Reads the profile name from ``dbt_project.yml``, then looks up the
99+
selected target in ``profiles.yml`` to extract its ``type`` field
100+
(e.g. ``"bigquery"``, ``"snowflake"``).
101101
102-
Returns ``None`` when the adapter type cannot be determined (missing
103-
files, missing keys, parse errors, etc.).
102+
Returns ``None`` when profiles.yml or the expected keys are missing.
104103
"""
105-
try:
106-
# Read dbt_project.yml to get the profile name.
107-
dbt_project_path = os.path.join(self.project_dir, "dbt_project.yml")
108-
if not os.path.exists(dbt_project_path):
109-
logger.debug("dbt_project.yml not found at %s", dbt_project_path)
110-
return None
111-
112-
with open(dbt_project_path) as f:
113-
dbt_project = yaml.safe_load(f)
114-
115-
profile_name = dbt_project.get("profile")
116-
if not profile_name:
117-
logger.debug("No profile name found in dbt_project.yml")
118-
return None
119-
120-
# Determine profiles directory.
121-
if self.profiles_dir:
122-
profiles_dir = self.profiles_dir
123-
else:
124-
profiles_dir = os.path.join(os.path.expanduser("~"), ".dbt")
125-
126-
profiles_path = os.path.join(profiles_dir, "profiles.yml")
127-
if not os.path.exists(profiles_path):
128-
logger.debug("profiles.yml not found at %s", profiles_path)
129-
return None
130-
131-
with open(profiles_path) as f:
132-
profiles = yaml.safe_load(f)
133-
134-
profile = profiles.get(profile_name)
135-
if not profile:
136-
logger.debug("Profile '%s' not found in profiles.yml", profile_name)
137-
return None
138-
139-
# Determine which target to use.
140-
target_name = self.target if self.target else profile.get("target")
141-
if not target_name:
142-
logger.debug("No target specified and no default target in profile")
143-
return None
144-
145-
outputs = profile.get("outputs", {})
146-
target_config = outputs.get(target_name)
147-
if not target_config:
148-
logger.debug("Target '%s' not found in profile outputs", target_name)
149-
return None
150-
151-
adapter_type = target_config.get("type")
152-
if adapter_type:
153-
logger.debug(
154-
"Resolved adapter type '%s' for target '%s'",
155-
adapter_type,
156-
target_name,
157-
)
158-
else:
159-
logger.debug("No type found in target configuration")
160-
161-
return adapter_type
162-
163-
except Exception as exc:
164-
logger.debug("Failed to resolve adapter type: %s", exc)
104+
profiles_dir = (
105+
self.profiles_dir
106+
if self.profiles_dir
107+
else os.path.join(os.path.expanduser("~"), ".dbt")
108+
)
109+
profiles_path = os.path.join(profiles_dir, "profiles.yml")
110+
if not os.path.exists(profiles_path):
111+
logger.debug("profiles.yml not found at %s", profiles_path)
112+
return None
113+
114+
with open(profiles_path) as f:
115+
profiles = yaml.safe_load(f)
116+
117+
# Read dbt_project.yml to get the profile name.
118+
dbt_project_path = os.path.join(self.project_dir, "dbt_project.yml")
119+
if not os.path.exists(dbt_project_path):
120+
logger.debug("dbt_project.yml not found at %s", dbt_project_path)
165121
return None
166122

123+
with open(dbt_project_path) as f:
124+
dbt_project = yaml.safe_load(f)
125+
126+
profile_name = dbt_project.get("profile")
127+
if not profile_name:
128+
logger.debug("No profile name found in dbt_project.yml")
129+
return None
130+
131+
profile = profiles.get(profile_name) if profiles else None
132+
if not profile:
133+
logger.debug("Profile '%s' not found in profiles.yml", profile_name)
134+
return None
135+
136+
# Determine which target to use.
137+
target_name = self.target or profile.get("target")
138+
if not target_name:
139+
logger.debug("No target specified and no default target in profile")
140+
return None
141+
142+
target_config = profile.get("outputs", {}).get(target_name)
143+
if not target_config:
144+
logger.debug("Target '%s' not found in profile outputs", target_name)
145+
return None
146+
147+
adapter_type = target_config.get("type")
148+
if adapter_type:
149+
logger.debug(
150+
"Resolved adapter type '%s' for target '%s'",
151+
adapter_type,
152+
target_name,
153+
)
154+
return adapter_type
155+
167156
def _inner_run_command(
168157
self,
169158
dbt_command_args: List[str],

0 commit comments

Comments
 (0)