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

Refactor code to properly handle reference deprecations #10852

Merged
merged 8 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20241015-121825.yaml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 0 additions & 19 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 30 additions & 27 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,36 +573,39 @@
return project_parser_files

def check_for_model_deprecations(self):
# build parent and child_maps
self.manifest.build_parent_and_child_maps()

Check warning on line 577 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L577

Added line #L577 was not covered by tests
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(

Check warning on line 581 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L579-L581

Added lines #L579 - L581 were not covered by tests
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 node.is_past_deprecation_date:
event_cls = DeprecatedReference

Check warning on line 595 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L591-L595

Added lines #L591 - L595 were not covered by tests
else:
event_cls = UpcomingReferenceDeprecation

Check warning on line 597 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L597

Added line #L597 was not covered by tests

warn_or_error(

Check warning on line 599 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L599

Added line #L599 was not covered by tests
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading