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

Support dbt global flags (via dbt_cmd_global_flags in operator_args) #469

Merged
merged 1 commit into from
Aug 16, 2023
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
5 changes: 5 additions & 0 deletions cosmos/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DbtBaseOperator(BaseOperator):
:param dbt_executable_path: Path to dbt executable can be used with venv
(i.e. /home/astro/.pyenv/versions/dbt_venv/bin/dbt)
:param dbt_cmd_flags: List of flags to pass to dbt command
:param dbt_cmd_global_flags: List of dbt global flags to be passed to the dbt command
"""

template_fields: Sequence[str] = ("env", "vars")
Expand Down Expand Up @@ -100,6 +101,7 @@ def __init__(
cancel_query_on_kill: bool = True,
dbt_executable_path: str = "dbt",
dbt_cmd_flags: list[str] | None = None,
dbt_cmd_global_flags: list[str] | None = None,
**kwargs: Any,
) -> None:
self.project_dir = project_dir
Expand Down Expand Up @@ -132,6 +134,7 @@ def __init__(
else:
self.dbt_executable_path = dbt_executable_path
self.dbt_cmd_flags = dbt_cmd_flags
self.dbt_cmd_global_flags = dbt_cmd_global_flags or []
super().__init__(**kwargs)

def get_env(self, context: Context) -> dict[str, str | bytes | os.PathLike[Any]]:
Expand Down Expand Up @@ -210,6 +213,8 @@ def build_cmd(
) -> Tuple[list[str | None], dict[str, str | bytes | os.PathLike[Any]]]:
dbt_cmd = [self.dbt_executable_path]

dbt_cmd.extend(self.dbt_cmd_global_flags)

if self.base_cmd:
dbt_cmd.extend(self.base_cmd)

Expand Down
19 changes: 18 additions & 1 deletion tests/operators/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,24 @@ def test_dbt_base_operator_add_user_supplied_flags() -> None:
cmd, _ = dbt_base_operator.build_cmd(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
assert "--full-refresh" in cmd
assert cmd[-2] == "run"
assert cmd[-1] == "--full-refresh"


def test_dbt_base_operator_add_user_supplied_global_flags() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
base_cmd=["run"],
dbt_cmd_global_flags=["--cache-selected-only"],
)

cmd, _ = dbt_base_operator.build_cmd(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
assert cmd[-2] == "--cache-selected-only"
assert cmd[-1] == "run"


@pytest.mark.parametrize(
Expand Down