diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d39d11d87f..f8cfb722dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features - Added warning to nodes selector if nothing was matched ([#2115](https://github.com/fishtown-analytics/dbt/issues/2115), [#2343](https://github.com/fishtown-analytics/dbt/pull/2343)) +- Suport column descriptions for BigQuery models ([#2335](https://github.com/fishtown-analytics/dbt/issues/2335), [#2402](https://github.com/fishtown-analytics/dbt/pull/2402)) ### Fixes - When tracking is disabled due to errors, do not reset the invocation ID ([#2398](https://github.com/fishtown-analytics/dbt/issues/2398), [#2400](https://github.com/fishtown-analytics/dbt/pull/2400)) diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index 58ce2e0b306..820febbfc71 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -210,6 +210,12 @@ def require(self, name, validator=None): def get(self, name, validator=None, default=None): return '' + def persist_relation_docs(self) -> bool: + return False + + def persist_column_docs(self) -> bool: + return False + class RuntimeConfigObject(Config): def __init__( @@ -249,6 +255,24 @@ def get(self, name, validator=None, default=None): return to_return + def persist_relation_docs(self) -> bool: + persist_docs = self.get('persist_docs', default={}) + if not isinstance(persist_docs, dict): + raise_compiler_error( + f"Invalid value provided for 'persist_docs'. Expected dict " + f"but received {type(persist_docs)}") + + return persist_docs.get('relation', False) + + def persist_column_docs(self) -> bool: + persist_docs = self.get('persist_docs', default={}) + if not isinstance(persist_docs, dict): + raise_compiler_error( + f"Invalid value provided for 'persist_docs'. Expected dict " + f"but received {type(persist_docs)}") + + return persist_docs.get('columns', False) + # `adapter` implementations class ParseDatabaseWrapper(BaseDatabaseWrapper): diff --git a/core/dbt/include/global_project/macros/adapters/common.sql b/core/dbt/include/global_project/macros/adapters/common.sql index 627d65b926c..36258c0ee4c 100644 --- a/core/dbt/include/global_project/macros/adapters/common.sql +++ b/core/dbt/include/global_project/macros/adapters/common.sql @@ -151,6 +151,19 @@ 'alter_relation_comment macro not implemented for adapter '+adapter.type()) }} {% endmacro %} +{% macro persist_docs(relation, model, for_relation=true, for_columns=true) -%} + {{ return(adapter_macro('persist_docs', relation, model, for_relation, for_columns)) }} +{% endmacro %} + +{% macro default__persist_docs(relation, model, for_relation, for_columns) -%} + {% if for_relation and config.persist_relation_docs() %} + {% do run_query(alter_relation_comment(relation, model.description)) %} + {% endif %} + + {% if for_columns and config.persist_column_docs() %} + {% do run_query(alter_column_comment(relation, model.columns)) %} + {% endif %} +{% endmacro %} @@ -304,22 +317,3 @@ {% macro set_sql_header(config) -%} {{ config.set('sql_header', caller()) }} {%- endmacro %} - - -{%- macro set_relation_comment(relation) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set comment = get_relation_comment(raw_persist_docs, model) -%} - {%- if comment is not none -%} - {{ alter_relation_comment(relation, comment) }} - {%- endif -%} -{%- endmacro -%} - - -{%- macro set_column_comments(relation) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set column_dict = get_relation_column_comments(raw_persist_docs, model) -%} - {%- if column_dict is not none -%} - {{ alter_column_comment(relation, column_dict) }} - {%- endif -%} -{%- endmacro -%} - diff --git a/core/dbt/include/global_project/macros/etc/get_relation_comment.sql b/core/dbt/include/global_project/macros/etc/get_relation_comment.sql deleted file mode 100644 index b3291b01a3a..00000000000 --- a/core/dbt/include/global_project/macros/etc/get_relation_comment.sql +++ /dev/null @@ -1,29 +0,0 @@ -{% macro get_relation_comment(persist_docs, model) %} - - {%- if persist_docs is not mapping -%} - {{ exceptions.raise_compiler_error("Invalid value provided for 'persist_docs'. Expected dict but got value: " ~ persist_docs) }} - {% endif %} - - {% if persist_docs.get('relation', false) %} - {{ return(model.description) }} - {%- else -%} - {{ return(none) }} - {% endif %} - -{% endmacro %} - - -{% macro get_relation_column_comments(persist_docs, model) %} - - {%- if persist_docs is not mapping -%} - {{ exceptions.raise_compiler_error("Invalid value provided for 'persist_docs'. Expected dict but got value: " ~ persist_docs) }} - {% endif %} - - {% if persist_docs.get('columns', false) and model.columns|length != 0 %} - {{ return(model.columns) }} - {%- else -%} - {{ return(none) }} - {% endif %} - -{% endmacro %} - diff --git a/core/dbt/include/global_project/macros/materializations/incremental/incremental.sql b/core/dbt/include/global_project/macros/materializations/incremental/incremental.sql index e185d8205d8..ef304ad1796 100644 --- a/core/dbt/include/global_project/macros/materializations/incremental/incremental.sql +++ b/core/dbt/include/global_project/macros/materializations/incremental/incremental.sql @@ -38,6 +38,8 @@ {{ build_sql }} {% endcall %} + {% do persist_docs(target_relation, model) %} + {{ run_hooks(post_hooks, inside_transaction=True) }} -- `COMMIT` happens here diff --git a/core/dbt/include/global_project/macros/materializations/seed/seed.sql b/core/dbt/include/global_project/macros/materializations/seed/seed.sql index 3b3c3d883f2..490ca1457d1 100644 --- a/core/dbt/include/global_project/macros/materializations/seed/seed.sql +++ b/core/dbt/include/global_project/macros/materializations/seed/seed.sql @@ -139,6 +139,9 @@ {{ sql }} {% endcall %} + {% set target_relation = this.incorporate(type='table') %} + {% do persist_docs(target_relation, model) %} + {{ run_hooks(post_hooks, inside_transaction=True) }} -- `COMMIT` happens here @@ -146,7 +149,6 @@ {{ run_hooks(post_hooks, inside_transaction=False) }} - {% set target_relation = this.incorporate(type='table') %} {{ return({'relations': [target_relation]}) }} {% endmaterialization %} diff --git a/core/dbt/include/global_project/macros/materializations/snapshot/snapshot.sql b/core/dbt/include/global_project/macros/materializations/snapshot/snapshot.sql index b5bf44f00e9..0342ffa6a61 100644 --- a/core/dbt/include/global_project/macros/materializations/snapshot/snapshot.sql +++ b/core/dbt/include/global_project/macros/materializations/snapshot/snapshot.sql @@ -228,6 +228,8 @@ {{ final_sql }} {% endcall %} + {% do persist_docs(target_relation, model) %} + {{ run_hooks(post_hooks, inside_transaction=True) }} {{ adapter.commit() }} diff --git a/core/dbt/include/global_project/macros/materializations/table/table.sql b/core/dbt/include/global_project/macros/materializations/table/table.sql index 086a04365bd..f0588fe724f 100644 --- a/core/dbt/include/global_project/macros/materializations/table/table.sql +++ b/core/dbt/include/global_project/macros/materializations/table/table.sql @@ -49,6 +49,8 @@ {{ run_hooks(post_hooks, inside_transaction=True) }} + {% do persist_docs(target_relation, model) %} + -- `COMMIT` happens here {{ adapter.commit() }} diff --git a/core/dbt/include/global_project/macros/materializations/view/view.sql b/core/dbt/include/global_project/macros/materializations/view/view.sql index 4172cc14f47..8901d3181fc 100644 --- a/core/dbt/include/global_project/macros/materializations/view/view.sql +++ b/core/dbt/include/global_project/macros/materializations/view/view.sql @@ -51,6 +51,8 @@ {% endif %} {{ adapter.rename_relation(intermediate_relation, target_relation) }} + {% do persist_docs(target_relation, model) %} + {{ run_hooks(post_hooks, inside_transaction=True) }} {{ adapter.commit() }} diff --git a/plugins/bigquery/dbt/adapters/bigquery/impl.py b/plugins/bigquery/dbt/adapters/bigquery/impl.py index a11af60b72a..eb8083390b4 100644 --- a/plugins/bigquery/dbt/adapters/bigquery/impl.py +++ b/plugins/bigquery/dbt/adapters/bigquery/impl.py @@ -28,8 +28,20 @@ import google.cloud.exceptions import google.cloud.bigquery +from google.cloud.bigquery import SchemaField + import time import agate +import json + + +def sql_escape(string): + if not isinstance(string, str): + dbt.exceptions.raise_compiler_exception( + f'cannot escape a non-string: {string}' + ) + + return json.dumps(string)[1:-1] @dataclass @@ -320,16 +332,14 @@ def _get_dbt_columns_from_bq_table(self, table) -> List[BigQueryColumn]: def _agate_to_schema( self, agate_table: agate.Table, column_override: Dict[str, str] - ) -> List[google.cloud.bigquery.SchemaField]: + ) -> List[SchemaField]: """Convert agate.Table with column names to a list of bigquery schemas. """ bq_schema = [] for idx, col_name in enumerate(agate_table.column_names): inferred_type = self.convert_agate_type(agate_table, idx) type_ = column_override.get(col_name, inferred_type) - bq_schema.append( - google.cloud.bigquery.SchemaField(col_name, type_) - ) + bq_schema.append(SchemaField(col_name, type_)) return bq_schema def _materialize_as_view(self, model: Dict[str, Any]) -> str: @@ -545,6 +555,33 @@ def parse_partition_by( """ return PartitionConfig.parse(raw_partition_by) + def get_table_ref_from_relation(self, conn, relation): + return self.connections.table_ref(relation.database, + relation.schema, + relation.identifier, + conn) + + @available.parse_none + def update_column_descriptions(self, relation, columns): + if len(columns) == 0: + return + + conn = self.connections.get_thread_connection() + table_ref = self.get_table_ref_from_relation(conn, relation) + table = conn.handle.get_table(table_ref) + + new_schema = [] + for column in table.schema: + if column.name in columns: + column_config = columns[column.name] + column_dict = column.to_api_repr() + column_dict['description'] = column_config.get('description') + column = SchemaField.from_api_repr(column_dict) + new_schema.append(column) + + new_table = google.cloud.bigquery.Table(table_ref, schema=new_schema) + conn.handle.update_table(new_table, ['schema']) + @available.parse_none def alter_table_add_columns(self, relation, columns): @@ -614,3 +651,25 @@ def _get_cache_schemas( .format(database, candidate.schema) ) return result + + @available.parse(lambda *a, **k: {}) + def get_table_options( + self, config: Dict[str, Any], node: Dict[str, Any], temporary: bool + ) -> Dict[str, Any]: + opts = {} + if temporary: + expiration = 'TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 12 hour)' + opts['expiration_timestamp'] = expiration + + if config.persist_relation_docs() and 'description' in node: + description = sql_escape(node['description']) + opts['description'] = '"""{}"""'.format(description) + + if config.get('kms_key_name') is not None: + opts['kms_key_name'] = "'{}'".format(config.get('kms_key_name')) + + if config.get('labels'): + labels = config.get('labels', {}) + opts['labels'] = list(labels.items()) + + return opts diff --git a/plugins/bigquery/dbt/include/bigquery/macros/adapters.sql b/plugins/bigquery/dbt/include/bigquery/macros/adapters.sql index 78549c4d086..1502ece807e 100644 --- a/plugins/bigquery/dbt/include/bigquery/macros/adapters.sql +++ b/plugins/bigquery/dbt/include/bigquery/macros/adapters.sql @@ -28,34 +28,8 @@ {%- endmacro -%} -{%- macro bigquery_escape_comment(comment) -%} - {%- if comment is not string -%} - {%- do exceptions.raise_compiler_exception('cannot escape a non-string: ' ~ comment) -%} - {%- endif -%} - {%- do return((comment | tojson)[1:-1]) -%} -{%- endmacro -%} - - -{% macro bigquery_table_options(persist_docs, temporary, kms_key_name, labels) %} - {% set opts = {} -%} - - {%- set description = get_relation_comment(persist_docs, model) -%} - {%- if description is not none -%} - {%- do opts.update({'description': "'" ~ bigquery_escape_comment(description) ~ "'"}) -%} - {%- endif -%} - {%- if temporary -%} - {% do opts.update({'expiration_timestamp': 'TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 12 hour)'}) %} - {%- endif -%} - {%- if kms_key_name -%} - {%- do opts.update({'kms_key_name': "'" ~ kms_key_name ~ "'"}) -%} - {%- endif -%} - {%- if labels -%} - {%- set label_list = [] -%} - {%- for label, value in labels.items() -%} - {%- do label_list.append((label, value)) -%} - {%- endfor -%} - {%- do opts.update({'labels': label_list}) -%} - {%- endif -%} +{% macro bigquery_table_options(config, node, temporary) %} + {% set opts = adapter.get_table_options(config, node, temporary) %} {% set options -%} OPTIONS({% for opt_key, opt_val in opts.items() %} @@ -68,9 +42,6 @@ {% macro bigquery__create_table_as(temporary, relation, sql) -%} {%- set raw_partition_by = config.get('partition_by', none) -%} {%- set raw_cluster_by = config.get('cluster_by', none) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set raw_kms_key_name = config.get('kms_key_name', none) -%} - {%- set raw_labels = config.get('labels', {}) -%} {%- set sql_header = config.get('sql_header', none) -%} {%- set partition_config = adapter.parse_partition_by(raw_partition_by) -%} @@ -80,12 +51,7 @@ create or replace table {{ relation }} {{ partition_by(partition_config) }} {{ cluster_by(raw_cluster_by) }} - {{ bigquery_table_options( - persist_docs=raw_persist_docs, - temporary=temporary, - kms_key_name=raw_kms_key_name, - labels=raw_labels - ) }} + {{ bigquery_table_options(config, model, temporary) }} as ( {{ sql }} ); @@ -93,14 +59,12 @@ {%- endmacro -%} {% macro bigquery__create_view_as(relation, sql) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set raw_labels = config.get('labels', []) -%} {%- set sql_header = config.get('sql_header', none) -%} {{ sql_header if sql_header is not none }} create or replace view {{ relation }} - {{ bigquery_table_options(persist_docs=raw_persist_docs, temporary=false, labels=raw_labels) }} + {{ bigquery_table_options(config, model, temporary=false) }} as ( {{ sql }} ); @@ -149,3 +113,14 @@ {% macro bigquery__check_schema_exists(information_schema, schema) %} {{ return(adapter.check_schema_exists(information_schema.database, schema)) }} {% endmacro %} + +{#-- relation-level macro is not implemented. This is handled in the CTAs statement #} +{% macro bigquery__persist_docs(relation, model, for_relation, for_columns) -%} + {% if for_columns and config.persist_column_docs() %} + {% do alter_column_comment(relation, model.columns) %} + {% endif %} +{% endmacro %} + +{% macro bigquery__alter_column_comment(relation, column_dict) -%} + {% do adapter.update_column_descriptions(relation, column_dict) %} +{% endmacro %} diff --git a/plugins/bigquery/dbt/include/bigquery/macros/materializations/incremental.sql b/plugins/bigquery/dbt/include/bigquery/macros/materializations/incremental.sql index 6aabf8d7521..28ceedebc77 100644 --- a/plugins/bigquery/dbt/include/bigquery/macros/materializations/incremental.sql +++ b/plugins/bigquery/dbt/include/bigquery/macros/materializations/incremental.sql @@ -155,6 +155,10 @@ {{ run_hooks(post_hooks) }} + {% set target_relation = this.incorporate(type='table') %} + + {% do persist_docs(target_relation, model) %} + {{ return({'relations': [target_relation]}) }} {%- endmaterialization %} diff --git a/plugins/bigquery/dbt/include/bigquery/macros/materializations/table.sql b/plugins/bigquery/dbt/include/bigquery/macros/materializations/table.sql index ea9b7e4d39c..cbb97ea4ec7 100644 --- a/plugins/bigquery/dbt/include/bigquery/macros/materializations/table.sql +++ b/plugins/bigquery/dbt/include/bigquery/macros/materializations/table.sql @@ -77,6 +77,8 @@ {{ run_hooks(post_hooks) }} + {% do persist_docs(target_relation, model) %} + {{ return({'relations': [target_relation]}) }} {% endmaterialization %} diff --git a/plugins/bigquery/dbt/include/bigquery/macros/materializations/view.sql b/plugins/bigquery/dbt/include/bigquery/macros/materializations/view.sql index 04cac1c1c06..3e43abfcf68 100644 --- a/plugins/bigquery/dbt/include/bigquery/macros/materializations/view.sql +++ b/plugins/bigquery/dbt/include/bigquery/macros/materializations/view.sql @@ -9,5 +9,11 @@ {% materialization view, adapter='bigquery' -%} - {{ return(create_or_replace_view(run_outside_transaction_hooks=False)) }} + {% set to_return = create_or_replace_view(run_outside_transaction_hooks=False) %} + + {% set target_relation = this.incorporate(type='view') %} + {% do persist_docs(target_relation, model) %} + + {% do return(to_return) %} + {%- endmaterialization %} diff --git a/plugins/postgres/dbt/include/postgres/macros/adapters.sql b/plugins/postgres/dbt/include/postgres/macros/adapters.sql index 54bb1e13410..52b40edaa24 100644 --- a/plugins/postgres/dbt/include/postgres/macros/adapters.sql +++ b/plugins/postgres/dbt/include/postgres/macros/adapters.sql @@ -12,20 +12,8 @@ as ( {{ sql }} ); - - {% set relation = relation.incorporate(type='table') %} - {{ set_relation_comment(relation) }} - {{ set_column_comments(relation) }} {%- endmacro %} - -{% macro postgres__create_view_as(relation, sql) %} - {{ default__create_view_as(relation, sql) }} - {%- set relation = relation.incorporate(type='view') -%} - {{ set_relation_comment(relation) }} - {{ set_column_comments(relation) }} -{% endmacro %} - {% macro postgres__create_schema(database_name, schema_name) -%} {% if database_name -%} {{ adapter.verify_database(database_name) }} @@ -142,16 +130,16 @@ {# By using dollar-quoting like this, users can embed anything they want into their comments - (including nested dollar-quoting), as long as they don't use this exact dollar-quoting + (including nested dollar-quoting), as long as they do not use this exact dollar-quoting label. It would be nice to just pick a new one but eventually you do have to give up. #} {% macro postgres_escape_comment(comment) -%} {% if comment is not string %} - {% do exceptions.raise_compiler_exception('cannot escape a non-string: ' ~ comment) %} + {% do exceptions.raise_compiler_error('cannot escape a non-string: ' ~ comment) %} {% endif %} {%- set magic = '$dbt_comment_literal_block$' -%} {%- if magic in comment -%} - {%- do exceptions.raise_compiler_exception('The string ' ~ magic ~ ' is not allowed in comments.') -%} + {%- do exceptions.raise_compiler_error('The string ' ~ magic ~ ' is not allowed in comments.') -%} {%- endif -%} {{ magic }}{{ comment }}{{ magic }} {%- endmacro %} diff --git a/plugins/redshift/dbt/include/redshift/macros/adapters.sql b/plugins/redshift/dbt/include/redshift/macros/adapters.sql index beace1e0294..e1b359873d8 100644 --- a/plugins/redshift/dbt/include/redshift/macros/adapters.sql +++ b/plugins/redshift/dbt/include/redshift/macros/adapters.sql @@ -49,10 +49,6 @@ as ( {{ sql }} ); - - {% set relation = relation.incorporate(type='table') %} - {{ set_relation_comment(relation) }} - {{ set_column_comments(relation) }} {%- endmacro %} @@ -67,16 +63,6 @@ create view {{ relation }} as ( {{ sql }} ) {{ bind_qualifier }}; - - {# - For late-binding views, it's possible to set comments on the view (though they don't seem to end up anywhere). - Unfortunately, setting comments on columns just results in an error. - #} - {% set relation = relation.incorporate(type='view') %} - {{ set_relation_comment(relation) }} - {% if binding %} - {{ set_column_comments(relation) }} - {% endif %} {% endmacro %} @@ -205,6 +191,19 @@ {% endmacro %} +{% macro redshift__persist_docs(relation, model, for_relation, for_columns) -%} + {% if for_relation and config.persist_relation_docs() %} + {% do run_query(alter_relation_comment(relation, model.description)) %} + {% endif %} + + {# Override: do not set column comments for LBVs #} + {% set is_lbv = config.get('materialized') == 'view' and config.get('bind') == false %} + {% if for_columns and config.persist_column_docs() and not is_lbv %} + {% do run_query(alter_column_comment(relation, model.columns)) %} + {% endif %} +{% endmacro %} + + {% macro redshift__alter_relation_comment(relation, comment) %} {% do return(postgres__alter_relation_comment(relation, comment)) %} {% endmacro %} diff --git a/plugins/snowflake/dbt/include/snowflake/macros/adapters.sql b/plugins/snowflake/dbt/include/snowflake/macros/adapters.sql index 8187a9e0cb2..57eab7daf9e 100644 --- a/plugins/snowflake/dbt/include/snowflake/macros/adapters.sql +++ b/plugins/snowflake/dbt/include/snowflake/macros/adapters.sql @@ -3,9 +3,6 @@ {%- set cluster_by_keys = config.get('cluster_by', default=none) -%} {%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%} {%- set copy_grants = config.get('copy_grants', default=false) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set relation_comment = get_relation_comment(raw_persist_docs, model) -%} - {%- set column_comment = get_relation_column_comments(raw_persist_docs, model) -%} {%- if cluster_by_keys is not none and cluster_by_keys is string -%} {%- set cluster_by_keys = [cluster_by_keys] -%} @@ -39,25 +36,12 @@ {% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%} alter table {{relation}} resume recluster; {%- endif -%} - -- add in comments - - {% set relation = relation.incorporate(type='table') %} - {% if relation_comment is not none -%} - {{ alter_relation_comment(relation, relation_comment) }} - {%- endif -%} - - {% if column_comment is not none -%} - {{ alter_column_comment(relation, column_comment) }} - {%- endif -%} - {% endmacro %} {% macro snowflake__create_view_as(relation, sql) -%} {%- set secure = config.get('secure', default=false) -%} {%- set copy_grants = config.get('copy_grants', default=false) -%} {%- set sql_header = config.get('sql_header', none) -%} - {%- set raw_persist_docs = config.get('persist_docs', {}) -%} - {%- set relation_comment = get_relation_comment(raw_persist_docs, model) -%} {{ sql_header if sql_header is not none }} create or replace {% if secure -%} @@ -65,12 +49,6 @@ {%- endif %} view {{ relation }} {% if copy_grants -%} copy grants {%- endif %} as ( {{ sql }} ); - - {%- set relation = relation.incorporate(type='view') -%} - {% if relation_comment is not none -%} - {{ alter_relation_comment(relation, relation_comment) }} - {%- endif -%} - {% endmacro %} {% macro snowflake__get_columns_in_relation(relation) -%} @@ -184,7 +162,3 @@ {{ column_name }} COMMENT $${{ column_dict[column_name]['description'] | replace('$', '[$]') }}$$ {{ ',' if not loop.last else ';' }} {% endfor %} {% endmacro %} - - - - diff --git a/plugins/snowflake/dbt/include/snowflake/macros/materializations/incremental.sql b/plugins/snowflake/dbt/include/snowflake/macros/materializations/incremental.sql index 92836ec0557..cdb660b0990 100644 --- a/plugins/snowflake/dbt/include/snowflake/macros/materializations/incremental.sql +++ b/plugins/snowflake/dbt/include/snowflake/macros/materializations/incremental.sql @@ -71,6 +71,9 @@ {{ run_hooks(post_hooks, inside_transaction=False) }} + {% set target_relation = target_relation.incorporate(type='table') %} + {% do persist_docs(target_relation, model) %} + {{ return({'relations': [target_relation]}) }} {%- endmaterialization %} diff --git a/plugins/snowflake/dbt/include/snowflake/macros/materializations/table.sql b/plugins/snowflake/dbt/include/snowflake/macros/materializations/table.sql index fdda238120c..84d78781864 100644 --- a/plugins/snowflake/dbt/include/snowflake/macros/materializations/table.sql +++ b/plugins/snowflake/dbt/include/snowflake/macros/materializations/table.sql @@ -30,6 +30,8 @@ {{ run_hooks(post_hooks, inside_transaction=False) }} + {% do persist_docs(target_relation, model) %} + {{ return({'relations': [target_relation]}) }} {% endmaterialization %} diff --git a/plugins/snowflake/dbt/include/snowflake/macros/materializations/view.sql b/plugins/snowflake/dbt/include/snowflake/macros/materializations/view.sql index 6515bd3e5fc..700bb3807bf 100644 --- a/plugins/snowflake/dbt/include/snowflake/macros/materializations/view.sql +++ b/plugins/snowflake/dbt/include/snowflake/macros/materializations/view.sql @@ -1,3 +1,9 @@ {% materialization view, adapter='snowflake' -%} - {{ return(create_or_replace_view()) }} + {% set to_return = create_or_replace_view() %} + + {% set target_relation = this.incorporate(type='view') %} + {% do persist_docs(target_relation, model, for_columns=false) %} + + {% do return(to_return) %} + {%- endmaterialization %} diff --git a/test/integration/060_persist_docs_tests/test_persist_docs.py b/test/integration/060_persist_docs_tests/test_persist_docs.py index f5890741c5f..3dd9f0da140 100644 --- a/test/integration/060_persist_docs_tests/test_persist_docs.py +++ b/test/integration/060_persist_docs_tests/test_persist_docs.py @@ -122,3 +122,30 @@ def test_redshift_late_binding_view(self): view_node = catalog_data['nodes']['model.test.view_model'] self._assert_has_view_comments(view_node, False, False) + + +class TestPersistDocsSimple(BasePersistDocsTest): + @property + def project_config(self): + return { + 'config-version': 2, + 'models': { + 'test': { + '+persist_docs': { + "relation": True, + "columns": True, + }, + 'view_model': { + 'bind': False, + } + } + } + } + + @use_profile('snowflake') + def test_snowflake_persist_docs(self): + self.run_dbt() + + @use_profile('bigquery') + def test_bigquery_persist_docs(self): + self.run_dbt()