Skip to content

Commit

Permalink
Fixes #7753: Fix regression in run-operation to not require the nam…
Browse files Browse the repository at this point in the history
…e of the package to run (#7811)
  • Loading branch information
aranke authored Jun 20, 2023
1 parent 8653ffc commit 0a1c73e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20230606-145217.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Fix regression in `run-operation` to not require the name of the package to
run
time: 2023-06-06T14:52:17.38538-07:00
custom:
Author: aranke
Issue: "7753"
26 changes: 19 additions & 7 deletions core/dbt/task/run_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
RunningOperationUncaughtError,
LogDebugStackTrace,
)
from dbt.exceptions import DbtInternalError
from dbt.node_types import NodeType
from dbt.task.base import ConfiguredTask

Expand All @@ -28,14 +29,13 @@ def _get_macro_parts(self):
if "." in macro_name:
package_name, macro_name = macro_name.split(".", 1)
else:
package_name = self.config.project_name
package_name = None

return package_name, macro_name

def _run_unsafe(self) -> agate.Table:
def _run_unsafe(self, package_name, macro_name) -> agate.Table:
adapter = get_adapter(self.config)

package_name, macro_name = self._get_macro_parts()
macro_kwargs = self.args.args

with adapter.connection_named("macro_{}".format(macro_name)):
Expand All @@ -52,8 +52,10 @@ def run(self) -> RunResultsArtifact:

success = True

package_name, macro_name = self._get_macro_parts()

try:
self._run_unsafe()
self._run_unsafe(package_name, macro_name)
except dbt.exceptions.Exception as exc:
fire_event(RunningOperationCaughtError(exc=str(exc)))
fire_event(LogDebugStackTrace(exc_info=traceback.format_exc()))
Expand All @@ -65,9 +67,19 @@ def run(self) -> RunResultsArtifact:

end = datetime.utcnow()

package_name, macro_name = self._get_macro_parts()
fqn = [NodeType.Operation, package_name, macro_name]
unique_id = ".".join(fqn)
macro = (
self.manifest.find_macro_by_name(macro_name, self.config.project_name, package_name)
if self.manifest
else None
)

if macro:
unique_id = macro.unique_id
fqn = unique_id.split(".")
else:
raise DbtInternalError(
f"dbt could not find a macro with the name '{macro_name}' in any package"
)

run_result = RunResult(
adapter_response={},
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/artifacts/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def test_run_operation(self, project):
results, log_output = run_dbt_and_capture(["run-operation", "alter_timezone"])
assert len(results) == 1
assert results[0].status == RunStatus.Success
assert results[0].unique_id == "operation.test.alter_timezone"
assert results[0].unique_id == "macro.test.alter_timezone"
assert "Timezone set to: America/Los_Angeles" in log_output

def test_run_model_with_operation(self, project):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/retry/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_run_operation(self, project):
)

expected_statuses = {
"operation.test.alter_timezone": RunStatus.Error,
"macro.test.alter_timezone": RunStatus.Error,
}

assert {n.unique_id: n.status for n in results.results} == expected_statuses
Expand Down
53 changes: 51 additions & 2 deletions tests/functional/run_operations/test_run_operations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import os

import pytest
import yaml

from dbt.tests.util import check_table_does_exist, run_dbt
from dbt.exceptions import DbtInternalError
from dbt.tests.util import (
check_table_does_exist,
run_dbt,
write_file,
mkdir,
run_dbt_and_capture,
rm_dir,
rm_file,
)
from tests.functional.run_operations.fixtures import happy_macros_sql, sad_macros_sql, model_sql


Expand Down Expand Up @@ -66,7 +76,11 @@ def test_macro_exception(self, project):
self.run_operation("syntax_error", False)

def test_macro_missing(self, project):
self.run_operation("this_macro_does_not_exist", False)
with pytest.raises(
DbtInternalError,
match="dbt could not find a macro with the name 'this_macro_does_not_exist' in any package",
):
self.run_operation("this_macro_does_not_exist", False)

def test_cannot_connect(self, project):
self.run_operation("no_args", extra_args=["--target", "noaccess"], expect_pass=False)
Expand All @@ -90,3 +104,38 @@ def test_access_graph(self, project):
def test_print(self, project):
# Tests that calling the `print()` macro does not cause an exception
self.run_operation("print_something")

def test_run_operation_local_macro(self, project):
pkg_macro = """
{% macro something_cool() %}
{{ log("something cool", info=true) }}
{% endmacro %}
"""

mkdir("pkg/macros")

write_file(pkg_macro, "pkg/macros/something_cool.sql")

pkg_yaml = """
packages:
- local: pkg
"""

write_file(pkg_yaml, "packages.yml")

pkg_dbt_project = """
name: 'pkg'
"""

write_file(pkg_dbt_project, "pkg/dbt_project.yml")

run_dbt(["deps"])

results, log_output = run_dbt_and_capture(["run-operation", "something_cool"])
assert "something cool" in log_output

results, log_output = run_dbt_and_capture(["run-operation", "pkg.something_cool"])
assert "something cool" in log_output

rm_dir("pkg")
rm_file("packages.yml")

0 comments on commit 0a1c73e

Please sign in to comment.