From ed94c0d2e7a73b1b2b3d34643c71c02b3c4e1147 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Thu, 10 Oct 2024 17:00:38 -0400 Subject: [PATCH] Enable setting dbt_valid_to snapshot column to new setting dbt_valid_to_current (#152) Co-authored-by: Kshitij Aranke --- .changes/unreleased/Features-20240927-133708.yaml | 6 ++++++ .../postgres/macros/materializations/snapshot_merge.sql | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Features-20240927-133708.yaml diff --git a/.changes/unreleased/Features-20240927-133708.yaml b/.changes/unreleased/Features-20240927-133708.yaml new file mode 100644 index 00000000..b2dba333 --- /dev/null +++ b/.changes/unreleased/Features-20240927-133708.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Enable setting current value of dbt_valid_to +time: 2024-09-27T13:37:08.808843-04:00 +custom: + Author: gshank + Issue: "151" diff --git a/dbt/include/postgres/macros/materializations/snapshot_merge.sql b/dbt/include/postgres/macros/materializations/snapshot_merge.sql index 894ea4b4..0b4deb1b 100644 --- a/dbt/include/postgres/macros/materializations/snapshot_merge.sql +++ b/dbt/include/postgres/macros/materializations/snapshot_merge.sql @@ -9,7 +9,12 @@ from {{ source }} as DBT_INTERNAL_SOURCE where DBT_INTERNAL_SOURCE.{{ columns.dbt_scd_id }}::text = {{ target }}.{{ columns.dbt_scd_id }}::text and DBT_INTERNAL_SOURCE.dbt_change_type::text in ('update'::text, 'delete'::text) - and {{ target }}.{{ columns.dbt_valid_to }} is null; + {% if config.get("dbt_valid_to_current") %} + and ({{ target }}.{{ columns.dbt_valid_to }} = {{ config.get('dbt_valid_to_current') }} or {{ target }}.{{ columns.dbt_valid_to }} is null); + {% else %} + and {{ target }}.{{ columns.dbt_valid_to }} is null; + {% endif %} + insert into {{ target }} ({{ insert_cols_csv }}) select {% for column in insert_cols -%}