Skip to content

Commit d0c0b5b

Browse files
committed
Added a macro to update dbt_scd_id
1 parent 97407c8 commit d0c0b5b

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
{{ strategy.scd_id }} as dbt_scd_id,
165165
{{ strategy.updated_at }} as dbt_updated_at,
166166
{{ strategy.updated_at }} as dbt_valid_from,
167-
cast(nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as TIMESTAMP) as dbt_valid_to
167+
cast(nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as TIMESTAMP(9)) as dbt_valid_to
168168
from (
169169
{{ sql }}
170170
) sbq
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{#
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
#}
16+
17+
{#
18+
Legacy hash function ORA_HASH is known to have collisions in large datasets
19+
causing errors in snapshot merge statement. Please check the below Github
20+
issues:
21+
22+
https://github.com/oracle/dbt-oracle/issues/52
23+
https://github.com/oracle/dbt-oracle/issues/102
24+
25+
This hash function is used in the marcro oracle__snapshot_hash_arguments
26+
27+
dbt-oracle 2.0 will switch to a stronger hash function - SHA256. Changing the
28+
hash function will invalidate existing snapshots.These helper macros will
29+
ensure a smoother transition to dbt-oracle 2.0.
30+
31+
It is recommended for teams to switch to SHA256 hash function before
32+
dbt-oracle 2.0 using a 2-step process:
33+
1. Create a macro oracle__snapshot_hash_arguments(args) in your dbt project
34+
Copy paste the contents of macro
35+
oracle__snapshot_standard_hash_arguments(args) shown below. This will become
36+
the default from dbt-oracle 2.0
37+
38+
2. Run the following operation on your snapshot table
39+
40+
dbt --debug run-operation update_legacy_dbt_scd_id \
41+
--args '{snapshot_table: PROMOTION_COSTS_SNAPSHOT, cols: ["promo_id", "dbt_updated_at"]}'
42+
43+
#}
44+
45+
{% macro oracle__snapshot_standard_hash_arguments(args) -%}
46+
STANDARD_HASH({%- for arg in args -%}
47+
coalesce(cast({{ arg }} as varchar(4000) ), '')
48+
{% if not loop.last %} || '|' || {% endif %}
49+
{%- endfor -%}, 'SHA256')
50+
{%- endmacro %}
51+
52+
53+
{% macro update_legacy_dbt_scd_id(snapshot_table, cols) -%}
54+
55+
{%- call statement('update_legacy_dbt_scd_id_dtype') -%}
56+
BEGIN
57+
UPDATE {{ snapshot_table }} SET DBT_SCD_ID = NULL;
58+
COMMIT;
59+
EXECUTE IMMEDIATE 'ALTER TABLE {{ snapshot_table }} MODIFY (dbt_scd_id RAW(32))';
60+
END;
61+
{%- endcall -%}
62+
63+
{%- call statement('update_legacy_dbt_scd_id') -%}
64+
BEGIN
65+
UPDATE {{ snapshot_table }}
66+
SET dbt_scd_id = {{ oracle__snapshot_standard_hash_arguments(cols) }};
67+
COMMIT;
68+
END;
69+
{%- endcall -%}
70+
{%- endmacro %}

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dbt-common>=1.1.0,<2.0
22
dbt-adapters>=1.2.1,<2.0
33
dbt-core>=1.8.1,<2.0
4-
oracledb==2.5.0
4+
oracledb==2.5.1

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ install_requires =
3535
dbt-common>=1.1.0,<2.0
3636
dbt-adapters>=1.2.1,<2.0
3737
dbt-core~=1.8,<1.9
38-
oracledb==2.5.0
38+
oracledb==2.5.1
3939
test_suite=tests
4040
test_requires =
4141
dbt-tests-adapter~=1.8,<1.9

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dbt-common>=1.1.0,<2.0",
4444
"dbt-adapters>=1.2.1,<2.0",
4545
"dbt-core~=1.8,<1.9",
46-
"oracledb==2.5.0"
46+
"oracledb==2.5.1"
4747
]
4848

4949
test_requirements = [

0 commit comments

Comments
 (0)