dbt_constraints
dbt_constraints copied to clipboard
On-run-end hook fails if model fails
We recently updated to 1.0 and it appeared that another package's on-run-end hooks do not work.
It appeared that dbt_constraints
on-run-end hook fails if any model with tests fails. Unfortunately DBT stops execution in case of on-run-end hook error.
How to reproduce:
- Add
debug
DBT profile (Snowflake in our case) -
dbt deps
-
dbt build
- Check logs and check log message:
on-run-end failed, error:
Full log:
12:38:21 Running with dbt=1.8.3
12:38:22 Registered adapter: snowflake=1.8.3
12:38:22 Unable to do partial parsing because saved manifest not found. Starting full parse.
12:38:24 [WARNING]: Deprecated functionality
The `tests` config has been renamed to `data_tests`. Please see
https://docs.getdbt.com/docs/build/data-tests#new-data_tests-syntax for more
information.
12:38:24 [WARNING]: Configuration paths exist in your dbt_project.yml file which do not apply to any resources.
There are 1 unused configuration paths:
- models.TFS_Debug.staging
12:38:25 Found 2 models, 1 operation, 2 data tests, 650 macros
12:38:25
12:38:27 Concurrency: 10 threads (target='denis')
12:38:27
12:38:27 1 of 4 START sql view model denis.stg_constraints .............................. [RUN]
12:38:27 2 of 4 START sql view model denis.stg_pass ..................................... [RUN]
12:38:29 1 of 4 OK created sql view model denis.stg_constraints ......................... [SUCCESS 1 in 1.36s]
12:38:29 2 of 4 OK created sql view model denis.stg_pass ................................ [SUCCESS 1 in 1.35s]
12:38:29 3 of 4 START test dbt_constraints_primary_key_stg_constraints_id ............... [RUN]
12:38:29 4 of 4 START test not_null_stg_pass_id ......................................... [RUN]
12:38:30 4 of 4 PASS not_null_stg_pass_id ............................................... [PASS in 1.50s]
12:38:30 3 of 4 ERROR dbt_constraints_primary_key_stg_constraints_id .................... [ERROR in 1.58s]
12:38:30
12:38:30 Running 1 on-run-end hook
12:38:30 Running dbt Constraints
12:38:30 Database error while running on-run-end
12:38:31
12:38:31 Finished running 2 view models, 2 data tests in 0 hours 0 minutes and 6.67 seconds (6.67s).
12:38:31
12:38:31 Completed with 2 errors and 0 warnings:
12:38:31
12:38:31 Database Error in test dbt_constraints_primary_key_stg_constraints_id (models\staging\stg_constraints.yml)
100051 (22012): Division by zero
compiled Code at target\run\Debug\models\staging\stg_constraints.yml\dbt_constraints_primary_key_stg_constraints_id.sql
12:38:31
12:38:31 on-run-end failed, error:
'>' not supported between instances of 'NoneType' and 'int'
12:38:31
12:38:31 Done. PASS=3 WARN=0 ERROR=2 SKIP=0 TOTAL=5
The problem is in lookup_should_rely
macro: it expects res.failures
to be numeric.
{#- This macro that checks if a test has results and whether there were errors -#}
{%- macro lookup_should_rely(test_model) -%}
{%- if test_model.config.where
or test_model.config.warn_if != "!= 0"
or test_model.config.fail_calc != "count(*)" -%}
{#- Set NORELY if there is a condition on the test -#}
{{ return('NORELY') }}
{%- endif -%}
{%- for res in results
if res.node.config.materialized == "test"
and res.node.unique_id == test_model.unique_id -%}
{%- if res.failures > 0 -%}
{#- Set NORELY if there is a test failure -#}
{{ return('NORELY') }}
{%- elif res.failures == 0 -%}
{#- Set RELY if there are 0 failures -#}
{{ return('RELY') }}
{%- endif -%}
{%- endfor -%}
{{ return('') }}
{%- endmacro -%}