Skip to content

Commit 89e3595

Browse files
authored
Merge branch 'main' into bug_179_sample_test_data
2 parents a4bb867 + 7a0cccc commit 89e3595

File tree

1 file changed

+33
-2
lines changed
  • dbt/include/oracle/macros/materializations/snapshot

1 file changed

+33
-2
lines changed

dbt/include/oracle/macros/materializations/snapshot/snapshot.sql

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@
119119
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
120120
where {{ unique_key_is_null(strategy.unique_key, "snapshotted_data") }}
121121
or ({{ unique_key_is_not_null(strategy.unique_key, "snapshotted_data") }} and ({{ strategy.row_changed }})
122-
122+
{%- if strategy.hard_deletes == 'new_record' -%}
123+
or ({{ unique_key_is_not_null(strategy.unique_key, "snapshotted_data") }} and snapshotted_data.{{ columns.dbt_is_deleted }} = 'True')
124+
{%- endif %}
123125
)
124126

125127
),
@@ -140,6 +142,9 @@
140142
where (
141143
{{ strategy.row_changed }}
142144
)
145+
{%- if strategy.hard_deletes == 'new_record' -%}
146+
or snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
147+
{%- endif %}
143148
)
144149

145150
{%- if strategy.hard_deletes == 'invalidate' or strategy.hard_deletes == 'new_record' -%}
@@ -162,6 +167,21 @@
162167
left join deletes_source_data source_data
163168
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
164169
where {{ unique_key_is_null(strategy.unique_key, "source_data") }}
170+
{%- if strategy.hard_deletes == 'new_record' %}
171+
and not (
172+
--avoid updating the record's valid_to if the latest entry is marked as deleted
173+
snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
174+
and
175+
{% if config.get('dbt_valid_to_current') %}
176+
{% set source_unique_key = columns.dbt_valid_to | trim %}
177+
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}
178+
( {{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null )
179+
{% else %}
180+
{{ columns.dbt_valid_to }} is null
181+
{% endif %}
182+
)
183+
{%- endif %}
184+
165185
)
166186
{%- endif %}
167187

@@ -191,7 +211,18 @@
191211
left join deletes_source_data source_data
192212
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
193213
where {{ unique_key_is_null(strategy.unique_key, "source_data") }}
194-
214+
and not (
215+
--avoid inserting a new record if the latest one is marked as deleted
216+
snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
217+
and
218+
{% if config.get('dbt_valid_to_current') %}
219+
{% set source_unique_key = columns.dbt_valid_to | trim %}
220+
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}
221+
( {{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null )
222+
{% else %}
223+
{{ columns.dbt_valid_to }} is null
224+
{% endif %}
225+
)
195226
)
196227
{%- endif %}
197228

0 commit comments

Comments
 (0)