dbt预挂钩无法呈现/查找宏吗?

问题描述

dbt_project.yml模型预钩未能呈现宏。以下实现:

# gold/dbt_project.yml

config-version: 2

...

models:
    gold:
        +pre-hooks: "{{ incremental_failsafe() }}"

以及以下宏:

-- gold/macros/utils/incremental_failsafe.sql

{% macro incremental_failsafe() %}

{# /*
Used to wrap AND clauses that should only be run on dev/CI dbt runs.

For example,to limit the number of records scanned on an incremental
table. This macro will return True unless the target profile is 'prod':

`dbt run --target prod`
*/ #}

{% if target.name == "prod" %}
    {% set incremental_failsafe = False %}
{% else %}
    {% set incremental_failsafe = True %}
{% endif %}

{{ log("Running with incremental_failsafe: " ~ incremental_failsafe,info=True) }}
{{ return(incremental_failsafe) }}

{% endmacro %}

结果如下:

(sNowflake) Teghans-MacBook-Pro:gold tnightengale$ dbt compile
Running with dbt=0.17.2
Encountered an error:
Compilation Error
  Could not render {{ incremental_failsafe() }}: 'incremental_failsafe' is undefined

我知道在this slack exchange中概述的预钩周围存在dbt 0.17.0的问题。尽管我运行的是0.17.2(而不是0.17.0),但我还是尝试按照该会话中的建议在yml中同时使用+pre-hook:pre-hook:规范。任何见识都将受到欢迎!

编辑:解决方

说实话,答案是如此愚蠢和明显:错误是由+pre-hooks:而非+pre-hook:引起的。但是,建议的答案使我重新考虑了一下。我将其标记为正确,因为它通常在钩子上提供很多有用的上下文。干杯!

解决方法

第一印象-也许您混淆了用于特定模型的模型预钩和应该放置 run 预钩的地方?

编辑:在上面纠正了我自己-Asker尝试执行模型配置预挂钩,而不是运行预挂钩。我现在认为,您需要将预钩移到与名称空间配置相同的优先级别。见下文:

dbt_project.yml

# A run pre-hook that applies to all runs would go here:
on-run-start:
- "{{ example_pre_run_macro() }}"


models:
    # a pre-hooks to all models goes here *before* but equivalent depth to the namespace:
    +pre-hook: "{{ incremental_failsafe() }}"
 
    namespace:
        # Below configures models found in models/events/
        events:  
          +enabled: true
          +materialized: view
          # a pre-hook to a single model / model directory goes here:
          +pre-hook: "{{ example_pre_model_macro() }}"

如何通过GitLab的dbt存储库建立一个非常好的示例:Gitlab dbt_project.yml

TLDR:如此设置dbt_project.yml

# gold/dbt_project.yml

config-version: 2

...

models:
    +pre-hooks: "{{ incremental_failsafe() }}"
    gold:
        <models config here>