9
9
{%- set tags_by_schema = {} - %}
10
10
{% for res in results - %}
11
11
{% 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
+
13
19
{%- set model_database = res .node .database - %}
14
20
{%- set model_schema = res .node .schema - %}
15
21
{%- set model_schema_full = model_database+ ' .' + model_schema - %}
23
29
USE SCHEMA {{model_schema}}
24
30
{%- endcall - %}
25
31
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) }}
28
34
{%- call statement(' main' , fetch_result= True) - %}
29
- show tags in {{model_database }}.{{model_schema }}
35
+ show tags in {{tag_database }}.{{tag_schema }}
30
36
{%- 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}) - %}
32
38
{{ log(' Added tags to cache' , info= True) }}
33
39
{% else %}
34
40
{{ log(' already have tag info for schema' , info= True) }}
35
41
{% endif %}
36
42
37
- {%- set current_tags_in_schema = tags_by_schema[model_schema_full ] - %}
43
+ {%- set current_tags_in_schema = tags_by_schema[tag_schema_full ] - %}
38
44
{{ log(' current_tags_in_schema:' , info= True) }}
39
45
{{ log(current_tags_in_schema, info= True) }}
40
46
{{ log(" ========== Processing tags for " + model_schema_full+ " ." + model_alias+ " ==========" , info= True) }}
62
68
{{ log(existing_tags_for_table, info= True) }}
63
69
64
70
{% 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 ) }}
66
73
{% 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) }}
68
75
{% endfor %}
69
76
{% for column in res .node .columns %}
70
77
{% for column_tag in res .node .columns[column].meta .database_tags %}
78
+ {% set column_tag_full = tag_schema_full+ ' .' + column_tag %}
71
79
{{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 )}}
73
81
{% 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)}}
75
83
{% endfor %}
76
84
{% endfor %}
77
85
{{ log(" ========== Finished processing tags for " + model_alias+ " ==========" , info= True) }}
82
90
{{ return(' ' ) }}
83
91
{% endmacro %}
84
92
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,
87
95
-- or any of the column's meta contains database_tags.
88
96
-- Otherwise it returns False
89
97
# }
90
98
{% macro model_contains_tag_meta(model_node) %}
91
99
{% if model_node .meta .database_tags %}
92
100
{{ return(True) }}
93
101
{% endif %}
94
- {#
102
+ {#
95
103
-- For compatibility with the old results structure
96
104
# }
97
105
{% if model_node .config .meta .database_tags %}
105
113
{{ return(False) }}
106
114
{% endmacro %}
107
115
108
- {#
116
+ {#
109
117
-- 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),
111
119
-- checks if the new tag (new_tag) is already in the list and
112
120
-- creates it in Snowflake if it doesn't.
113
121
# }
114
122
{% 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 %}
116
124
{{ log(' Creating missing tag ' + new_tag, info= True) }}
117
125
{%- call statement(' main' , fetch_result= True) - %}
118
126
create tag {{new_tag}}
125
133
{% endmacro %}
126
134
127
135
-- select LEVEL,OBJECT_NAME,COLUMN_NAME,UPPER(TAG_NAME) as TAG_NAME,TAG_VALUE
128
- {#
136
+ {#
129
137
-- 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.
131
139
-- The first column (attribute '0') contains 'TABLE' or 'COLUMN', since we're looking
132
140
-- at table tags here then we include only 'TABLE' values.
133
141
-- The second column (attribute '1') contains the name of the table, we filter on that.
152
160
{{ log(load_result(' main' ).data, info= True) }}
153
161
{% endif %}
154
162
{% endmacro %}
155
- {#
163
+ {#
156
164
-- 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.
158
166
-- The first column (attribute '0') contains 'TABLE' or 'COLUMN', since we're looking
159
167
-- at column tags here then we include only 'COLUMN' values.
160
168
-- The second column (attribute '1') contains the name of the table, we filter on that.
177
185
{%- endcall - %}
178
186
{{ log(load_result(' main' ).data, info= True) }}
179
187
{% endif %}
180
- {% endmacro %}
188
+ {% endmacro %}
0 commit comments