Skip to content

Commit

Permalink
Merge pull request #195 from nicholasyager/fix/find_group_yaml
Browse files Browse the repository at this point in the history
Fix: The `group` command will check for existing group file paths.
  • Loading branch information
dave-connors-3 authored Jan 31, 2024
2 parents eb92e80 + 03d9792 commit dcb4d97
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
17 changes: 16 additions & 1 deletion dbt_meshify/dbt_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os
from pathlib import Path
from typing import Any, Dict, MutableMapping, Optional, Set, Union
from typing import Any, Dict, List, MutableMapping, Optional, Set, Union

import yaml
from dbt.contracts.graph.manifest import Manifest
Expand Down Expand Up @@ -309,6 +309,7 @@ def __init__(
self.path = path
self.dbt = dbt
resources = self.select_resources(output_key="unique_id")
self._group_definition_files: Optional[List[Path]] = None

super().__init__(manifest, project, catalog, name, resources)
self.jinja_blocks: Dict[str, JinjaBlock] = self.find_jinja_blocks()
Expand Down Expand Up @@ -390,6 +391,20 @@ def split(

return subproject

@property
def group_definition_files(self) -> List[Path]:
"""A list of files that contain group definitions, in order of how many groups are present."""

if self._group_definition_files is not None:
return self._group_definition_files

paths: Set[Path] = {
self.path / Path(group.original_file_path) for group in self.manifest.groups.values()
}
self._group_definition_files = list(paths)

return self._group_definition_files


class DbtSubProject(BaseDbtProject, PathedProject):
"""
Expand Down
10 changes: 9 additions & 1 deletion dbt_meshify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,15 @@ def create_group(
project = DbtProject.from_directory(path, read_catalog)

if group_yml_path is None:
group_yml_path = (path / Path("models/_groups.yml")).resolve()
existing_paths = project.group_definition_files
if len(existing_paths) > 1:
raise FatalMeshifyException(
"Unable to pick which group YAML file to use. Please specify one using the --group-yml-path argument."
)
if len(existing_paths) == 1:
group_yml_path = existing_paths[0]
else:
group_yml_path = (path / Path("models/_groups.yml")).resolve()
else:
group_yml_path = Path(group_yml_path).resolve()
logger.info(f"Creating new model group in file {group_yml_path.name}")
Expand Down
85 changes: 85 additions & 0 deletions tests/integration/test_group_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,88 @@ def test_read_catalog_group():
assert result.exit_code != 0
assert "dbt docs generate" not in result.stdout
teardown_test_project(dest_path_string)


def test_group_name_group_file():
"""Verify that providing a group yaml path results in a group yml being created at the appropriate path."""
setup_test_project(src_path_string, dest_path_string)
runner = CliRunner()
result = runner.invoke(
cli,
[
"group",
"test_group",
"--owner-name",
"Teenage Mutant Jinja Turtles",
"--select",
"+order",
"--project-path",
dest_path_string,
"--group-yml-path",
str(Path(dest_path_string) / "models" / "test_groups.yml"),
],
)

assert result.exit_code == 0
assert (Path(dest_path_string) / "models" / "test_groups.yml").exists()
teardown_test_project(dest_path_string)


def test_group_raises_exception_ambiguous_group_file():
"""The group command raises an exception if there are multiple group files present and `--group-yml-path` is not specified."""
setup_test_project(src_path_string, dest_path_string)

for index in range(2):
group_yaml = Path(dest_path_string) / "models" / f"groups_{index}.yml"
with open(group_yaml, "w") as file:
file.write(f"groups:\n - name: group_{index}\n owner:\n email: foo@example.com")

runner = CliRunner()
result = runner.invoke(
cli,
[
"group",
"test_group",
"--owner-name",
"Teenage Mutant Jinja Turtles",
"--select",
"+order",
"--project-path",
dest_path_string,
],
)

assert result.exit_code != 0
assert "Unable to pick which group YAML" in result.stdout
teardown_test_project(dest_path_string)


def test_defining_group_yaml_disambiguates_group_file():
"""The group command will not raise an exception if there are multiple group files present and `--group-yml-path` is specified."""
setup_test_project(src_path_string, dest_path_string)

for index in range(2):
group_yaml = Path(dest_path_string) / "models" / f"groups_{index}.yml"
with open(group_yaml, "w") as file:
file.write(f"groups:\n - name: group_{index}\n owner:\n email: foo@example.com")

runner = CliRunner()
result = runner.invoke(
cli,
[
"group",
"test_group",
"--owner-name",
"Teenage Mutant Jinja Turtles",
"--select",
"+order",
"--project-path",
dest_path_string,
"--group-yml-path",
str(Path(dest_path_string) / "models" / "test_groups.yml"),
],
)

assert result.exit_code == 0
assert (Path(dest_path_string) / "models" / "test_groups.yml").exists()
teardown_test_project(dest_path_string)

0 comments on commit dcb4d97

Please sign in to comment.