Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle hard deletes in snapshots #2355

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@
select
'update' as dbt_change_type,
snapshotted_data.dbt_scd_id,
source_data.dbt_valid_from as dbt_valid_to
coalesce(source_data.dbt_valid_from, {{ snapshot_get_time() }}) as dbt_valid_to

from source_data
join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
from snapshotted_data
left join source_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
where snapshotted_data.dbt_valid_to is null
and (
{{ strategy.row_changed }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
{% set updated_at = config['updated_at'] %}

{% set row_changed_expr -%}
({{ snapshotted_rel }}.{{ updated_at }} < {{ current_rel }}.{{ updated_at }})
({{ snapshotted_rel }}.{{ updated_at }} < {{ current_rel }}.{{ updated_at }}
-- this indicates that the source record was hard-deleted
or {{ current_rel }}.{{ primary_key }} is null
)
{%- endset %}

{% set scd_id_expr = snapshot_hash_arguments([primary_key, updated_at]) %}
Expand Down Expand Up @@ -148,12 +151,16 @@
{%- if column_added -%}
TRUE
{%- else -%}
{%- for col in check_cols -%}
-- this indicates that the source record was hard-deleted
{{ current_rel }}.{{ primary_key }} is null) or
{% for col in check_cols -%}
{{ snapshotted_rel }}.{{ col }} != {{ current_rel }}.{{ col }}
or
({{ snapshotted_rel }}.{{ col }} is null) != ({{ current_rel }}.{{ col }} is null)
{%- if not loop.last %} or {% endif -%}
{%- endfor -%}


{%- endif -%}
)
{%- endset %}
Expand Down