Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions dbt/include/databricks/macros/relations/view/alter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
{% endif %}
{% if query %}
{{ alter_query(target_relation, query.query) }}
{% if config.persist_column_docs() and model.columns %}
{#-- ALTER VIEW AS <query> 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) }}
Expand Down
31 changes: 31 additions & 0 deletions tests/functional/adapter/views/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/macros/relations/test_view_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()