Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 2d63e94

Browse files
committed
adds connection parser error handling
1 parent 584206b commit 2d63e94

File tree

3 files changed

+175
-136
lines changed

3 files changed

+175
-136
lines changed

data_diff/dbt.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import yaml
77
from dataclasses import dataclass
88
from packaging.version import parse as parse_version
9-
from typing import List, Optional, Dict
9+
from typing import List, Optional, Dict, Tuple
1010

1111
import requests
1212
from dbt_artifacts_parser.parser import parse_run_results, parse_manifest
@@ -19,7 +19,7 @@
1919
send_event_json,
2020
is_tracking_enabled,
2121
)
22-
from .utils import run_as_daemon, truncate_error
22+
from .utils import get_from_dict_with_raise, run_as_daemon, truncate_error
2323
from . import connect_to_table, diff_tables, Algorithm
2424

2525
RUN_RESULTS_PATH = "/target/run_results.json"
@@ -297,18 +297,40 @@ def set_project_dict(self):
297297
with open(self.project_dir + PROJECT_FILE) as project:
298298
self.project_dict = yaml.safe_load(project)
299299

300-
def set_connection(self):
301-
with open(self.profiles_dir + PROFILES_FILE) as profiles:
300+
def _get_connection_creds(self) -> Tuple[Dict[str, str], str]:
301+
profiles_path = self.profiles_dir + PROFILES_FILE
302+
with open(profiles_path) as profiles:
302303
profiles = yaml.safe_load(profiles)
303304

304305
dbt_profile = self.project_dict.get("profile")
305-
profile_outputs = profiles.get(dbt_profile)
306-
profile_target = profile_outputs.get("target")
307-
credentials = profile_outputs.get("outputs").get(profile_target)
308-
conn_type = credentials.get("type").lower()
306+
307+
profile_outputs = get_from_dict_with_raise(
308+
profiles, dbt_profile, f"No profile '{dbt_profile}' found in '{profiles_path}'."
309+
)
310+
profile_target = get_from_dict_with_raise(
311+
profile_outputs, "target", f"No target found in profile '{dbt_profile}' in '{profiles_path}'."
312+
)
313+
outputs = get_from_dict_with_raise(
314+
profile_outputs, "outputs", f"No outputs found in profile '{dbt_profile}' in '{profiles_path}'."
315+
)
316+
credentials = get_from_dict_with_raise(
317+
outputs,
318+
profile_target,
319+
f"No credentials found for target '{profile_target}' in profile '{dbt_profile}' in '{profiles_path}'.",
320+
)
321+
conn_type = get_from_dict_with_raise(
322+
credentials,
323+
"type",
324+
f"No type found for target '{profile_target}' in profile '{dbt_profile}' in '{profiles_path}'.",
325+
)
326+
conn_type = conn_type.lower()
309327

310328
# values can contain env_vars
311329
rendered_credentials = ProfileRenderer().render_data(credentials)
330+
return rendered_credentials, conn_type
331+
332+
def set_connection(self):
333+
rendered_credentials, conn_type = self._get_connection_creds()
312334

313335
if conn_type == "snowflake":
314336
if rendered_credentials.get("password") is None or rendered_credentials.get("private_key_path") is not None:

data_diff/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import re
3-
from typing import Iterable, Sequence
3+
from typing import Dict, Iterable, Sequence
44
from urllib.parse import urlparse
55
import operator
66
import threading
@@ -77,3 +77,10 @@ def get_timestamp(_match):
7777
def truncate_error(error: str):
7878
first_line = error.split("\n", 1)[0]
7979
return re.sub("'(.*?)'", "'***'", first_line)
80+
81+
82+
def get_from_dict_with_raise(dictionary: Dict, key: str, error_message: str):
83+
result = dictionary.get(key)
84+
if result is None:
85+
raise ValueError(error_message)
86+
return result

0 commit comments

Comments
 (0)