Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macro parents #140

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions dbt_meshify/dbt_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,25 +390,29 @@ def _rename_project(self) -> None:
project_dict["name"] = self.name
self.project = Project.from_dict(project_dict)

def _get_macro_dependencies(self, unique_id: str) -> Set[str]:
resource = self.get_manifest_node(unique_id)
if not resource or any(isinstance(resource, class_) for class_ in [Documentation, Group]):
return set()
macros = resource.depends_on.macros # type: ignore
project_macros = {
macro
for macro in macros
if hashlib.md5((macro.split(".")[1]).encode()).hexdigest()
== self.manifest.metadata.project_id
}
return project_macros

def _get_custom_macros(self) -> Set[str]:
"""
get a set of macro unique_ids for all the selected resources
"""
macros_set = set()
for unique_id in self.resources:
resource = self.get_manifest_node(unique_id)
if not resource or any(
isinstance(resource, class_) for class_ in [Documentation, Group]
):
continue
macros = resource.depends_on.macros # type: ignore
project_macros = [
macro
for macro in macros
if hashlib.md5((macro.split(".")[1]).encode()).hexdigest()
== self.manifest.metadata.project_id
]
project_macros = self._get_macro_dependencies(unique_id)
macros_set.update(project_macros)
for macro in project_macros:
macros_set.update(self._get_macro_dependencies(macro))
return macros_set

def _get_indirect_groups(self) -> Set[str]:
Expand Down
2 changes: 1 addition & 1 deletion test-projects/split/split_proj/macros/cents_to_dollars.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{# A basic example for a project-wide macro to cast a column uniformly #}

{% macro cents_to_dollars(column_name, precision=2) -%}
({{ column_name }} / 100)::numeric(16, {{ precision }})
({{ column_name }} / 100)::{{ type_numeric() }}(16, {{ precision }})
{%- endmacro %}
3 changes: 3 additions & 0 deletions test-projects/split/split_proj/macros/type_numeric.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%- macro type_numeric() -%}
numeric
{%- endmacro -%}
4 changes: 4 additions & 0 deletions tests/integration/test_split_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def test_split_one_model_one_source_custom_macro(self, project):
assert (
Path(dest_project_path) / "my_new_project" / "macros" / "cents_to_dollars.sql"
).exists()
# copied custom macro parents too!
assert (
Path(dest_project_path) / "my_new_project" / "macros" / "type_numeric.sql"
).exists()

# assert only one source moved
source_yml = yaml.safe_load(
Expand Down