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) 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 }}; 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')