From 589b4be7c6977b9c85d25bd0804fc887397d00d1 Mon Sep 17 00:00:00 2001 From: Niall Woodward Date: Fri, 18 Aug 2023 10:02:36 +0100 Subject: [PATCH] Add warning when non-mapping query tag is set --- .changes/2.3.1.md | 7 +++++++ CHANGELOG.md | 8 ++++++++ dbt_project.yml | 2 +- .../models/materialized_incremental.sql | 2 +- .../models/materialized_table.sql | 5 +++-- macros/query_tags.sql | 20 ++++++++++++------- 6 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 .changes/2.3.1.md diff --git a/.changes/2.3.1.md b/.changes/2.3.1.md new file mode 100644 index 0000000..37e6ad8 --- /dev/null +++ b/.changes/2.3.1.md @@ -0,0 +1,7 @@ +## dbt-snowflake-query-tags 2.3.1 - August 18, 2023 + +### Features + +- Handle non-mapping configs gracefully ([#17](https://github.com/get-select/dbt-snowflake-query-tags/pull/17)) + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 98bc340..9bd0835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and is generated by [Changie](https://github.com/miniscruff/changie). +## dbt-snowflake-query-tags 2.3.1 - August 18, 2023 + +### Features + +- Handle non-mapping configs gracefully ([#17](https://github.com/get-select/dbt-snowflake-query-tags/pull/17)) + + + ## dbt-snowflake-query-tags 2.3.0 - June 29, 2023 ### Features diff --git a/dbt_project.yml b/dbt_project.yml index 367649f..cce6929 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -1,3 +1,3 @@ name: 'dbt_snowflake_query_tags' -version: '2.3.0' +version: '2.3.1' config-version: 2 diff --git a/integration_test_project/models/materialized_incremental.sql b/integration_test_project/models/materialized_incremental.sql index bd6e4fc..9855ef1 100644 --- a/integration_test_project/models/materialized_incremental.sql +++ b/integration_test_project/models/materialized_incremental.sql @@ -1,4 +1,4 @@ -{{ config(materialized='incremental', tags=['a', 'b', 'c']) }} +{{ config(materialized='incremental', tags=['a', 'b', 'c'], query_tag={'test': 'test'}) }} select 1 as a diff --git a/integration_test_project/models/materialized_table.sql b/integration_test_project/models/materialized_table.sql index af11a9f..a42cd4d 100644 --- a/integration_test_project/models/materialized_table.sql +++ b/integration_test_project/models/materialized_table.sql @@ -1,11 +1,12 @@ {{ config( meta={ - "owner": "@alice", + "owner": "@alice", "model_maturity": "in dev" }, materialized="table", - tags='a' + tags='a', + query_tag="this will generate a warning" ) }} diff --git a/macros/query_tags.sql b/macros/query_tags.sql index 63ec09c..31ba45f 100644 --- a/macros/query_tags.sql +++ b/macros/query_tags.sql @@ -4,15 +4,21 @@ {% macro default__set_query_tag() -%} {# Start with any model-configured dict #} - {% set tag_dict = config.get('query_tag', default={}) %} + {% set query_tag = config.get('query_tag', default={}) %} - {%- do tag_dict.update( + {% if query_tag is not mapping %} + {% do log("dbt-snowflake-query-tags warning: the query_tag config value of '{}' is not a mapping type, so is being ignored. If you'd like to add additional query tag information, use a mapping type instead, or remove it to avoid this message.".format(query_tag), True) %} + {% set query_tag = {} %} {# If the user has set the query tag config as a non mapping type, start fresh #} + {% endif %} + + + {%- do query_tag.update( app='dbt', dbt_snowflake_query_tags_version='2.3.0', ) -%} {% if thread_id %} - {%- do tag_dict.update( + {%- do query_tag.update( thread_id=thread_id ) -%} {% endif %} @@ -20,15 +26,15 @@ {# We have to bring is_incremental through here because its not available in the comment context #} {% if model.resource_type == 'model' %} - {%- do tag_dict.update( + {%- do query_tag.update( is_incremental=is_incremental() ) -%} {% endif %} - {% set new_query_tag = tojson(tag_dict) %} + {% set query_tag_json = tojson(query_tag) %} {% set original_query_tag = get_current_query_tag() %} - {{ log("Setting query_tag to '" ~ new_query_tag ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }} - {% do run_query("alter session set query_tag = '{}'".format(new_query_tag)) %} + {{ log("Setting query_tag to '" ~ query_tag_json ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }} + {% do run_query("alter session set query_tag = '{}'".format(query_tag_json)) %} {{ return(original_query_tag)}} {% endmacro %}