From d69a12f27a62a6ccc0366b2f9e7155938337b384 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Fri, 20 Sep 2024 16:33:06 -0400 Subject: [PATCH] Allow configuring snapshot table column names (#145) --- .changes/unreleased/Features-20240903-160221.yaml | 6 ++++++ .../postgres/macros/materializations/snapshot_merge.sql | 8 +++++--- pyproject.toml | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 .changes/unreleased/Features-20240903-160221.yaml diff --git a/.changes/unreleased/Features-20240903-160221.yaml b/.changes/unreleased/Features-20240903-160221.yaml new file mode 100644 index 00000000..8409e248 --- /dev/null +++ b/.changes/unreleased/Features-20240903-160221.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Allow configuring snapshot column names +time: 2024-09-03T16:02:21.069085-04:00 +custom: + Author: gshank + Issue: "144" diff --git a/dbt/include/postgres/macros/materializations/snapshot_merge.sql b/dbt/include/postgres/macros/materializations/snapshot_merge.sql index 807c70b6..894ea4b4 100644 --- a/dbt/include/postgres/macros/materializations/snapshot_merge.sql +++ b/dbt/include/postgres/macros/materializations/snapshot_merge.sql @@ -2,12 +2,14 @@ {% macro postgres__snapshot_merge_sql(target, source, insert_cols) -%} {%- set insert_cols_csv = insert_cols | join(', ') -%} + {%- set columns = config.get("snapshot_table_column_names") or get_snapshot_table_column_names() -%} + update {{ target }} - set dbt_valid_to = DBT_INTERNAL_SOURCE.dbt_valid_to + set {{ columns.dbt_valid_to }} = DBT_INTERNAL_SOURCE.{{ columns.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.{{ 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 }}.dbt_valid_to is null; + and {{ target }}.{{ columns.dbt_valid_to }} is null; insert into {{ target }} ({{ insert_cols_csv }}) select {% for column in insert_cols -%} diff --git a/pyproject.toml b/pyproject.toml index e6848a4f..726dc64a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ classifiers = [ ] dependencies = [ "psycopg2-binary>=2.9,<3.0", - "dbt-adapters>=1.1.1,<2.0", + "dbt-adapters>=1.7.0,<2.0", # add dbt-core to ensure backwards compatibility of installation, this is not a functional dependency "dbt-core>=1.8.0", # installed via dbt-adapters but used directly