Skip to content

Commit

Permalink
Add run-operation to click CLI (#5552) (#6656)
Browse files Browse the repository at this point in the history
* Add run-operation to click CLI

* Add changelog entry

* PR feedback

* Fix unit test
  • Loading branch information
jtcohen6 authored Jan 20, 2023
1 parent f034948 commit 11c6222
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230119-105304.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Add dbt run-operation to click CLI
time: 2023-01-19T10:53:04.154871+01:00
custom:
Author: jtcohen6
Issue: "5552"
16 changes: 13 additions & 3 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from dbt.task.seed import SeedTask
from dbt.task.list import ListTask
from dbt.task.freshness import FreshnessTask
from dbt.task.run_operation import RunOperationTask


# CLI invocation
Expand Down Expand Up @@ -260,6 +261,8 @@ def deps(ctx, **kwargs):
# dbt init
@cli.command("init")
@click.pass_context
# for backwards compatibility, accept 'project_name' as an optional positional argument
@click.argument("project_name", required=False)
@p.profile
@p.profiles_dir
@p.project_dir
Expand All @@ -268,7 +271,7 @@ def deps(ctx, **kwargs):
@p.vars
@requires.preflight
def init(ctx, **kwargs):
"""Initialize a new DBT project."""
"""Initialize a new dbt project."""
click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
return None, True

Expand Down Expand Up @@ -364,17 +367,24 @@ def run(ctx, **kwargs):
# dbt run operation
@cli.command("run-operation")
@click.pass_context
@click.argument("macro")
@p.args
@p.profile
@p.profiles_dir
@p.project_dir
@p.target
@p.vars
@requires.preflight
@requires.profile
@requires.project
def run_operation(ctx, **kwargs):
"""Run the named macro with any supplied arguments."""
click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
return None, True
config = RuntimeConfig.from_parts(ctx.obj["project"], ctx.obj["profile"], ctx.obj["flags"])
task = RunOperationTask(ctx.obj["flags"], config)

results = task.run()
success = task.interpret_results(results)
return results, success


# dbt seed
Expand Down
12 changes: 1 addition & 11 deletions core/dbt/task/run_operation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from typing import Dict, Any
import traceback

import agate
Expand All @@ -8,7 +7,6 @@

import dbt.exceptions
from dbt.adapters.factory import get_adapter
from dbt.config.utils import parse_cli_vars
from dbt.contracts.results import RunOperationResultsArtifact
from dbt.exceptions import InternalException
from dbt.events.functions import fire_event
Expand All @@ -29,14 +27,6 @@ def _get_macro_parts(self):

return package_name, macro_name

def _get_kwargs(self) -> Dict[str, Any]:
# N.B. parse_cli_vars is embedded into the param when using click.
# replace this with:
# return self.args.args
# when this task is refactored for click
# or remove the function completely as it's basically a noop
return parse_cli_vars(self.args.args)

def compile_manifest(self) -> None:
if self.manifest is None:
raise InternalException("manifest was None in compile_manifest")
Expand All @@ -45,7 +35,7 @@ def _run_unsafe(self) -> agate.Table:
adapter = get_adapter(self.config)

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

with adapter.connection_named("macro_{}".format(macro_name)):
adapter.clear_transaction()
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def run_test(commands):
def test_unhidden_params_have_help_texts(self):
def run_test(command):
for param in command.params:
if not param.hidden:
# arguments can't have help text
if not isinstance(param, click.Argument) and not param.hidden:
assert param.help is not None
if type(command) is click.Group:
for command in command.commands.values():
Expand Down

0 comments on commit 11c6222

Please sign in to comment.