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

[Backport 1.9.latest] Fix #11012: Catch DbtRuntimeError for hooks #11045

Merged
merged 3 commits into from
Nov 25, 2024
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20241121-181739.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Catch DbtRuntimeError for hooks
time: 2024-11-21T18:17:39.753235Z
custom:
Author: aranke
Issue: "11012"
2 changes: 1 addition & 1 deletion core/dbt/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@
try:
with adapter.connection_named("master"):
self.safe_run_hooks(adapter, RunHookType.End, extras)
except (KeyboardInterrupt, SystemExit):
except (KeyboardInterrupt, SystemExit, DbtRuntimeError):

Check warning on line 925 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L925

Added line #L925 was not covered by tests
run_result = self.get_result(
results=self.node_results,
elapsed_time=time.time() - self.started_at,
Expand Down
36 changes: 35 additions & 1 deletion tests/functional/adapter/hooks/test_on_run_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from dbt.artifacts.schemas.results import RunStatus
from dbt.contracts.graph.nodes import HookNode
from dbt.tests.util import get_artifact, run_dbt_and_capture
from dbt.tests.util import get_artifact, run_dbt, run_dbt_and_capture
from dbt_common.exceptions import CompilationError


class Test__StartHookFail__FlagIsNone__ModelFail:
Expand Down Expand Up @@ -242,3 +243,36 @@ def test_results_in_context_hook_fail(self, project):
assert "Thread ID: main" in log_output
assert results[0].thread_id == "main"
assert "Num Results in context: 2" in log_output # failed hook and model


class Test__HookCompilationError:
@pytest.fixture(scope="class")
def models(self):
return {"my_model.sql": "select 1 as id"}

@pytest.fixture(scope="class")
def macros(self):
return {
"rce.sql": """
{% macro rce(relation) %}
{% if execute %}
{{ exceptions.raise_compiler_error("Always raise a compiler error in execute") }}
{% endif %}
{% endmacro %}
"""
}

@pytest.fixture(scope="class")
def project_config_update(self):
return {
"on-run-end": ["{{ rce() }}"],
}

def test_results(self, project):
with pytest.raises(CompilationError, match="Always raise a compiler error in execute"):
run_dbt(["run"], expect_pass=False)

run_results = get_artifact(project.project_root, "target", "run_results.json")
assert [(result["unique_id"], result["status"]) for result in run_results["results"]] == [
("model.test.my_model", RunStatus.Success)
]