Skip to content

Commit

Permalink
Merge pull request #17 from get-select/handle_non_mappings_gracefully
Browse files Browse the repository at this point in the history
Add warning when non-mapping query tag is set
  • Loading branch information
NiallRees authored Aug 18, 2023
2 parents bf3b513 + 589b4be commit 630b422
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changes/2.3.1.md
Original file line number Diff line number Diff line change
@@ -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))


8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: 'dbt_snowflake_query_tags'
version: '2.3.0'
version: '2.3.1'
config-version: 2
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 3 additions & 2 deletions integration_test_project/models/materialized_table.sql
Original file line number Diff line number Diff line change
@@ -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"
)
}}

Expand Down
20 changes: 13 additions & 7 deletions macros/query_tags.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,37 @@

{% 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 %}


{# 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 %}

Expand Down

0 comments on commit 630b422

Please sign in to comment.