Skip to content

Commit a1f1d4d

Browse files
authored
Merge pull request #38 from oracle/feature/snapshot_config
Bugfix for invalidate_hard_deletes=True directive used in dbt snapshots
2 parents 26f7631 + d42e8c1 commit a1f1d4d

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ adbs_local_env_test: wheel clean_venv
3737
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt snapshot --profiles-dir ./
3838
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt snapshot --profiles-dir ./
3939
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt docs generate --profiles-dir ./
40+
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt run-operation drop_schema --args 'relation: ${DBT_ORACLE_SCHEMA}' --profiles-dir ./
4041
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt clean --profiles-dir ./
4142

4243

@@ -57,4 +58,5 @@ adbs_pypi_test: clean_venv
5758
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt snapshot --profiles-dir ./
5859
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt snapshot --profiles-dir ./
5960
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt docs generate --profiles-dir ./
61+
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt run-operation drop_schema --args 'relation: ${DBT_ORACLE_SCHEMA}' --profiles-dir ./
6062
cd dbt_adbs_test_project && ${VENV_DIR}/bin/dbt clean --profiles-dir ./

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
snapshotted_data.dbt_scd_id
142142

143143
from snapshotted_data
144-
left join deletes_source_data as source_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
144+
left join deletes_source_data source_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
145145
where source_data.dbt_unique_key is null
146146
)
147147
{%- endif %}

dbt_adbs_test_project/snapshots/promotion_costs.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
strategy='check',
1919
unique_key='promo_id',
2020
check_cols='all',
21+
invalidate_hard_deletes=True
2122
)
2223
}}
2324
select * from {{ ref('promotion_costs') }}

tests/functional/adapter/snapshots/__init__.py

Whitespace-only changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
Copyright (c) 2022, Oracle and/or its affiliates.
3+
Copyright (c) 2020, Vitor Avancini
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
18+
import pytest
19+
from pathlib import Path
20+
21+
from dbt.tests.util import run_dbt
22+
23+
# seeds/my_seed.csv
24+
my_seed_csv = """
25+
id,name,some_date
26+
1,Easton,1981-05-20T06:46:51
27+
2,Lillian,1978-09-03T18:10:33
28+
3,Jeremiah,1982-03-11T03:59:51
29+
4,Nolan,1976-05-06T20:21:35
30+
""".lstrip()
31+
32+
33+
cc_all_snapshot_sql = """
34+
{% snapshot cc_all_snapshot %}
35+
{{ config(
36+
check_cols='all',
37+
unique_key='id',
38+
strategy='check',
39+
target_database=database,
40+
target_schema=schema,
41+
invalidate_hard_deletes=True
42+
) }}
43+
SELECT * FROM {{ ref('seed') }}
44+
{% endsnapshot %}
45+
""".strip()
46+
47+
# seeds/insert.sql
48+
seeds__insert_sql = """
49+
INSERT ALL
50+
INTO {schema}.seed (id, name, some_date) VALUES
51+
(5, 'John Doe', TO_DATE('1982-02-03', 'YYYY-MM-DD'))
52+
SELECT * FROM dual
53+
"""
54+
55+
# seeds/update.sql
56+
seeds__update_sql = """
57+
UPDATE {schema}.seed
58+
SET name = 'Lord Easton'
59+
WHERE id = 1
60+
"""
61+
62+
# seeds/delete.sql
63+
seeds__delete_sql = """
64+
DELETE FROM {schema}.seed WHERE id = 2
65+
"""
66+
67+
68+
class TestSnapshotCheckInvalidateHardDeletes:
69+
70+
@pytest.fixture(scope="class")
71+
def seeds(self):
72+
return {
73+
"seed.csv": my_seed_csv,
74+
"insert.sql": seeds__insert_sql,
75+
"update.sql": seeds__update_sql,
76+
"delete.sql": seeds__delete_sql
77+
78+
}
79+
80+
@pytest.fixture(scope="class")
81+
def snapshots(self):
82+
return {
83+
"cc_all_snapshot.sql": cc_all_snapshot_sql,
84+
}
85+
86+
def test_run_dbt(self, project):
87+
"""dbt seed
88+
dbt snapshot
89+
Perform insert/update/delete
90+
dbt snapshot
91+
92+
MERGE INTO dbt_test.cc_all_snapshot d
93+
USING o$pt_cc_all_snapshot182811 s
94+
ON (s.dbt_scd_id = d.dbt_scd_id)
95+
WHEN MATCHED
96+
THEN UPDATE
97+
SET dbt_valid_to = s.dbt_valid_to
98+
WHERE d.dbt_valid_to IS NULL
99+
AND s.dbt_change_type IN ('update', 'delete')
100+
WHEN NOT MATCHED
101+
THEN INSERT (d.id, d.name, d.some_date, d.dbt_updated_at, d.dbt_valid_from, d.dbt_valid_to, d.dbt_scd_id)
102+
VALUES (s.id, s.name, s.some_date, s.dbt_updated_at, s.dbt_valid_from, s.dbt_valid_to, s.dbt_scd_id)
103+
WHERE s.dbt_change_type = 'insert'
104+
105+
"""
106+
results = run_dbt(['seed'])
107+
assert len(results) == 1
108+
109+
# snapshot command
110+
results = run_dbt(["snapshot"])
111+
for result in results:
112+
assert result.status == "success"
113+
114+
project.run_sql_file(Path("seeds") / Path("insert.sql"))
115+
project.run_sql_file(Path("seeds") / Path("update.sql"))
116+
project.run_sql_file(Path("seeds") / Path("delete.sql"))
117+
118+
# run snapshot command
119+
results = run_dbt(["snapshot"])
120+
for result in results:
121+
assert result.status == "success"
122+
123+
snapshot_of_updated_rows = project.run_sql(f"select * from cc_all_snapshot where id=1", fetch="all")
124+
assert len(snapshot_of_updated_rows) == 2
125+
126+
# Deleted record will be invalidated. r['dbt_valid_to'] is set to current timestamp.
127+
snapshot_of_deleted_rows = project.run_sql(f"select * from cc_all_snapshot where id=2", fetch="all")
128+
assert len(snapshot_of_deleted_rows) == 1
129+
130+
131+

0 commit comments

Comments
 (0)