Skip to content

Commit

Permalink
Specify dbt-common >= 1.11.0, create unique_key_fields macro
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Oct 17, 2024
1 parent d88a42a commit 095c9f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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') %}
Expand All @@ -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) }},
Expand All @@ -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 }}
Expand All @@ -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 %}
Expand Down Expand Up @@ -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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down Expand Up @@ -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) }}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 095c9f9

Please sign in to comment.