Skip to content

Bug 179 #180

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

Closed
wants to merge 4 commits 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
35 changes: 33 additions & 2 deletions dbt/include/oracle/macros/materializations/snapshot/snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
where {{ unique_key_is_null(strategy.unique_key, "snapshotted_data") }}
or ({{ unique_key_is_not_null(strategy.unique_key, "snapshotted_data") }} and ({{ strategy.row_changed }})

{%- if strategy.hard_deletes == 'new_record' -%}
or ({{ unique_key_is_not_null(strategy.unique_key, "snapshotted_data") }} and snapshotted_data.{{ columns.dbt_is_deleted }} = 'True')
{%- endif %}
)

),
Expand All @@ -140,6 +142,10 @@
where (
{{ strategy.row_changed }}
)
{%- if strategy.hard_deletes == 'new_record' -%}
or snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
{%- endif %}

)

{%- if strategy.hard_deletes == 'invalidate' or strategy.hard_deletes == 'new_record' -%}
Expand All @@ -162,6 +168,20 @@
left join deletes_source_data source_data
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
where {{ unique_key_is_null(strategy.unique_key, "source_data") }}
{%- if strategy.hard_deletes == 'new_record' %}
and not (
--avoid updating the record's valid_to if the latest entry is marked as deleted
snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
and
{% if config.get('dbt_valid_to_current') %}
{% set source_unique_key = columns.dbt_valid_to | trim %}
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}
( {{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null )
{% else %}
{{ columns.dbt_valid_to }} is null
{% endif %}
)
{%- endif %}
)
{%- endif %}

Expand Down Expand Up @@ -191,7 +211,18 @@
left join deletes_source_data source_data
on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }}
where {{ unique_key_is_null(strategy.unique_key, "source_data") }}

and not (
--avoid inserting a new record if the latest one is marked as deleted
snapshotted_data.{{ columns.dbt_is_deleted }} = 'True'
and
{% if config.get('dbt_valid_to_current') %}
{% set source_unique_key = columns.dbt_valid_to | trim %}
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}
( {{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null )
{% else %}
{{ columns.dbt_valid_to }} is null
{% endif %}
)
)
{%- endif %}

Expand Down
15 changes: 15 additions & 0 deletions dbt_adbs_test_project/dbt_test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export DBT_ORACLE_USER=dbt_test
export DBT_ORACLE_HOST=localhost
export DBT_ORACLE_PROTOCOL=tcps
export DBT_ORACLE_PORT=1522
export DBT_ORACLE_SERVICE=myadw_medium.adb.oraclecloud.com
export DBT_ORACLE_PASSWORD=Auto_MY_DBT_1234
export DBT_ORACLE_DATABASE=MYADW
export DBT_ORACLE_SCHEMA=dbt_test
export DBT_ORACLE_CUSTOM_SCHEMA=dbt_test
export DBT_MACRO_DEBUGGING=1
export WALLET_LOCATION=/scratch/tls_wallet
export WALLET_PASSWORD=Auto_MY_ADB_Wallet
export TNS_ADMIN=/scratch/tls_wallet
export SSL_SERVER_CERT_DN="CN=3746b756d92b"
export DBT_ORACLE_OML_CLOUD_SERVICE_URL=""
8 changes: 8 additions & 0 deletions dbt_adbs_test_project/models/dummy_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ config(materialized='table') }}

SELECT
id,
label,
current_timestamp AS created_at
FROM {{ ref('dummy_table') }}

5 changes: 5 additions & 0 deletions dbt_adbs_test_project/models/filter_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
select id,
label,
created_at
from {{ ref ('dummy_model') }}
where id != 2
15 changes: 15 additions & 0 deletions dbt_adbs_test_project/models/property/property_data_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{ config(materialized='table') }}

SELECT
Cast (prop_data.property_id as int) property_id,
mort_data.mortgage_id,
prop_data.property_type,
prop_data.living_area,
--mort_data.create_date --zum zeigen von Enforce Model (contract sagt Date, aber ohne Spezification wird es Timestamp) (Null/Fehler bei Testdaten wird auch erkannt)
Cast( mort_data.create_date as date) create_date
FROM
{{ ref('mortgage_application_seed') }} mort_data
LEFT JOIN
{{ ref('property_data_seed') }} prop_data
ON
prop_data.property_id = mort_data.mortgage_id
49 changes: 49 additions & 0 deletions dbt_adbs_test_project/models/property/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: 2
models:
- name: property_data_product
config:
meta:
data_contract: property-data-product
owner: property_onboarding
materialized: table
contract:
enforced: True
description: This table contains all active mortgages and their financed properties.
columns:
- name: property_id
data_tests:
- dbt_expectations.expect_column_values_to_be_between:
min_value: 0
data_type: NUMBER
description: Unique identifier for the property
constraints:
- type: not_null
- type: unique
meta:
pii: false
- name: mortgage_id
data_tests:
- dbt_expectations.expect_column_values_to_be_between:
min_value: 0
data_type: NUMBER
description: Unique identifier for the mortgage agreement
constraints:
- type: not_null
meta:
pii: false
- name: property_type
data_type: STRING
description: Type of the property like single-family home.
meta:
pii: false
- name: living_area
data_type: FLOAT
description: Living area of the property
meta:
pii: false
- name: create_date
data_type: DATE
description: Creation date of the mortgage agreement
meta:
pii: false

1 change: 0 additions & 1 deletion dbt_adbs_test_project/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ dbt_test:
service: "{{ env_var('DBT_ORACLE_SERVICE') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
oml_cloud_service_url: "{{ env_var('DBT_ORACLE_OML_CLOUD_SERVICE_URL')}}"
session_info:
action: "dbt run"
client_identifier: "dbt-mac-abhisoms"
Expand Down
3 changes: 3 additions & 0 deletions dbt_adbs_test_project/seeds/dummy_table.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,label
1,first
2,second
4 changes: 4 additions & 0 deletions dbt_adbs_test_project/seeds/mortgage_application_seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mortgage_id,create_date
1,2025-06-03
2,2025-01-20
3,2024-02-03
5 changes: 5 additions & 0 deletions dbt_adbs_test_project/seeds/property_data_seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
property_id,property_type,living_area
1,commercial,200.34
2,office,3000.0
3,retail,2020.0
4,apartment,690.4
15 changes: 15 additions & 0 deletions dbt_adbs_test_project/snapshots/dummy_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{% snapshot new_snapshot_dummy_table %}

{{
config(
strategy = 'timestamp'
, unique_key = 'id'
, updated_at = 'created_at'
, hard_deletes = 'new_record'
)
}}

SELECT * FROM {{ ref ('dummy_model') }}

{% endsnapshot %}