dbt-labs/dbt-core

[Bug] When a custom `dbt_valid_to_current` is configured, it doesn't update

Closed this issue · 1 comments

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

Here's how dbt works when a custom dbt_valid_to_current is not configured and id 1 is updated between two snapshots:

Snapshot 1:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:36:... 2024-10-25 17:36:...

Snapshot 2:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:36:... 2024-10-25 17:36:... 2024-10-25 17:36:...
1 2024-10-25 17:36:... 2024-10-25 17:36:...

Note that dbt_valid_to in the 1st row NULL after the initial snapshot, and it is updated to be the current time during the following snapshot.

But if you configure a custom dbt_valid_to_current, it doesn't update 😢

Snapshot 1:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:44:... 2024-10-25 17:44:... 9999-12-31 00:00:00

Snapshot 2:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:44:... 2024-10-25 17:44:... 9999-12-31 00:00:00
1 2024-10-25 17:44:... 2024-10-25 17:44:... 9999-12-31 00:00:00

Expected Behavior

If you configure a custom dbt_valid_to_current, it should update the dbt_valid_to from the configured value to the time of the snapshot.

Snapshot 1:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:44:... 2024-10-25 17:44:... 9999-12-31 00:00:00

Snapshot 2:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-25 17:44:... 2024-10-25 17:44:... 2024-10-25 17:44:...
1 2024-10-25 17:44:... 2024-10-25 17:44:... 9999-12-31 00:00:00

Steps To Reproduce

models/my_model.sql

select 1 as id, {{ dbt.current_timestamp() }} as updated_at

snapshots/_snapshots.yml

snapshots:
  - name: my_snapshot
    relation: "ref('my_model')"
    config:
      unique_key: id
      strategy: check
      check_cols: all
      dbt_valid_to_current: "date('9999-12-31')"

Assuming that a table for my_snapshot doesn't already exist, then run these commands:

dbt build
dbt show --inline "select id, updated_at, dbt_valid_from, dbt_valid_to from {{ ref('my_snapshot') }} order by updated_at asc" --no-partial-parse --limit 99
dbt build
dbt show --inline "select id, updated_at, dbt_valid_from, dbt_valid_to from {{ ref('my_snapshot') }} order by updated_at asc" --no-partial-parse --limit 99

Relevant log output

(env) $ dbt build                                  
dbt show --inline "select id, updated_at, dbt_valid_from, dbt_valid_to from {{ ref('my_snapshot') }}
order by updated_at asc" --no-partial-parse --limit 99
17:44:30  Running with dbt=1.9.0-b2
17:44:30  Registered adapter: postgres=1.9.0-b1
17:44:31  Found 1 model, 1 snapshot, 430 macros
17:44:31  
17:44:31  Concurrency: 5 threads (target='postgres')
17:44:31  
17:44:31  1 of 2 START sql view model dbt_dbeatty.my_model ............................... [RUN]
17:44:31  1 of 2 OK created sql view model dbt_dbeatty.my_model .......................... [CREATE VIEW in 0.16s]
17:44:31  2 of 2 START snapshot dbt_dbeatty.my_snapshot .................................. [RUN]
17:44:31  2 of 2 OK snapshotted dbt_dbeatty.my_snapshot .................................. [SELECT 1 in 0.11s]
17:44:31  
17:44:31  Finished running 1 snapshot, 1 view model in 0 hours 0 minutes and 0.57 seconds (0.57s).
17:44:31  
17:44:31  Completed successfully
17:44:31  
17:44:31  Done. PASS=2 WARN=0 ERROR=0 SKIP=0 TOTAL=2
17:44:33  Running with dbt=1.9.0-b2
17:44:33  Registered adapter: postgres=1.9.0-b1
17:44:34  Found 1 model, 1 snapshot, 1 sql operation, 430 macros
17:44:34  
17:44:34  Concurrency: 5 threads (target='postgres')
17:44:34  
17:44:35  Previewing inline node:
| id |           updated_at |       dbt_valid_from |        dbt_valid_to |
| -- | -------------------- | -------------------- | ------------------- |
|  1 | 2024-10-25 17:44:... | 2024-10-25 17:44:... | 9999-12-31 00:00:00 |

(env) $ dbt build
dbt show --inline "select id, updated_at, dbt_valid_from, dbt_valid_to from {{ ref('my_snapshot') }} order by updated_at asc" --no-partial-parse --limit 99
17:44:49  Running with dbt=1.9.0-b2
17:44:49  Registered adapter: postgres=1.9.0-b1
17:44:50  Found 1 model, 1 snapshot, 430 macros
17:44:50  
17:44:50  Concurrency: 5 threads (target='postgres')
17:44:50  
17:44:50  1 of 2 START sql view model dbt_dbeatty.my_model ............................... [RUN]
17:44:50  1 of 2 OK created sql view model dbt_dbeatty.my_model .......................... [CREATE VIEW in 0.13s]
17:44:50  2 of 2 START snapshot dbt_dbeatty.my_snapshot .................................. [RUN]
17:44:50  2 of 2 OK snapshotted dbt_dbeatty.my_snapshot .................................. [INSERT 0 1 in 0.30s]
17:44:50  
17:44:50  Finished running 1 snapshot, 1 view model in 0 hours 0 minutes and 0.74 seconds (0.74s).
17:44:50  
17:44:50  Completed successfully
17:44:50  
17:44:50  Done. PASS=2 WARN=0 ERROR=0 SKIP=0 TOTAL=2
17:44:52  Running with dbt=1.9.0-b2
17:44:52  Registered adapter: postgres=1.9.0-b1
17:44:53  Found 1 model, 1 snapshot, 1 sql operation, 430 macros
17:44:53  
17:44:53  Concurrency: 5 threads (target='postgres')
17:44:53  
17:44:54  Previewing inline node:
| id |           updated_at |       dbt_valid_from |        dbt_valid_to |
| -- | -------------------- | -------------------- | ------------------- |
|  1 | 2024-10-25 17:44:... | 2024-10-25 17:44:... | 9999-12-31 00:00:00 |
|  1 | 2024-10-25 17:44:... | 2024-10-25 17:44:... | 9999-12-31 00:00:00 |

Environment

- OS:
- Python:
- dbt: dbt=1.9.0-b2

Which database adapter are you using with dbt?

postgres

Additional Context

Consequence: without the expected behavior, there will be duplicate rows whenever a user tries to slice between the dbt_valid_from and dbt_valid_to columns.

Nevermind! After reinstalling all the relevant repos from their main branches, I got the expected output:

id updated_at dbt_valid_from dbt_valid_to
1 2024-10-29 20:21:... 2024-10-29 20:21:... 2024-10-29 20:21:...
1 2024-10-29 20:21:... 2024-10-29 20:21:... 9999-12-31 00:00:00