From c02466f3db6bb4bd43391335b5e720902e571339 Mon Sep 17 00:00:00 2001 From: Can Bekleyici Date: Thu, 7 May 2026 13:33:58 +0200 Subject: [PATCH] fix: missing column comments after ALTER VIEW AS Signed-off-by: Can Bekleyici --- CHANGELOG.md | 1 + .../macros/relations/view/alter.sql | 6 ++++ tests/functional/adapter/views/test_views.py | 31 +++++++++++++++++++ .../unit/macros/relations/test_view_macros.py | 10 ++++++ 4 files changed, 48 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11f4aa35c..5abeb716b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Warn when `contract.enforced: true` is set on a `materialized_view` model ([#1279](https://github.com/databricks/dbt-databricks/issues/1279)) - Fix `materialized_view` models with `databricks_tags` silently going stale on `dbt run`. `MaterializedViewAPI._describe_relation` was not fetching `information_schema.tags`, so existing tags always parsed as empty, producing a spurious tag diff that routed the materialization to `ALTER ... SET TAGS` instead of `REFRESH MATERIALIZED VIEW` ([#1419](https://github.com/databricks/dbt-databricks/issues/1419)) - Fix `dbt docs generate` failing with `RuntimeError: Tables contain columns with the same names ... but different types` during catalog merge across schemas ([#1392](https://github.com/databricks/dbt-databricks/issues/1392)) +- Fix column comments being permanently dropped from views when `view_update_via_alter` issues `ALTER VIEW AS`; reapply persisted column comments after the query update ([#1357](https://github.com/databricks/dbt-databricks/issues/1357)) ## dbt-databricks 1.11.7 (Apr 17, 2026) diff --git a/dbt/include/databricks/macros/relations/view/alter.sql b/dbt/include/databricks/macros/relations/view/alter.sql index 43c03acba..55cc10b0f 100644 --- a/dbt/include/databricks/macros/relations/view/alter.sql +++ b/dbt/include/databricks/macros/relations/view/alter.sql @@ -16,6 +16,12 @@ {% endif %} {% if query %} {{ alter_query(target_relation, query.query) }} + {% if config.persist_column_docs() and model.columns %} + {#-- ALTER VIEW AS wipes all column comments, so reapply them here. --#} + {%- set existing_columns = adapter.get_columns_in_relation(target_relation) -%} + {%- set columns_to_persist = adapter.get_persist_doc_columns(existing_columns, model.columns) -%} + {{ alter_column_comment(target_relation, columns_to_persist) }} + {% endif %} {% endif %} {% if column_comments %} {{ alter_column_comments(target_relation, column_comments.comments) }} diff --git a/tests/functional/adapter/views/test_views.py b/tests/functional/adapter/views/test_views.py index 20baaed5f..a4248e9fa 100644 --- a/tests/functional/adapter/views/test_views.py +++ b/tests/functional/adapter/views/test_views.py @@ -89,6 +89,21 @@ def test_view_update_with_column_comments(self, project): assert results[0][2] == "This is an id column" +class BaseUpdateQueryPreservesColumnComments(BaseUpdateView): + """Regression for #1357: ALTER VIEW AS wipes column comments; they must be reapplied.""" + + def test_view_update_query_preserves_column_comments(self, project): + util.run_dbt(["build"]) + util.write_file(fixtures.altered_view_sql, "models", "initial_view.sql") + util.run_dbt(["run"]) + + results = project.run_sql( + "describe extended {database}.{schema}.initial_view", + fetch="all", + ) + assert results[0][2] == "This is the id column" + + class BaseRemoveTags(BaseUpdateView): def test_view_update_remove_tags(self, project): util.run_dbt(["build"]) @@ -191,6 +206,22 @@ def project_config_update(self): } +@pytest.mark.skip_profile("databricks_cluster") +class TestUpdateViewViaAlterQueryPreservesColumnComments(BaseUpdateQueryPreservesColumnComments): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "flags": {"use_materialization_v2": True}, + "models": { + "+view_update_via_alter": True, + "+persist_docs": { + "relation": True, + "columns": True, + }, + }, + } + + @pytest.mark.skip_profile("databricks_cluster") class TestUpdateViewViaAlterRemoveTags(BaseRemoveTags): @pytest.fixture(scope="class") diff --git a/tests/unit/macros/relations/test_view_macros.py b/tests/unit/macros/relations/test_view_macros.py index c2c3fd9ae..6f37905e0 100644 --- a/tests/unit/macros/relations/test_view_macros.py +++ b/tests/unit/macros/relations/test_view_macros.py @@ -51,6 +51,7 @@ def mocks(self, context): context["apply_tags"] = Mock() context["apply_tblproperties"] = Mock() context["alter_query"] = Mock() + context["alter_column_comment"] = Mock() def render_alter_view(self, template_bundle, changes): return self.run_macro( @@ -83,3 +84,12 @@ def test_macros__alter_view_with_query(self, context, template_bundle): context["apply_tags"].assert_not_called() context["apply_tblproperties"].assert_not_called() context["alter_query"].assert_called_once() + + def test_macros__alter_view_with_query_reapplies_column_comments( + self, context, template_bundle + ): + context["config"].persist_column_docs = Mock(return_value=True) + context["model"].columns = {"id": Mock()} + self.render_alter_view(template_bundle, {"query": Mock()}) + context["alter_query"].assert_called_once() + context["alter_column_comment"].assert_called_once()