Skip to content

Commit

Permalink
Add column comments to Snowflake views (#53)
Browse files Browse the repository at this point in the history
* Add column comments to Snowflake views
* Update create_view_as macro to include column comments
* Add test for column-level view comments
* Add testing for column comments with non-lowercase column names

* Organizing adapter macros for column persistence
  • Loading branch information
spencer-taylor-workrise authored Nov 19, 2021
1 parent e1ee1c8 commit 55f478b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

### Fixes
- Apply query tags for Seed and Snapshot materialisations ([#20](https://github.com/dbt-labs/dbt-snowflake/issues/20), [#48](https://github.com/dbt-labs/dbt-snowflake/issues/48))
- Adds column-level comments to Snowflake views ([#17](https://github.com/dbt-labs/dbt-snowflake/issues/17))

### Under the hood
- Resolves an issue caused when the Snowflake OCSP server is not accessible, by exposing the `insecure_mode` boolean avalable in the Snowflake python connector ([#31](https://github.com/dbt-labs/dbt-snowflake/issues/31), [#49](https://github.com/dbt-labs/dbt-snowflake/pull/49))

### Contributors
- [@anthu](https://github.com/anthu) ([#48](https://github.com/dbt-labs/dbt-snowflake/pull/48))
- [@JoshuaHuntley](https://github.com/JoshuaHuntley) ([#49](https://github.com/dbt-labs/dbt-snowflake/pull/49))
- [@spencer-taylor-workrise](https://github.com/spencer-taylor-workrise) ([#17](https://github.com/dbt-labs/dbt-snowflake/issues/17))

## dbt-snowflake 1.0.0rc1 (November 10, 2021)

Expand Down
28 changes: 26 additions & 2 deletions dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@
{%- endif -%}
{% endmacro %}

{% macro get_column_comment_sql(column_name, column_dict) %}
{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} COMMENT $${{ column_dict[column_name]['description'] | replace('$', '[$]') }}$$
{% endmacro %}

{% macro get_persist_docs_column_list(model_columns, query_columns) %}
(
{% for column_name in query_columns %}
{% if (column_name|upper in model_columns) or (column_name in model_columns) %}
{{ get_column_comment_sql(column_name, model_columns) }}
{% else %}
{{column_name}}
{% endif %}
{{ ", " if not loop.last else "" }}
{% endfor %}
)
{% endmacro %}

{% macro snowflake__create_view_as(relation, sql) -%}
{%- set secure = config.get('secure', default=false) -%}
{%- set copy_grants = config.get('copy_grants', default=false) -%}
Expand All @@ -46,7 +63,14 @@
{{ sql_header if sql_header is not none }}
create or replace {% if secure -%}
secure
{%- endif %} view {{ relation }} {% if copy_grants -%} copy grants {%- endif %} as (
{%- endif %} view {{ relation }}
{% if config.persist_column_docs() -%}
{% set model_columns = model.columns %}
{% set query_columns = get_columns_in_query(sql) %}
{{ get_persist_docs_column_list(model_columns, query_columns) }}

{%- endif %}
{% if copy_grants -%} copy grants {%- endif %} as (
{{ sql }}
);
{% endmacro %}
Expand Down Expand Up @@ -158,7 +182,7 @@
{% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %}
alter {{ relation.type }} {{ relation }} alter
{% for column_name in column_dict if (column_name in existing_columns) or (column_name|upper in existing_columns) %}
{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} COMMENT $${{ column_dict[column_name]['description'] | replace('$', '[$]') }}$$ {{ ',' if not loop.last else ';' }}
{{ get_column_comment_sql(column_name, column_dict) }} {{ ',' if not loop.last else ';' }}
{% endfor %}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
select 1 as {{ adapter.quote("2id") }}
select 1 as {{ adapter.quote("2id") }}, 2 as {{ adapter.quote("3iD") }}
4 changes: 3 additions & 1 deletion tests/integration/column_comments_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ models:
- name: 2id
description: "XXX My description"
quote: true

- name: 3iD
description: "XXX Testing for non-lowercase column name"
quote: true
45 changes: 32 additions & 13 deletions tests/integration/column_comments_tests/test_column_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from tests.integration.base import DBTIntegrationTest, use_profile


class TestColumnComment(DBTIntegrationTest):
class BaseTestColumnComment(DBTIntegrationTest):
@property
def schema(self):
return "column_comment"
Expand All @@ -12,6 +11,19 @@ def schema(self):
def models(self):
return "models"

def run_has_comments(self):
self.run_dbt()
self.run_dbt(['docs', 'generate'])
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 1
column_node = catalog_data['nodes']['model.test.quote_model']
for column in column_node['columns'].keys():
column_comment = column_node['columns'][column]['comment']
assert column_comment.startswith('XXX')

class TestColumnCommentInTable(BaseTestColumnComment):
@property
def project_config(self):
return {
Expand All @@ -26,18 +38,25 @@ def project_config(self):
}
}
}
@use_profile('snowflake')
def test_snowflake_comments(self):
self.run_has_comments()

def run_has_comments(self):
self.run_dbt()
self.run_dbt(['docs', 'generate'])
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 1
column_node = catalog_data['nodes']['model.test.quote_model']
column_comment = column_node['columns']['2id']['comment']
assert column_comment.startswith('XXX')

class TestColumnCommentInView(BaseTestColumnComment):
@property
def project_config(self):
return {
'config-version': 2,
'models': {
'test': {
'materialized': 'view',
'+persist_docs': {
"relation": True,
"columns": True,
},
}
}
}
@use_profile('snowflake')
def test_snowflake_comments(self):
self.run_has_comments()

0 comments on commit 55f478b

Please sign in to comment.