Skip to content

Commit

Permalink
Merge pull request gocardless#47 from fenimore/dbt-clean-operator
Browse files Browse the repository at this point in the history
Add DbtCleanOperator
  • Loading branch information
andrewrjones authored Oct 25, 2021
2 parents 04f660a + 2e3b6da commit 6b8af6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ from airflow_dbt.operators.dbt_operator import (
DbtSeedOperator,
DbtSnapshotOperator,
DbtRunOperator,
DbtTestOperator
DbtTestOperator,
DbtCleanOperator,
)
from airflow.utils.dates import days_ago

Expand Down Expand Up @@ -36,7 +37,11 @@ with DAG(dag_id='dbt', default_args=default_args, schedule_interval='@daily') as
retries=0, # Failing tests would fail the task, and we don't want Airflow to try again
)

dbt_seed >> dbt_snapshot >> dbt_run >> dbt_test
dbt_clean = DbtCleanOperator(
task_id='dbt_clean,
)

dbt_seed >> dbt_snapshot >> dbt_run >> dbt_test >> dbt_clean
```

## Installation
Expand Down Expand Up @@ -65,6 +70,8 @@ There are five operators currently implemented:
* Calls [`dbt run`](https://docs.getdbt.com/docs/run)
* `DbtTestOperator`
* Calls [`dbt test`](https://docs.getdbt.com/docs/test)
* `DbtCleanOperator`
* Calls [`dbt clean`](https://docs.getdbt.com/docs/clean)


Each of the above operators accept the following arguments:
Expand Down
9 changes: 9 additions & 0 deletions airflow_dbt/operators/dbt_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ def __init__(self, profiles_dir=None, target=None, *args, **kwargs):

def execute(self, context):
self.create_hook().run_cli('deps')


class DbtCleanOperator(DbtBaseOperator):
@apply_defaults
def __init__(self, profiles_dir=None, target=None, *args, **kwargs):
super(DbtCleanOperator, self).__init__(profiles_dir=profiles_dir, target=target, *args, **kwargs)

def execute(self, context):
self.create_hook().run_cli('clean')
12 changes: 11 additions & 1 deletion tests/operators/test_dbt_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
DbtSnapshotOperator,
DbtRunOperator,
DbtTestOperator,
DbtDepsOperator
DbtDepsOperator,
DbtCleanOperator,
)


Expand Down Expand Up @@ -64,3 +65,12 @@ def test_dbt_deps(self, mock_run_cli):
)
operator.execute(None)
mock_run_cli.assert_called_once_with('deps')

@mock.patch.object(DbtCliHook, 'run_cli')
def test_dbt_clean(self, mock_run_cli):
operator = DbtCleanOperator(
task_id='clean',
dag=self.dag
)
operator.execute(None)
mock_run_cli.assert_called_once_with('clean')

0 comments on commit 6b8af6e

Please sign in to comment.