Skip to content

Commit

Permalink
Merge pull request elementary-data#964 from elementary-data/ele-937-t…
Browse files Browse the repository at this point in the history
…able-materialization-in-sync-doesnt-do-db-migrations

Schema changes are applied to table materialized models.
  • Loading branch information
elongl authored Jun 26, 2023
2 parents 8332a9d + 9e502fa commit 830ebfd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 2 additions & 0 deletions elementary/monitor/dbt_project/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
32 changes: 13 additions & 19 deletions elementary/monitor/dbt_project/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

0 comments on commit 830ebfd

Please sign in to comment.