Skip to content
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

feat: adding a check_exclude_cols strategy #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240726-083735.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Adding a check_exclude_cols option to the 'check' snapshots strategy.
time: 2024-07-26T08:37:35.411334+01:00
custom:
Author: willmaclean
Issue: "273"
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,33 @@
{#-- handle any schema changes --#}
{%- set target_relation = adapter.get_relation(database=node.database, schema=node.schema, identifier=node.alias) -%}

{% if check_cols_config == 'all' %}
{%- set query_columns = get_columns_in_query(node['compiled_code']) -%}

{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %}
{#-- query for proper casing/quoting, to support comparison below --#}
{%- set select_check_cols_from_target -%}
{#-- N.B. The whitespace below is necessary to avoid edge case issue with comments --#}
{#-- See: https://github.com/dbt-labs/dbt-core/issues/6781 --#}
select {{ check_cols_config | join(', ') }} from (
{{ node['compiled_code'] }}
) subq
{%- endset -%}
{% set query_columns = get_columns_in_query(select_check_cols_from_target) %}
{% if check_cols_config is not null %}
{% if check_cols_config == 'all' %}
{%- set query_columns = get_columns_in_query(node['compiled_code']) -%}

{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %}
{#-- query for proper casing/quoting, to support comparison below --#}
{%- set select_check_cols_from_target -%}
{#-- N.B. The whitespace below is necessary to avoid edge case issue with comments --#}
{#-- See: https://github.com/dbt-labs/dbt-core/issues/6781 --#}
select {{ check_cols_config | join(', ') }} from (
{{ node['compiled_code'] }}
) subq
{%- endset -%}
{% set query_columns = get_columns_in_query(select_check_cols_from_target) %}

{% else %}
{% do exceptions.raise_compiler_error("Invalid value for 'check_cols': " ~ check_cols_config) %}
{% endif %}
{# check_cols_config and check_exclude_cols_config will not both be non-null #}
{% elif check_exclude_cols_config is iterable and (check_exclude_cols_config | length) > 0 %}
-- need to get all columns except the excluded ones
-- get all query_columns but then remove all of the ones in check_exclude_cols_config
{%- set all_columns_in_query = get_columns_in_query(node['compiled_code']) -%}
{% set query_columns = check_exclude_cols_config | difference(all_columns_in_query) %}

{% else %}
{% do exceptions.raise_compiler_error("Invalid value for 'check_cols': " ~ check_cols_config) %}
{% do exceptions.raise_compiler_error("Invalid value for 'check_exclude_cols': " ~ check_exclude_cols_config) %}
{% endif %}

{%- set existing_cols = adapter.get_columns_in_relation(target_relation) | map(attribute = 'name') | list -%}
Expand All @@ -135,6 +146,7 @@

{% macro snapshot_check_strategy(node, snapshotted_rel, current_rel, config, target_exists) %}
{% set check_cols_config = config['check_cols'] %}
{% set check_exclude_cols_config = config['check_exclude_cols'] %}
{% set primary_key = config['unique_key'] %}
{% set invalidate_hard_deletes = config.get('invalidate_hard_deletes', false) %}
{% set updated_at = config.get('updated_at', snapshot_get_time()) %}
Expand Down