From 09a15f6ba7b2e88c00e51a207aece2bc64c4760e Mon Sep 17 00:00:00 2001 From: morsapaes Date: Fri, 17 May 2024 12:57:19 +0200 Subject: [PATCH 1/5] Support persisting docs for materialized_view materializations --- dbt/include/postgres/macros/adapters.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dbt/include/postgres/macros/adapters.sql b/dbt/include/postgres/macros/adapters.sql index 294443be..61565f04 100644 --- a/dbt/include/postgres/macros/adapters.sql +++ b/dbt/include/postgres/macros/adapters.sql @@ -196,7 +196,12 @@ {% macro postgres__alter_relation_comment(relation, comment) %} {% set escaped_comment = postgres_escape_comment(comment) %} - comment on {{ relation.type }} {{ relation }} is {{ escaped_comment }}; + {% if relation.type = 'materialized_view' -%} + {% set relation_type = "materialized view" %} + {%- else -%} + {%- set relation_type = relation.type -%} + {%- endif -%} + comment on {{ relation_type }} {{ relation }} is {{ escaped_comment }}; {% endmacro %} From 4c781ddc37e38f6a12066579a4ac60807be88891 Mon Sep 17 00:00:00 2001 From: morsapaes Date: Wed, 26 Jun 2024 16:40:22 +0200 Subject: [PATCH 2/5] Add changelog entry --- .changes/unreleased/Fixes-20240626-163930.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240626-163930.yaml diff --git a/.changes/unreleased/Fixes-20240626-163930.yaml b/.changes/unreleased/Fixes-20240626-163930.yaml new file mode 100644 index 00000000..37fcc56f --- /dev/null +++ b/.changes/unreleased/Fixes-20240626-163930.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix `persist_docs` for `materialized_view` materializations. Previously, using this configuration with materialized view models would lead to an error. +time: 2024-06-26T16:39:30.455995+02:00 +custom: + Author: morsapaes + Issue: "120" From 0064c00b5011777d7d1e557ec613a793a3924d93 Mon Sep 17 00:00:00 2001 From: morsapaes Date: Sat, 29 Jun 2024 15:47:47 +0200 Subject: [PATCH 3/5] Fix equality operator --- dbt/include/postgres/macros/adapters.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/include/postgres/macros/adapters.sql b/dbt/include/postgres/macros/adapters.sql index 61565f04..1d20e6b3 100644 --- a/dbt/include/postgres/macros/adapters.sql +++ b/dbt/include/postgres/macros/adapters.sql @@ -196,7 +196,7 @@ {% macro postgres__alter_relation_comment(relation, comment) %} {% set escaped_comment = postgres_escape_comment(comment) %} - {% if relation.type = 'materialized_view' -%} + {% if relation.type == 'materialized_view' -%} {% set relation_type = "materialized view" %} {%- else -%} {%- set relation_type = relation.type -%} From 350cbf92f19979c7e26520351925dd068ba79bac Mon Sep 17 00:00:00 2001 From: Colin Date: Wed, 3 Jul 2024 16:41:48 -0700 Subject: [PATCH 4/5] add functional test to persist docs updates --- .../shared_tests/test_persist_docs.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/functional/shared_tests/test_persist_docs.py b/tests/functional/shared_tests/test_persist_docs.py index 2653ca4a..2c9cd29a 100644 --- a/tests/functional/shared_tests/test_persist_docs.py +++ b/tests/functional/shared_tests/test_persist_docs.py @@ -1,8 +1,29 @@ +import pytest +import json + +from dbt.tests.adapter.materialized_view import files from dbt.tests.adapter.persist_docs.test_persist_docs import ( BasePersistDocs, BasePersistDocsColumnMissing, BasePersistDocsCommentOnQuotedColumn, ) +from tests.functional.utils import run_dbt + +_MATERIALIZED_VIEW_PROPERTIES__SCHEMA_YML = """ +version: 2 + +models: + - name: my_materialized_view + description: | + Materialized view model description "with double quotes" + and with 'single quotes' as welll as other; + '''abc123''' + reserved -- characters + 80% of statistics are made up on the spot + -- + /* comment */ + Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting +""" class TestPersistDocs(BasePersistDocs): @@ -15,3 +36,30 @@ class TestPersistDocsColumnMissing(BasePersistDocsColumnMissing): class TestPersistDocsCommentOnQuotedColumn(BasePersistDocsCommentOnQuotedColumn): pass + + +class TestPersistDocsWithMaterializedView(BasePersistDocs): + @pytest.fixture(scope="class", autouse=True) + def seeds(self): + return {"my_seed.csv": files.MY_SEED} + @pytest.fixture(scope="class") + def models(self): + return { + "my_materialized_view.sql": files.MY_MATERIALIZED_VIEW, + } + + @pytest.fixture(scope="class") + def properties(self): + return { + "schema.yml": _MATERIALIZED_VIEW_PROPERTIES__SCHEMA_YML, + } + + def test_has_comments_pglike(self, project): + 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"]) == 2 + view_node = catalog_data["nodes"]["model.test.my_materialized_view"] + assert view_node["metadata"]["comment"].startswith("Materialized view model description") + From 6b1774ba48c0519bba4e9b8f28af45aacd6cb84d Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 8 Jul 2024 10:29:59 -0700 Subject: [PATCH 5/5] formatting --- tests/functional/shared_tests/test_persist_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/shared_tests/test_persist_docs.py b/tests/functional/shared_tests/test_persist_docs.py index 2c9cd29a..f1ad342b 100644 --- a/tests/functional/shared_tests/test_persist_docs.py +++ b/tests/functional/shared_tests/test_persist_docs.py @@ -42,6 +42,7 @@ class TestPersistDocsWithMaterializedView(BasePersistDocs): @pytest.fixture(scope="class", autouse=True) def seeds(self): return {"my_seed.csv": files.MY_SEED} + @pytest.fixture(scope="class") def models(self): return { @@ -62,4 +63,3 @@ def test_has_comments_pglike(self, project): assert len(catalog_data["nodes"]) == 2 view_node = catalog_data["nodes"]["model.test.my_materialized_view"] assert view_node["metadata"]["comment"].startswith("Materialized view model description") -