首页 > 解决方案 > DBT 向雪花列添加注释

问题描述

我们在雪花中将 DBT 用于 ELT。想要为 Snowflake 中的每一列添加评论。每次完全刷新后使用 COMMENT 或 ALTER 命令。

决定添加带有命令的宏,并在 on-run-end 挂钩下调用它。

{​​​​​​​% macro comment_transactions_master() %}​​​​​​​

    {% if execute %}
        (COMMENT ON COLUMN 
        "DEV_SCHEMA"."DBT_TEMP"."TR_MASTER"."TR_ID" IS 'testing comment';​​​​​​​)
    {% endif %}

{​​​​​​​% endmacro %}​​​​​​​

由于有 100 多列并且我是 DBT 的新手,有没有更好的方法来执行此操作?

标签: snowflake-cloud-data-platformsnowflake-schemadbt

解决方案


我不知道雪花,但我知道在其他数据库中,您可以向表中的多个列添加注释,如下所示:

comment on column schema.table (
   a is 'just a comment',
   b is 'just another comment'
)

所以为此你可以使用这个宏:

{% macro snowflake__alter_column_comment(relation, column_dict) %}

    COMMENT on COLUMN {{ relation }} (
      {% for column_name in column_dict %}
        {% set comment = column_dict[column_name]['description'] %}
        {{ column_name }} is '{{ comment }}'{%- if not loop.last %}, {% endif -%}
      {% endfor %}
    )
  
{% endmacro %}

并将其添加到雪花persist_docs宏中:

{% macro snowflake__persist_docs(relation, model, for_relation, for_columns) -%}
  {# -- Override the persist_docs default behaviour to add the short descriptions --#}

.........


{# Add the new macro here #}
  {% if for_columns and config.persist_column_docs() and model.columns %}
    {% do run_query(alter_column_comment(relation, model.columns)) %}
  {% endif %}

{% endmacro %}

Persist_docs几乎在每个物化中,所以你应该没问题。让我知道这是否有帮助。


推荐阅读