diff --git a/.changes/unreleased/Fixes-20241015-121825.yaml b/.changes/unreleased/Fixes-20241015-121825.yaml new file mode 100644 index 00000000000..f5867871868 --- /dev/null +++ b/.changes/unreleased/Fixes-20241015-121825.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix warnings for models referring to a deprecated model +time: 2024-10-15T12:18:25.14525-04:00 +custom: + Author: gshank + Issue: "10833" diff --git a/core/dbt/contracts/graph/manifest.py b/core/dbt/contracts/graph/manifest.py index b556b479fb4..4ce887591d9 100644 --- a/core/dbt/contracts/graph/manifest.py +++ b/core/dbt/contracts/graph/manifest.py @@ -1315,25 +1315,6 @@ def singular_test_lookup(self) -> SingularTestLookup: def external_node_unique_ids(self): return [node.unique_id for node in self.nodes.values() if node.is_external_node] - def resolve_refs( - self, - source_node: ModelNode, - current_project: str, # TODO: ModelNode is overly restrictive typing - ) -> List[MaybeNonSource]: - resolved_refs: List[MaybeNonSource] = [] - for ref in source_node.refs: - resolved = self.resolve_ref( - source_node, - ref.name, - ref.package, - ref.version, - current_project, - source_node.package_name, - ) - resolved_refs.append(resolved) - - return resolved_refs - # Called by dbt.parser.manifest._process_refs & ManifestLoader.check_for_model_deprecations def resolve_ref( self, diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index 7ffd00febc5..493b562bbdc 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -573,36 +573,41 @@ def safe_update_project_parser_files_partially(self, project_parser_files: Dict) return project_parser_files def check_for_model_deprecations(self): + # build parent and child_maps + self.manifest.build_parent_and_child_maps() for node in self.manifest.nodes.values(): - if isinstance(node, ModelNode) and node.is_past_deprecation_date: - warn_or_error( - DeprecatedModel( - model_name=node.name, - model_version=version_to_str(node.version), - deprecation_date=node.deprecation_date.isoformat(), + if isinstance(node, ModelNode) and node.deprecation_date: + if node.is_past_deprecation_date: + warn_or_error( + DeprecatedModel( + model_name=node.name, + model_version=version_to_str(node.version), + deprecation_date=node.deprecation_date.isoformat(), + ) ) - ) - - resolved_refs = self.manifest.resolve_refs(node, self.root_project.project_name) - resolved_model_refs = [r for r in resolved_refs if isinstance(r, ModelNode)] - node.depends_on - for resolved_ref in resolved_model_refs: - if resolved_ref.deprecation_date: - if resolved_ref.is_past_deprecation_date: - event_cls = DeprecatedReference - else: - event_cls = UpcomingReferenceDeprecation - - warn_or_error( - event_cls( - model_name=node.name, - ref_model_package=resolved_ref.package_name, - ref_model_name=resolved_ref.name, - ref_model_version=version_to_str(resolved_ref.version), - ref_model_latest_version=str(resolved_ref.latest_version), - ref_model_deprecation_date=resolved_ref.deprecation_date.isoformat(), - ) + # At this point _process_refs should already have been called, and + # we just rebuilt the parent and child maps. + # Get the child_nodes and check for deprecations. + child_nodes = self.manifest.child_map[node.unique_id] + for child_unique_id in child_nodes: + child_node = self.manifest.nodes[child_unique_id] + if not isinstance(child_node, ModelNode): + continue + if node.is_past_deprecation_date: + event_cls = DeprecatedReference + else: + event_cls = UpcomingReferenceDeprecation + + warn_or_error( + event_cls( + model_name=child_node.name, + ref_model_package=node.package_name, + ref_model_name=node.name, + ref_model_version=version_to_str(node.version), + ref_model_latest_version=str(node.latest_version), + ref_model_deprecation_date=node.deprecation_date.isoformat(), ) + ) def check_for_spaces_in_resource_names(self): """Validates that resource names do not contain spaces diff --git a/tests/functional/deprecations/model_deprecations.py b/tests/functional/deprecations/test_model_deprecations.py similarity index 97% rename from tests/functional/deprecations/model_deprecations.py rename to tests/functional/deprecations/test_model_deprecations.py index 03e38b1220e..1318562c10f 100644 --- a/tests/functional/deprecations/model_deprecations.py +++ b/tests/functional/deprecations/test_model_deprecations.py @@ -1,8 +1,8 @@ import pytest from dbt.cli.main import dbtRunner -from dbt.exceptions import EventCompilationError from dbt.tests.util import run_dbt +from dbt_common.exceptions import EventCompilationError deprecated_model__yml = """ version: 2 @@ -52,7 +52,7 @@ def test_deprecation_warning_error_options(self, project): run_dbt(["--warn-error-options", '{"include": ["DeprecatedModel"]}', "parse"]) -class TestUpcomingReferenceDeprecatingWarning: +class TestUpcomingReferenceDeprecationWarning: @pytest.fixture(scope="class") def models(self): return {