From daba363e5722f972abcf2bdb80c4b31665d19397 Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Mon, 4 Nov 2024 11:54:13 -0500 Subject: [PATCH] Raise an exception if the user changes the snapshot mode to hard_deletes="new_record" from a different mode. --- .../materializations/snapshots/helpers.sql | 2 +- .../materializations/snapshots/snapshot.sql | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dbt/include/global_project/macros/materializations/snapshots/helpers.sql b/dbt/include/global_project/macros/materializations/snapshots/helpers.sql index 273a1582..b41a1986 100644 --- a/dbt/include/global_project/macros/materializations/snapshots/helpers.sql +++ b/dbt/include/global_project/macros/materializations/snapshots/helpers.sql @@ -119,7 +119,7 @@ 'insert' as dbt_change_type, source_data.* {%- if strategy.hard_deletes == 'new_record' -%}, - 'False' as {{ columns.dbt_is_deleted }} {# Implies I, II, or III #} + 'False' as {{ columns.dbt_is_deleted }} {%- endif %} from insertions_source_data as source_data diff --git a/dbt/include/global_project/macros/materializations/snapshots/snapshot.sql b/dbt/include/global_project/macros/materializations/snapshots/snapshot.sql index 0c9590b6..0c54ede6 100644 --- a/dbt/include/global_project/macros/materializations/snapshots/snapshot.sql +++ b/dbt/include/global_project/macros/materializations/snapshots/snapshot.sql @@ -39,6 +39,22 @@ {{ adapter.valid_snapshot_target(target_relation, columns) }} + {# Raise an exception if the user has selected the new_record mode for + hard deletes, but there is no dbt_is_deleted column in the target, + which would indicate it was created in a different mode. #} + {% if strategy.hard_deletes == 'new_record' %} + {% set target_cols = adapter.get_columns_in_relation(target_relation) %} + {% set ns = namespace(found_is_deleted_col = false) %} + {% for col in target_cols %} + {% if col.column == columns['dbt_is_deleted'] %} + {% set ns.found_is_deleted_col = true %} + {% endif %} + {% endfor %} + {% if not ns.found_is_deleted_col %} + {% do exceptions.raise_compiler_error('Did not find a dbt_is_deleted column in snapshot target. Changing the snapshot hard_deletes mode is not supported after a snapshot has been created.') %} + {% endif %} + {% endif %} + {% set build_or_select_sql = snapshot_staging_table(strategy, sql, target_relation) %} {% set staging_table = build_snapshot_staging_table(strategy, sql, target_relation) %}