Skip to content

Commit

Permalink
Fire deprecation warning for custom microbatch macros
Browse files Browse the repository at this point in the history
  • Loading branch information
QMalcolm committed Nov 8, 2024
1 parent 63445b5 commit 26ba807
Show file tree
Hide file tree
Showing 6 changed files with 656 additions and 592 deletions.
6 changes: 6 additions & 0 deletions core/dbt/deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class MFCumulativeTypeParamsDeprecation(DBTDeprecation):
_event = "MFCumulativeTypeParamsDeprecation"


class MicrobatchMacroOutsideOfBatchesDeprecation(DBTDeprecation):
_name = "microbatch-macro-outside-of-batches-deprecation"
_event = "MicrobatchMacroOutsideOfBatchesDeprecation"


def renamed_env_var(old_name: str, new_name: str):
class EnvironmentVariableRenamed(DBTDeprecation):
_name = f"environment-variable-renamed:{old_name}"
Expand Down Expand Up @@ -178,6 +183,7 @@ def show_callback():
SourceFreshnessProjectHooksNotRun(),
MFTimespineWithoutYamlConfigurationDeprecation(),
MFCumulativeTypeParamsDeprecation(),
MicrobatchMacroOutsideOfBatchesDeprecation(),
]

deprecations: Dict[str, DBTDeprecation] = {d.name: d for d in deprecations_list}
Expand Down
8 changes: 8 additions & 0 deletions core/dbt/events/core_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ message MFCumulativeTypeParamsDeprecationMsg {
MFCumulativeTypeParamsDeprecation data = 2;
}

// D020
message MicrobatchMacroOutsideOfBatchesDeprecation {}

message MicrobatchMacroOutsideOfBatchesDeprecationMsg {
CoreEventInfo info = 1;
MicrobatchMacroOutsideOfBatchesDeprecation data = 2;
}

// I065
message DeprecatedModel {
string model_name = 1;
Expand Down
1,182 changes: 593 additions & 589 deletions core/dbt/events/core_types_pb2.py

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ def message(self) -> str:
return line_wrap_message(warning_tag(description))


class MicrobatchMacroOutsideOfBatchesDeprecation(WarnLevel):
def code(self) -> str:
return "D020"

Check warning on line 491 in core/dbt/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/events/types.py#L491

Added line #L491 was not covered by tests

def message(self) -> str:
description = "The use of a custom microbatch macro outside of batched execution is deprecated. To use it with batched execution, set `flags.require_batched_execution_for_custom_microbatch_strategy` to `True` in `dbt_project.yml`. In the future this will be the default behavior."

Check warning on line 494 in core/dbt/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/events/types.py#L494

Added line #L494 was not covered by tests

return line_wrap_message(warning_tag(description))

Check warning on line 496 in core/dbt/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/events/types.py#L496

Added line #L496 was not covered by tests


# =======================================================
# I - Project parsing
# =======================================================
Expand Down
18 changes: 18 additions & 0 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ def load(self) -> Manifest:

self.check_for_model_deprecations()
self.check_for_spaces_in_resource_names()
self.check_for_microbatch_deprecations()

return self.manifest

Expand Down Expand Up @@ -649,6 +650,23 @@ def check_for_spaces_in_resource_names(self):
else: # ERROR level
raise DbtValidationError("Resource names cannot contain spaces")

def check_for_microbatch_deprecations(self) -> None:
if not get_flags().require_batched_execution_for_custom_microbatch_strategy:
has_microbatch_model = False
for _, node in self.manifest.nodes.items():
if (
isinstance(node, ModelNode)
and node.config.materialized == "incremental"
and node.config.incremental_strategy == "microbatch"
):
has_microbatch_model = True
break

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

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L662-L663

Added lines #L662 - L663 were not covered by tests

if has_microbatch_model and self.manifest._microbatch_macro_is_core(
self.root_project.project_name
):
dbt.deprecations.warn("microbatch-macro-outside-of-batches-deprecation")

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

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L668

Added line #L668 was not covered by tests

def load_and_parse_macros(self, project_parser_files):
for project in self.all_projects.values():
if project.project_name not in project_parser_files:
Expand Down
24 changes: 21 additions & 3 deletions tests/functional/microbatch/test_microbatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import pytest

from dbt.events.types import LogModelResult, MicrobatchModelNoEventTimeInputs
from dbt.events.types import (
LogModelResult,
MicrobatchMacroOutsideOfBatchesDeprecation,
MicrobatchModelNoEventTimeInputs,
)
from dbt.tests.util import (
get_artifact,
patch_microbatch_end_time,
Expand Down Expand Up @@ -159,6 +163,10 @@ def project_config_update(self):
}
}

@pytest.fixture(scope="class")
def deprecation_catcher(self) -> EventCatcher:
return EventCatcher(MicrobatchMacroOutsideOfBatchesDeprecation)


class TestMicrobatchCustomUserStrategyDefault(BaseMicrobatchCustomUserStrategy):
@pytest.fixture(scope="class")
Expand All @@ -169,7 +177,11 @@ def project_config_update(self):
}
}

def test_use_custom_microbatch_strategy_by_default(self, project):
def test_use_custom_microbatch_strategy_by_default(
self,
project,
deprecation_catcher: EventCatcher,
):
with mock.patch.object(
type(project.adapter), "valid_incremental_strategies", lambda _: []
):
Expand All @@ -181,11 +193,15 @@ def test_use_custom_microbatch_strategy_by_default(self, project):
assert "custom microbatch strategy" in logs
# The custom strategy wasn't used with batch functionality
assert "START batch" not in logs
# Deprecation warning about custom microbatch macro fired
assert len(deprecation_catcher.caught_events) == 0


class TestMicrobatchCustomUserStrategyProjectFlagTrueValid(BaseMicrobatchCustomUserStrategy):
def test_use_custom_microbatch_strategy_project_flag_true_invalid_incremental_strategy(
self, project
self,
project,
deprecation_catcher: EventCatcher,
):
with mock.patch.object(
type(project.adapter), "valid_incremental_strategies", lambda _: ["microbatch"]
Expand All @@ -200,6 +216,8 @@ def test_use_custom_microbatch_strategy_project_flag_true_invalid_incremental_st
assert "custom microbatch strategy" in logs
# The custom strategy was used with batch functionality
assert "START batch" in logs
# Deprecation warning about custom microbatch macro not fired
assert len(deprecation_catcher.caught_events) == 0


class TestMicrobatchCustomUserStrategyProjectFlagTrueInvalid(BaseMicrobatchCustomUserStrategy):
Expand Down

0 comments on commit 26ba807

Please sign in to comment.