Skip to content

Commit 33491ea

Browse files
arjunthangarajArjun Thangaraj
and
Arjun Thangaraj
authored
feat: add environment variables for tag database and schema (#49)
* Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema * Introduve environment varirables for tags created in common schema --------- Co-authored-by: Arjun Thangaraj <[email protected]>
1 parent 3d72c29 commit 33491ea

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,18 @@ The above means:
188188
The Snowflake table ACCOUNT will have the tag 'accounting_row_string' set to the value 'visible'.
189189
Its columns ACCOUNT_NAME and ACCOUNT_NUMBER will both have the tag 'accounting_col_string' set to the value 'visible'
190190

191-
All tags are created in the schema of the model where they are added. In the above example the tags will end up
191+
All tags are created in the schema of the model where they are added, by default. In the above example the tags will end up
192192
in the FINANCE schema (name depends on how [DBT has been configured](https://docs.getdbt.com/docs/build/custom-schemas)).
193+
If the tag needs to be created in a different location/referred from different location, the below two environment
194+
variables need to be added in dbt_project.yml file as below:
195+
196+
```yml
197+
# dbt_project.yml
198+
199+
common_tag_database: 'audit'
200+
common_tag_schema: 'tags'
201+
```
202+
In the above example, the tags will be created in audit.tags .
193203
194204
The macro must be called as part of on-run-end, so add the following to dbt_project.yml:
195205
```

macros/apply_meta_as_tags.sql

+29-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
{%- set tags_by_schema = {} -%}
1010
{% for res in results -%}
1111
{% if snowflake_utils.model_contains_tag_meta(res.node) %}
12-
12+
13+
-- Tagging database and schema will be fetched from the below environment variables.
14+
-- If not given any enviornment variables, model database and schema will be used
15+
{%- set tag_database = var('common_tag_database', res.node.database) -%}
16+
{%- set tag_schema = var('common_tag_schema', res.node.schema) -%}
17+
{%- set tag_schema_full = tag_database+'.'+tag_schema -%}
18+
1319
{%- set model_database = res.node.database -%}
1420
{%- set model_schema = res.node.schema -%}
1521
{%- set model_schema_full = model_database+'.'+model_schema -%}
@@ -23,18 +29,18 @@
2329
USE SCHEMA {{model_schema}}
2430
{%- endcall -%}
2531

26-
{% if model_schema_full not in tags_by_schema.keys() %}
27-
{{ log('need to fetch tags for schema '+model_schema_full, info=True) }}
32+
{% if tag_schema_full not in tags_by_schema.keys() %}
33+
{{ log('need to fetch tags for schema '+tag_schema_full, info=True) }}
2834
{%- call statement('main', fetch_result=True) -%}
29-
show tags in {{model_database}}.{{model_schema}}
35+
show tags in {{tag_database}}.{{tag_schema}}
3036
{%- endcall -%}
31-
{%- set _ = tags_by_schema.update({model_schema_full: load_result('main')['table'].columns.get('name').values()|list}) -%}
37+
{%- set _ = tags_by_schema.update({tag_schema_full: load_result('main')['table'].columns.get('name').values()|list}) -%}
3238
{{ log('Added tags to cache', info=True) }}
3339
{% else %}
3440
{{ log('already have tag info for schema', info=True) }}
3541
{% endif %}
3642

37-
{%- set current_tags_in_schema = tags_by_schema[model_schema_full] -%}
43+
{%- set current_tags_in_schema = tags_by_schema[tag_schema_full] -%}
3844
{{ log('current_tags_in_schema:', info=True) }}
3945
{{ log(current_tags_in_schema, info=True) }}
4046
{{ log("========== Processing tags for "+model_schema_full+"."+model_alias+" ==========", info=True) }}
@@ -62,16 +68,18 @@
6268
{{ log(existing_tags_for_table, info=True) }}
6369

6470
{% for table_tag in model_meta.database_tags %}
65-
{{ snowflake_utils.create_tag_if_missing(current_tags_in_schema,table_tag|upper) }}
71+
{% set table_tag_full = tag_schema_full+'.'+table_tag %}
72+
{{ snowflake_utils.create_tag_if_missing(current_tags_in_schema,table_tag_full|upper) }}
6673
{% set desired_tag_value = model_meta.database_tags[table_tag] %}
67-
{{ snowflake_utils.set_table_tag_value_if_different(model_alias|upper,table_tag,desired_tag_value,existing_tags_for_table) }}
74+
{{ snowflake_utils.set_table_tag_value_if_different(model_alias|upper,table_tag_full|upper,desired_tag_value,existing_tags_for_table) }}
6875
{% endfor %}
6976
{% for column in res.node.columns %}
7077
{% for column_tag in res.node.columns[column].meta.database_tags %}
78+
{% set column_tag_full = tag_schema_full+'.'+column_tag %}
7179
{{log(column_tag,info=True)}}
72-
{{ snowflake_utils.create_tag_if_missing(current_tags_in_schema,column_tag|upper)}}
80+
{{ snowflake_utils.create_tag_if_missing(current_tags_in_schema,column_tag_full|upper)}}
7381
{% set desired_tag_value = res.node.columns[column].meta.database_tags[column_tag] %}
74-
{{ snowflake_utils.set_column_tag_value_if_different(model_alias|upper,column|upper,column_tag,desired_tag_value,existing_tags_for_table)}}
82+
{{ snowflake_utils.set_column_tag_value_if_different(model_alias|upper,column|upper,column_tag_full|upper,desired_tag_value,existing_tags_for_table)}}
7583
{% endfor %}
7684
{% endfor %}
7785
{{ log("========== Finished processing tags for "+model_alias+" ==========", info=True) }}
@@ -82,16 +90,16 @@
8290
{{ return('') }}
8391
{% endmacro %}
8492

85-
{#
86-
-- Given a node in a Result object, returns True if either the model meta contains database_tags,
93+
{#
94+
-- Given a node in a Result object, returns True if either the model meta contains database_tags,
8795
-- or any of the column's meta contains database_tags.
8896
-- Otherwise it returns False
8997
#}
9098
{% macro model_contains_tag_meta(model_node) %}
9199
{% if model_node.meta.database_tags %}
92100
{{ return(True) }}
93101
{% endif %}
94-
{#
102+
{#
95103
-- For compatibility with the old results structure
96104
#}
97105
{% if model_node.config.meta.database_tags %}
@@ -105,14 +113,14 @@
105113
{{ return(False) }}
106114
{% endmacro %}
107115

108-
{#
116+
{#
109117
-- Snowflake tags must exist before they are used.
110-
-- Given a list of all the existing tags in the account (all_tag_names),
118+
-- Given a list of all the existing tags in the account (all_tag_names),
111119
-- checks if the new tag (new_tag) is already in the list and
112120
-- creates it in Snowflake if it doesn't.
113121
#}
114122
{% macro create_tag_if_missing(all_tag_names,new_tag) %}
115-
{% if new_tag not in all_tag_names %}
123+
{% if new_tag.split('.')[2] not in all_tag_names %}
116124
{{ log('Creating missing tag '+new_tag, info=True) }}
117125
{%- call statement('main', fetch_result=True) -%}
118126
create tag {{new_tag}}
@@ -125,9 +133,9 @@
125133
{% endmacro %}
126134

127135
-- select LEVEL,OBJECT_NAME,COLUMN_NAME,UPPER(TAG_NAME) as TAG_NAME,TAG_VALUE
128-
{#
136+
{#
129137
-- Updates the value of a Snowflake table tag, if the provided value is different.
130-
-- existing_tags contains the results from querying tag_references_all_columns.
138+
-- existing_tags contains the results from querying tag_references_all_columns.
131139
-- The first column (attribute '0') contains 'TABLE' or 'COLUMN', since we're looking
132140
-- at table tags here then we include only 'TABLE' values.
133141
-- The second column (attribute '1') contains the name of the table, we filter on that.
@@ -152,9 +160,9 @@
152160
{{ log(load_result('main').data, info=True) }}
153161
{% endif %}
154162
{% endmacro %}
155-
{#
163+
{#
156164
-- Updates the value of a Snowflake column tag, if the provided value is different.
157-
-- existing_tags contains the results from querying tag_references_all_columns.
165+
-- existing_tags contains the results from querying tag_references_all_columns.
158166
-- The first column (attribute '0') contains 'TABLE' or 'COLUMN', since we're looking
159167
-- at column tags here then we include only 'COLUMN' values.
160168
-- The second column (attribute '1') contains the name of the table, we filter on that.
@@ -177,4 +185,4 @@
177185
{%- endcall -%}
178186
{{ log(load_result('main').data, info=True) }}
179187
{% endif %}
180-
{% endmacro %}
188+
{% endmacro %}

0 commit comments

Comments
 (0)