Skip to content

Commit

Permalink
exclude hook results from results in on-run-end context
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx committed Oct 18, 2024
1 parent 8be0635 commit 38a83aa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/dbt/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@ def after_run(self, adapter, results) -> None:

extras = {
"schemas": list({s for _, s in database_schema_set}),
"results": results,
"results": [
r for r in results if r.thread_id != "main"
], # exclude hooks to preserve backwards compatibility
"database_schemas": list(database_schema_set),
}
with adapter.connection_named("master"):
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/adapter/hooks/test_on_run_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,41 @@ def test_results(self, project):

run_results = get_artifact(project.project_root, "target", "run_results.json")
assert run_results["results"] == []


class Test__HookContext:
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"on-run-start": [
"select 1 as id", # success
"select 1 as id", # success
],
"on-run-end": [
'{{ log("Num Results in context: " ~ results|length)}}'
"{{ output_thread_ids(results) }}",
],
}

@pytest.fixture(scope="class")
def macros(self):
return {
"log.sql": """
{% macro output_thread_ids(results) %}
{% for result in results %}
{{ log("Thread ID: " ~ result.thread_id) }}
{% endfor %}
{% endmacro %}
"""
}

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

def test_results_in_context(self, project):
results, log_output = run_dbt_and_capture(["--debug", "run"])
assert "Thread ID: " in log_output
assert "Thread ID: main" not in log_output
assert results[0].thread_id == "main"
assert "Num Results in context: 1" in log_output

0 comments on commit 38a83aa

Please sign in to comment.