From 13e5655477610f9aa002aa4b8a0c7cc4af6a9cfe Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Tue, 27 Aug 2024 21:54:32 -0400 Subject: [PATCH] Update snapshot_merge.sql --- .../postgres/macros/materializations/snapshot_merge.sql | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dbt/include/postgres/macros/materializations/snapshot_merge.sql b/dbt/include/postgres/macros/materializations/snapshot_merge.sql index 807c70b6..6db5d458 100644 --- a/dbt/include/postgres/macros/materializations/snapshot_merge.sql +++ b/dbt/include/postgres/macros/materializations/snapshot_merge.sql @@ -2,12 +2,15 @@ {% macro postgres__snapshot_merge_sql(target, source, insert_cols) -%} {%- set insert_cols_csv = insert_cols | join(', ') -%} + {%- set dbt_scd_id = config.get("dbt_scd_id_column_name") or "dbt_scd_id" -%} + {%- set dbt_valid_to = config.get("dbt_valid_to_column_name") or "dbt_valid_to" -%} + update {{ target }} - set dbt_valid_to = DBT_INTERNAL_SOURCE.dbt_valid_to + set {{ dbt_valid_to }} = DBT_INTERNAL_SOURCE.{{ dbt_valid_to }} from {{ source }} as DBT_INTERNAL_SOURCE - where DBT_INTERNAL_SOURCE.dbt_scd_id::text = {{ target }}.dbt_scd_id::text + where DBT_INTERNAL_SOURCE.{{ dbt_scd_id }}::text = {{ target }}.{{ dbt_scd_id }}::text and DBT_INTERNAL_SOURCE.dbt_change_type::text in ('update'::text, 'delete'::text) - and {{ target }}.dbt_valid_to is null; + and {{ target }}.{{ dbt_valid_to }} is null; insert into {{ target }} ({{ insert_cols_csv }}) select {% for column in insert_cols -%}