Skip to content

Commit

Permalink
Check if a snowflake column exists before altering its comment (#3149)
Browse files Browse the repository at this point in the history
* Check if column exists when altering column comments in snowflake

* Add new test class for persist docs models with missing columns

* Parallel run all integration tests after unit (#3328)

* don't clobber default args

* update changelog

* Update changelog for PR #3149

* Pull in upstream changes

Co-authored-by: Jeremy Cohen <jeremy@fishtownanalytics.com>
Co-authored-by: Kyle Wigley <kyle@fishtownanalytics.com>
  • Loading branch information
3 people committed May 19, 2021
1 parent 920ef85 commit 89cc53d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Fix compiled sql for ephemeral models ([#3317](https://github.com/fishtown-analytics/dbt/issues/3317), [#3318](https://github.com/fishtown-analytics/dbt/pull/3318))
- Now generating `run_results.json` even when no nodes are selected ([#3313](https://github.com/fishtown-analytics/dbt/issues/3313), [#3315](https://github.com/fishtown-analytics/dbt/pull/3315))
- Stop clobbering default keyword arguments for jinja test definitions ([#3329](https://github.com/fishtown-analytics/dbt/issues/3329), [#3340](https://github.com/fishtown-analytics/dbt/pull/3340))
- Update the snowflake adapter to only comment on a column if it exists when using the persist_docs config ([#3039](https://github.com/fishtown-analytics/dbt/issues/3039), [#3149](https://github.com/fishtown-analytics/dbt/pull/3149))

### Under the hood
- Added logic for registry requests to raise a timeout error after a response hangs out for 30 seconds and 5 attempts have been made to reach the endpoint ([#3177](https://github.com/fishtown-analytics/dbt/issues/3177), [#3275](https://github.com/fishtown-analytics/dbt/pull/3275))
Expand All @@ -12,6 +13,7 @@ Contributors:
- [@TeddyCr](https://github.com/TeddyCr) ([#3275](https://github.com/fishtown-analytics/dbt/pull/3275))
- [@panasenco](https://github.com/panasenco) ([#3315](https://github.com/fishtown-analytics/dbt/pull/3315))
- [@peiwangdb](https://github.com/peiwangdb) ([#3344](https://github.com/fishtown-analytics/dbt/pull/3344))
- [@elikastelein](https://github.com/elikastelein) ([#3149](https://github.com/fishtown-analytics/dbt/pull/3149))

## dbt 0.20.0b1 (May 03, 2021)

Expand Down
3 changes: 1 addition & 2 deletions plugins/snowflake/dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@


{% macro snowflake__alter_column_comment(relation, column_dict) -%}
alter {{ relation.type }} {{ relation }} alter
{% for column_name in column_dict %}
{{ 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 ';' }}
comment if exists on column {{ relation }}.{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} is $${{ column_dict[column_name]['description'] | replace('$', '[$]') }}$$;
{% endfor %}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ config(materialized='table') }}
select 1 as id, 'Ed' as name
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
models:
- name: missing_column
columns:
- name: id
description: "test id column description"
- name: column_that_does_not_exist
description: "comment that cannot be created"
35 changes: 35 additions & 0 deletions test/integration/060_persist_docs_tests/test_persist_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,38 @@ def test_bigquery_persist_docs(self):

level_3_column = node['columns']['level_1.level_2.level_3_a']
assert level_3_column['comment'] == "level_3 column description"


class TestPersistDocsColumnMissing(BasePersistDocsTest):
@property
def project_config(self):
return {
'config-version': 2,
'models': {
'test': {
'+persist_docs': {
"columns": True,
},
}
}
}

@property
def models(self):
return 'models-column-missing'

@use_profile('snowflake')
def test_snowflake_missing_column(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

table_node = catalog_data['nodes']['model.test.missing_column']
table_id_comment = table_node['columns']['ID']['comment']
assert table_id_comment.startswith('test id column description')

@use_profile('bigquery')
def test_bigquery_missing_column(self):
self.run_dbt()

0 comments on commit 89cc53d

Please sign in to comment.