Skip to content

Commit

Permalink
Check if profile data with given profile name is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanau7 authored and Sumanau Sareen committed Apr 8, 2020
1 parent c76d6f3 commit 9991b06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 15 additions & 8 deletions core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
{profiles_file}/profiles.yml
""".format(profiles_file=PROFILES_DIR)

EMPTY_PROFILE_MESSAGE = """
dbt cannot run because profiles.yml is empty for this dbt project.
"""


def read_profile(profiles_dir: str) -> Dict[str, Any]:
path = os.path.join(profiles_dir, 'profiles.yml')
Expand All @@ -58,7 +54,12 @@ def read_profile(profiles_dir: str) -> Dict[str, Any]:
contents = load_file_contents(path, strip=False)
yaml_content = load_yaml_text(contents)
if not yaml_content:
raise DbtProfileError(EMPTY_PROFILE_MESSAGE)
msg = f'The profiles.yml file at {path} is empty'
raise DbtProfileError(
INVALID_PROFILE_MESSAGE.format(
error_string=msg
)
)
return yaml_content
except ValidationException as e:
msg = INVALID_PROFILE_MESSAGE.format(error_string=e)
Expand Down Expand Up @@ -287,7 +288,6 @@ def from_raw_profile_info(
# user_cfg is not rendered.
if user_cfg is None:
user_cfg = raw_profile.get('config')

# TODO: should it be, and the values coerced to bool?
target_name, profile_data = cls.render_profile(
raw_profile, profile_name, target_override, renderer
Expand Down Expand Up @@ -342,7 +342,15 @@ def from_raw_profiles(
# First, we've already got our final decision on profile name, and we
# don't render keys, so we can pluck that out
raw_profile = raw_profiles[profile_name]

if not raw_profile:
msg = (
f'Profile {profile_name} in profiles.yml is empty'
)
raise DbtProfileError(
INVALID_PROFILE_MESSAGE.format(
error_string=msg
)
)
user_cfg = raw_profiles.get('config')

return cls.from_raw_profile_info(
Expand Down Expand Up @@ -380,7 +388,6 @@ def render_from_args(
raw_profiles = read_profile(args.profiles_dir)
profile_name = cls.pick_profile_name(getattr(args, 'profile', None),
project_profile_name)

return cls.from_raw_profiles(
raw_profiles=raw_profiles,
profile_name=profile_name,
Expand Down
14 changes: 13 additions & 1 deletion test/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def setUp(self):
}
},
'target': 'other-postgres',
}
},
'empty_profile_data': {}
}
self.args = Args(profiles_dir=self.profiles_dir, cli_vars='{}',
version_check=True, project_dir=self.project_dir)
Expand Down Expand Up @@ -540,6 +541,17 @@ def test_empty_profile(self):
self.from_args()
self.assertIn('profiles.yml is empty', str(exc.exception))

def test_profile_with_empty_profile_data(self):
renderer = empty_renderer()
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
dbt.config.Profile.from_raw_profiles(
self.default_profile_data, 'empty_profile_data', renderer
)
self.assertIn(
'Profile empty_profile_data in profiles.yml is empty',
str(exc.exception)
)


class TestProject(BaseConfigTest):
def setUp(self):
Expand Down

0 comments on commit 9991b06

Please sign in to comment.