dbt on_configuration_change 简单说明

dbt on_configuration_change 目前主要是在处理物化视图中,on_configuration_change 包含了三类设置

参考设置

  • apply
    是默认参数,尝试进行更新存在的数据库对象
  • continue
    允许继续运行,但是提供一个警告
  • fail
    如果捕捉到变动就失败

参考使用

  • 比如模型上的
{{ config(
    materialized="<materialization_name>",
    on_configuration_change="apply" | "continue" | "fail"
) }}

materialized_view 实现中的处理

{% macro materialized_view_get_build_sql(existing_relation, target_relation, backup_relation, intermediate_relation) %}
 
    {% set full_refresh_mode = should_full_refresh() %}
 
    -- determine the scenario we're in: create, full_refresh, alter, refresh data
    {% if existing_relation is none %}
        {% set build_sql = get_create_materialized_view_as_sql(target_relation, sql) %}
    {% elif full_refresh_mode or not existing_relation.is_materialized_view %}
        {% set build_sql = get_replace_sql(existing_relation, target_relation, sql) %}
    {% else %}
 
        -- get config options
        {% set on_configuration_change = config.get('on_configuration_change') %}
        {% set configuration_changes = get_materialized_view_configuration_changes(existing_relation, config) %}
 
        {% if configuration_changes is none %}
            {% set build_sql = refresh_materialized_view(target_relation) %}
 
        {% elif on_configuration_change == 'apply' %}
            {% set build_sql = get_alter_materialized_view_as_sql(target_relation, configuration_changes, sql, existing_relation, backup_relation, intermediate_relation) %}
        {% elif on_configuration_change == 'continue' %}
            {% set build_sql = '' %}
            {{ exceptions.warn("Configuration changes were identified and `on_configuration_change` was set to `continue` for `" ~ target_relation ~ "`") }}
        {% elif on_configuration_change == 'fail' %}
            {{ exceptions.raise_fail_fast_error("Configuration changes were identified and `on_configuration_change` was set to `fail` for `" ~ target_relation ~ "`") }}
 
        {% else %}
            -- this only happens if the user provides a value other than `apply`, 'skip', 'fail'
            {{ exceptions.raise_compiler_error("Unexpected configuration scenario") }}
 
        {% endif %}
 
    {% endif %}
 
    {% do return(build_sql) %}
 
{% endmacro %}

说明

模型变动是比较正常的事情,了解on_configuration_change 的一个使用以及配置是比较有用的

参考资料

dbt/include/global_project/macros/materializations/models/materialized_view.sql
https://docs.getdbt.com/reference/resource-configs/on_configuration_change

posted on 2024-05-08 11:57  荣锋亮  阅读(13)  评论(0编辑  收藏  举报

导航