diff --git a/dbt_meshify/dbt_projects.py b/dbt_meshify/dbt_projects.py index eb14350..e65e694 100644 --- a/dbt_meshify/dbt_projects.py +++ b/dbt_meshify/dbt_projects.py @@ -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]: diff --git a/test-projects/split/split_proj/macros/cents_to_dollars.sql b/test-projects/split/split_proj/macros/cents_to_dollars.sql index dd3b99a..efe3361 100644 --- a/test-projects/split/split_proj/macros/cents_to_dollars.sql +++ b/test-projects/split/split_proj/macros/cents_to_dollars.sql @@ -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 %} diff --git a/test-projects/split/split_proj/macros/type_numeric.sql b/test-projects/split/split_proj/macros/type_numeric.sql new file mode 100644 index 0000000..9688adc --- /dev/null +++ b/test-projects/split/split_proj/macros/type_numeric.sql @@ -0,0 +1,3 @@ +{%- macro type_numeric() -%} + numeric +{%- endmacro -%} diff --git a/tests/integration/test_split_command.py b/tests/integration/test_split_command.py index 9d69aeb..abfd653 100644 --- a/tests/integration/test_split_command.py +++ b/tests/integration/test_split_command.py @@ -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(