Skip to content
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: 3 additions & 3 deletions cosmos/operators/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from cosmos.config import ProfileConfig
from cosmos.constants import AIRFLOW_VERSION, PRODUCER_WATCHER_TASK_ID, InvocationMode
from cosmos.operators.base import (
DbtBuildMixin,
DbtRunMixin,
DbtSeedMixin,
DbtSnapshotMixin,
Expand Down Expand Up @@ -70,7 +71,7 @@ def safe_xcom_push(task_instance: TaskInstance, key: str, value: Any) -> None:
task_instance.xcom_push(key=key, value=value)


class DbtProducerWatcherOperator(DbtLocalBaseOperator):
class DbtProducerWatcherOperator(DbtBuildMixin, DbtLocalBaseOperator):
"""Run dbt build and update XCom with the progress of each model, as part of the *WATCHER* execution mode.

Executes **one** ``dbt build`` covering the whole selection.
Expand All @@ -95,8 +96,7 @@ class DbtProducerWatcherOperator(DbtLocalBaseOperator):
feedback and granular task-level observability downstream.
"""

base_cmd = ["build"]
template_fields = DbtLocalBaseOperator.template_fields
template_fields = DbtLocalBaseOperator.template_fields + DbtBuildMixin.template_fields # type: ignore[operator]

def __init__(self, *args: Any, **kwargs: Any) -> None:
task_id = kwargs.pop("task_id", "dbt_producer_watcher_operator")
Expand Down
52 changes: 52 additions & 0 deletions tests/operators/test_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,55 @@ def test_dbt_task_group_with_watcher():
assert isinstance(dag_dbt_task_group_watcher.task_dict["dbt_task_group.orders_run"], DbtRunWatcherOperator)

assert dag_dbt_task_group_watcher.task_dict["dbt_task_group.dbt_producer_watcher"].downstream_task_ids == set()


@pytest.mark.skipif(AIRFLOW_VERSION < Version("2.7"), reason="Airflow did not have dag.test() until the 2.6 release")
@pytest.mark.integration
def test_dbt_task_group_with_watcher_has_correct_dbt_cmd():
"""
Create an Airflow DAG that uses a DbtTaskGroup with `ExecutionMode.WATCHER`.
Confirm that the dbt command flags include the expected flags.
"""
from airflow import DAG

from cosmos import DbtTaskGroup, ExecutionConfig
from cosmos.config import RenderConfig
from cosmos.constants import ExecutionMode, TestBehavior

context = {"ti": MagicMock(), "run_id": "test_run_id"}

operator_args = {
"install_deps": True, # install any necessary dependencies before running any dbt command
"execution_timeout": timedelta(seconds=120),
"full_refresh": True,
}

with DAG(
dag_id="example_watcher_taskgroup_flags",
start_date=datetime(2025, 1, 1),
) as dag_dbt_task_group_watcher_flags:
"""
The simplest example of using Cosmos to render a dbt project as a TaskGroup.
"""
DbtTaskGroup(
group_id="dbt_task_group",
execution_config=ExecutionConfig(
execution_mode=ExecutionMode.WATCHER,
),
profile_config=profile_config,
project_config=project_config,
render_config=RenderConfig(test_behavior=TestBehavior.NONE),
operator_args=operator_args,
)

producer_operator = dag_dbt_task_group_watcher_flags.task_dict["dbt_task_group.dbt_producer_watcher"]
assert producer_operator.base_cmd == ["build"]

cmd_flags = producer_operator.add_cmd_flags()

# Build the command without executing it
full_cmd, env = producer_operator.build_cmd(context=context, cmd_flags=cmd_flags)

# Verify the command was built correctly
assert full_cmd[1] == "build" # dbt build command
assert "--full-refresh" in full_cmd