diff --git a/elementary/monitor/dbt_project/dbt_project.yml b/elementary/monitor/dbt_project/dbt_project.yml index 3430c0e02..1713f19b8 100644 --- a/elementary/monitor/dbt_project/dbt_project.yml +++ b/elementary/monitor/dbt_project/dbt_project.yml @@ -38,3 +38,5 @@ quoting: # as tables. These settings can be overridden in the individual model files # using the `{{ config(...) }}` macro. models: + # Required in order to apply table schema changes in the table materialization. + +on_schema_change: "append_new_columns" diff --git a/elementary/monitor/dbt_project/macros/materializations/table.sql b/elementary/monitor/dbt_project/macros/materializations/table.sql index f98d9c957..fafca1bd2 100644 --- a/elementary/monitor/dbt_project/macros/materializations/table.sql +++ b/elementary/monitor/dbt_project/macros/materializations/table.sql @@ -4,23 +4,17 @@ {{ return(dbt.materialization_table_default()) }} {% endif %} - {% set existing_relation = load_cached_relation(this) %} - {% if not existing_relation %} - {% set init_schema_sql %} - with result as ( - {{ sql }} - ) - select * from result where 1 = 0 - {% endset %} - {% do context.update({"sql": init_schema_sql}) %} - {# This is meant to deal with a race between initializing the table and loading data into it. #} - {% else %} - {% set same_sql %} - select * from {{ this }} - {% endset %} - {% do context.update({"sql": same_sql}) %} - {% endif %} - - {% do context.update({"pre_hooks": [], "post_hooks": []}) %} - {{ return(dbt.materialization_table_default()) }} + {% set init_schema_sql %} + with result as ( + {{ sql }} + ) + select * from result where 1 = 0 + {% endset %} + {% do context.update({ + "sql": init_schema_sql, + "pre_hooks": [], + "post_hooks": [], + }) %} + {# We call the incremental materialization in order to hand-off the implementation of schema changes to dbt. #} + {{ return(dbt.materialization_incremental_default()) }} {% endmaterialization %}