Created
August 1, 2024 03:55
-
-
Save adinsmoor/7918f029a4bf390d771b523a5c694cab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ config( | |
tags=["finance"] | |
) }} | |
{% macro centralize_test_failures(results) %} | |
{# --add "{{ centralize_test_failures(results) }}" to an on-run-end: block in dbt_project.yml #} | |
{# --run with dbt build --store-failures. #} | |
{%- set test_results = [] -%} | |
{%- for result in results -%} | |
{%- if result.node.resource_type == 'test' and result.status != 'skipped' and ( | |
result.node.config.get('store_failures') or flags.STORE_FAILURES | |
) | |
-%} | |
{%- do test_results.append(result) -%} | |
{%- endif -%} | |
{%- endfor -%} | |
{%- set central_tbl -%} {{ target.schema }}.test_failure_central {%- endset -%} | |
{%- set history_tbl -%} {{ target.schema }}.test_failure_history {%- endset -%} | |
{{ log("Centralizing test failures in " + central_tbl, info = true) if execute }} | |
create or replace table {{ central_tbl }} as ( | |
{% for result in test_results %} | |
select | |
'{{ result.node.name }}' as test_name, | |
'{{ result.node.unique_id }}' as model_name, | |
object_construct_keep_null(*) as test_failures_json, | |
current_timestamp as _timestamp | |
from {{ result.node.relation_name }} | |
{{ "union all" if not loop.last }} | |
{% endfor %} | |
); | |
-- only run centralization in higher environments | |
{% if target.name != 'default' %} | |
create table if not exists {{ history_tbl }} as ( | |
select | |
{{ dbt_utils.surrogate_key(["test_name", "test_failures_json", "_timestamp"]) }} as sk_id, | |
* | |
from {{ central_tbl }} | |
where false | |
); | |
insert into {{ history_tbl }} | |
select | |
{{ dbt_utils.surrogate_key(["test_name", "test_failures_json", "_timestamp"]) }} as sk_id, | |
* | |
from {{ central_tbl }} | |
; | |
{% endif %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment