Skip to content

Commit

Permalink
Avoid errors when missing column in yaml doc (#4285)
Browse files Browse the repository at this point in the history
* Update postgres__alter_column_comment

* Update changelog

* Add integration test
  • Loading branch information
kadero authored Nov 16, 2021
1 parent 3d28b67 commit 2241698
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
## dbt-core 1.0.0rc2 (TBD)

### Features
- Avoid error when missing column in YAML description ([#4151](https://github.com/dbt-labs/dbt-core/issues/4151), [#4285](https://github.com/dbt-labs/dbt-core/pull/4285))

### Under the hood
Add --indirect-selection parameter to profiles.yml and builtin DBT_ env vars; stringified parameter to enable multi-modal use ([#3997](https://github.com/dbt-labs/dbt-core/issues/3997), [PR #4270](https://github.com/dbt-labs/dbt-core/pull/4270))
- Fix filesystem searcher test failure on Python 3.9 ([#3689](https://github.com/dbt-labs/dbt-core/issues/3689), [#4271](https://github.com/dbt-labs/dbt-core/pull/4271))

Contributors:
- [@kadero](https://github.com/kadero) ([#4285](https://github.com/dbt-labs/dbt-core/pull/4285))

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

Expand Down
3 changes: 2 additions & 1 deletion plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@


{% macro postgres__alter_column_comment(relation, column_dict) %}
{% for column_name in column_dict %}
{% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %}
{% for column_name in column_dict if (column_name in existing_columns) %}
{% set comment = column_dict[column_name]['description'] %}
{% set escaped_comment = postgres_escape_comment(comment) %}
comment on column {{ relation }}.{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} is {{ escaped_comment }};
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"
29 changes: 29 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 @@ -95,3 +95,32 @@ def run_has_comments_pglike(self):
def test_postgres_comments(self):
self.run_has_comments_pglike()

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('postgres')
def test_postgres_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')

0 comments on commit 2241698

Please sign in to comment.