DBT 向雪花列添加注释

Kav*_*ree 3 snowflake-schema snowflake-cloud-data-platform dbt

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

\n

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

\n
{\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b% macro comment_transactions_master() %}\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\n\n    {% if execute %}\n        (COMMENT ON COLUMN \n        "DEV_SCHEMA"."DBT_TEMP"."TR_MASTER"."TR_ID" IS 'testing comment';\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b)\n    {% endif %}\n\n{\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b% endmacro %}\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

Kay*_*Kay 5

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

comment on column schema.table (
   a is 'just a comment',
   b is 'just another comment'
)
Run Code Online (Sandbox Code Playgroud)

因此,您可以使用这个宏:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

并将其添加到雪花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 %}
Run Code Online (Sandbox Code Playgroud)

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