您能否将 dbt 模型配置块仅应用于一种类型的数据库(例如 postgresql)?

Can you have a dbt model config block apply to only one type of database (e.g. postgresql)?

在我的 dbt 模型定义中,我想要 运行 一个配置块,但前提是数据库类型是 postgresql。有没有办法做到这一点?下面的示例模型:

{{
  config(
    post_hook='create index if not exists "{{ this.name }}__idx_on_key" on {{ this }} ("key")'
  )
}}

select * from {{ ref('stg_customer') }}

{{ target.type }} 将编译为您正在使用的适配器的名称。参见 docs

您可以将钩子包裹在 {% if target.type == 'postgres' %}...{% endif %} 块中。这可能会变得笨拙,所以我建议创建一个接受关系作为参数的宏:

{% macro create_index_if_pg(relation, field) %}
{% if target.type == 'postgres' %}
create index if not exists "{{ relation.name }}__idx_on_{{ field }}" on {{ relation }} ("{{ field }}")
{% endif %}
{% endmacro %}

然后从 post-hook:

调用宏
{{
  config(
    post_hook='{{ create_index_if_pg(this, "key") }}'
  )
}}