diff --git a/dbt/include/global_project/macros/materializations/snapshots/helpers.sql b/dbt/include/global_project/macros/materializations/snapshots/helpers.sql index 6710ed25..a8a2ff72 100644 --- a/dbt/include/global_project/macros/materializations/snapshots/helpers.sql +++ b/dbt/include/global_project/macros/materializations/snapshots/helpers.sql @@ -49,16 +49,7 @@ snapshotted_data as ( - select *, - {% if strategy.unique_key | is_list %} - {% for key in strategy.unique_key %} - {{ key }} as dbt_unique_key_{{ loop.index }} - {%- if not loop.last %} , {%- endif %} - {% endfor %} - {% else %} - {{ strategy.unique_key }} as dbt_unique_key - {% endif %} - + select *, {% unique_key_fields(strategy.unique_key) %} from {{ target_relation }} where {% if config.get('dbt_valid_to_current') %} @@ -72,15 +63,7 @@ insertions_source_data as ( - select - *, - {% if strategy.unique_key | is_list %} - {% for key in strategy.unique_key %} - {{ key }} as dbt_unique_key_{{ loop.index }}, - {% endfor %} - {% else %} - {{ strategy.unique_key }} as dbt_unique_key, - {% endif %} + select *, {% unique_key_fields(strategy.unique_key) %}, {{ strategy.updated_at }} as {{ columns.dbt_updated_at }}, {{ strategy.updated_at }} as {{ columns.dbt_valid_from }}, {{ get_dbt_valid_to_current(strategy, columns) }}, @@ -91,15 +74,7 @@ updates_source_data as ( - select - *, - {% if strategy.unique_key | is_list %} - {% for key in strategy.unique_key %} - {{ key }} as dbt_unique_key_{{ loop.index }}, - {% endfor %} - {% else %} - {{ strategy.unique_key }} as dbt_unique_key, - {% endif %} + select *, {% unique_key_fields(strategy.unique_key %}, {{ strategy.updated_at }} as {{ columns.dbt_updated_at }}, {{ strategy.updated_at }} as {{ columns.dbt_valid_from }}, {{ strategy.updated_at }} as {{ columns.dbt_valid_to }} @@ -111,16 +86,7 @@ deletes_source_data as ( - select - *, - {% if strategy.unique_key | is_list %} - {% for key in strategy.unique_key %} - {{ key }} as dbt_unique_key_{{ loop.index }} - {%- if not loop.last %} , {%- endif %} - {% endfor %} - {% else %} - {{ strategy.unique_key }} as dbt_unique_key - {% endif %} + select *, {% unique_key_fields(strategy.unique_key) %} from snapshot_query ), {% endif %} @@ -275,8 +241,21 @@ {% endif %} {% endmacro %} + {% macro get_dbt_valid_to_current(strategy, columns) %} {% set dbt_valid_to_current = config.get('dbt_valid_to_current') or "null" %} coalesce(nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}), {{dbt_valid_to_current}}) as {{ columns.dbt_valid_to }} {% endmacro %} + + +{% macro unique_key_fields(unique_key) %} + {% if unique_key | is_list %} + {% for key in unique_key %} + {{ key }} as dbt_unique_key_{{ loop.index }} + {%- if not loop.last %} , {%- endif %} + {% endfor %} + {% else %} + {{ unique_key }} as dbt_unique_key + {% endif %} +{% endmacro %} diff --git a/dbt/include/global_project/macros/materializations/snapshots/strategies.sql b/dbt/include/global_project/macros/materializations/snapshots/strategies.sql index 35d341b9..dba5886f 100644 --- a/dbt/include/global_project/macros/materializations/snapshots/strategies.sql +++ b/dbt/include/global_project/macros/materializations/snapshots/strategies.sql @@ -70,7 +70,7 @@ ({{ snapshotted_rel }}.{{ columns.dbt_valid_from }} < {{ current_rel }}.{{ updated_at }}) {%- endset %} - {% if primary_key is iterable and primary_key is not string and primary_key is not mapping %} + {% if primary_key | is_list %} {% set scd_args = [] %} {% for key in primary_key %} {{ scd_args.append(key) }} @@ -175,7 +175,7 @@ ) {%- endset %} - {% if primary_key is sequence and primary_key is not mapping and primary_key is not string %} + {% if primary_key | is_list %} {% set scd_args = [] %} {% for key in primary_key %} {{ scd_args.append(key) }} diff --git a/pyproject.toml b/pyproject.toml index 76ca3dee..70a19dd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ classifiers = [ "Programming Language :: Python :: 3.12", ] dependencies = [ - "dbt-common>=1.10,<2.0", + "dbt-common>=1.11,<2.0", "pytz>=2015.7", # installed via dbt-common but used directly "agate>=1.0,<2.0",