From df40f138f2ee427b171981fcc722c3ab00965df5 Mon Sep 17 00:00:00 2001 From: Abdelkader Kouhli Date: Tue, 16 Nov 2021 11:24:28 +0100 Subject: [PATCH 1/3] Update postgres__alter_column_comment --- plugins/postgres/dbt/include/postgres/macros/adapters.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/postgres/dbt/include/postgres/macros/adapters.sql b/plugins/postgres/dbt/include/postgres/macros/adapters.sql index 3a0e7e57329..c8f9cd3bd1a 100644 --- a/plugins/postgres/dbt/include/postgres/macros/adapters.sql +++ b/plugins/postgres/dbt/include/postgres/macros/adapters.sql @@ -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 }}; From 7c90bcf0ded49681057648e687531cb9ec9c5e64 Mon Sep 17 00:00:00 2001 From: Abdelkader Kouhli Date: Tue, 16 Nov 2021 12:10:32 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9f2741a2e1..d6fcca7b20c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From 8c805a5cf445deecb331eadd77991fb1cc7f8f91 Mon Sep 17 00:00:00 2001 From: Abdelkader Kouhli Date: Tue, 16 Nov 2021 13:03:31 +0100 Subject: [PATCH 3/3] Add integration test --- .../models-column-missing/missing_column.sql | 2 ++ .../models-column-missing/schema.yaml | 8 +++++ .../test_persist_docs.py | 29 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 test/integration/060_persist_docs_tests/models-column-missing/missing_column.sql create mode 100644 test/integration/060_persist_docs_tests/models-column-missing/schema.yaml diff --git a/test/integration/060_persist_docs_tests/models-column-missing/missing_column.sql b/test/integration/060_persist_docs_tests/models-column-missing/missing_column.sql new file mode 100644 index 00000000000..642b0f14a19 --- /dev/null +++ b/test/integration/060_persist_docs_tests/models-column-missing/missing_column.sql @@ -0,0 +1,2 @@ +{{ config(materialized='table') }} +select 1 as id, 'Ed' as name diff --git a/test/integration/060_persist_docs_tests/models-column-missing/schema.yaml b/test/integration/060_persist_docs_tests/models-column-missing/schema.yaml new file mode 100644 index 00000000000..aa7b4f88820 --- /dev/null +++ b/test/integration/060_persist_docs_tests/models-column-missing/schema.yaml @@ -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" 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 a2e298294f0..89fecf6383e 100644 --- a/test/integration/060_persist_docs_tests/test_persist_docs.py +++ b/test/integration/060_persist_docs_tests/test_persist_docs.py @@ -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')