dbt 1.4 onwards supports the
--warn-error-options
flag orDBT_WARN_ERROR_OPTIONS
env var where you can now raise an error duringNoNodesForSelectionCriteria
so this should no longer be required.
- Add the following three macros to your project:
{% macro pre_run() %}
{% set query %}
create or replace table {{ target.database }}.{{ target.schema }}.dbt_meta__selection_checker (
run_id int,
models_run int
);
insert into {{ target.database }}.{{ target.schema }}.dbt_meta__selection_checker values (
{{ env_var('DBT_CLOUD_RUN_ID') }},
0
);
{% endset %}
{% do run_query(query) %}
{% endmacro %}
{% macro post_run() %}
{% set query %}
select models_run
from {{ target.database }}.{{ target.schema }}.dbt_meta__selection_checker
where run_id = {{ env_var('DBT_CLOUD_RUN_ID') }};
{% endset %}
{% set results = run_query(query) %}
{% set results_list = results.columns[0].values()[0] %}
{% if results_list == 0 %}
{{ exceptions.raise_compiler_error("Something went wrong with your selector - no models were included in the run.") }}
{% else %}
{{ log(results_list ~ " model(s) ran successfully.", info=True) }}
{% endif %}
{% endmacro %}
{% macro log_results(results) %}
{% if execute %}
{% set models_ran = results | length %}
{% set query %}
update {{ target.database }}.{{ target.schema }}.dbt_meta__selection_checker
set models_run = {{ models_ran }}
where run_id = {{ env_var('DBT_CLOUD_RUN_ID') }};
{% endset %}
{% do run_query(query) %}
{% endif %}
{% endmacro %}
- Make sure you have an
on-run-end
hook to update thedbt_meta__selection_checker
table (which will not run if no models were in our selection - this is the main reason for the two otherrun-operation
macros):
# dbt_project.yml
...
on-run-end:
- "{{ log_results(results) }}"
- Then in your dbt Cloud job, make sure to have a
dbt run-operation
step before and after yourdbt run
step that executes the appropriate macro (pre_run
andpost_run
). For example:
dbt run-operation pre_run
dbt run --select my_model
dbt run-operation post_run
If your selector was correct and models were included in the run, your run should have a successful state:
But if your selector had a typo and no models were included in the run, your run should have an error state:
If you run locally with dbt CLI, you can simulate this by exporting a fake DBT_CLOUD_RUN_ID
environment variable (dbt Cloud jobs on the other hand will generate this environment variable for you):
export DBT_CLOUD_RUN_ID=123
dbt run-operation pre_run
dbt run --select this_doesnt_exist
dbt run-operation post_run