Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: alter table description without full_refresh #1139

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

kiwamizamurai
Copy link

@kiwamizamurai kiwamizamurai commented Mar 14, 2024

resolves #1138
docs dbt-labs/docs.getdbt.com/#

Problem

when materialization is incremental, table description does not be updated.

Solution

I implemented the method that alters the table description without table recreation.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX

Copy link

cla-bot bot commented Mar 14, 2024

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @kiwamizamurai

@kiwamizamurai kiwamizamurai marked this pull request as draft March 15, 2024 12:33
@kiwamizamurai kiwamizamurai marked this pull request as ready for review March 15, 2024 12:33
@kiwamizamurai
Copy link
Author

@cla-bot
cla has been signed

@kiwamizamurai
Copy link
Author

@colin-rogers-dbt
hello. this is my frist pr. please help me.
is there anything else i should do?

@cla-bot cla-bot bot added the cla:yes label Mar 19, 2024
@kiwamizamurai
Copy link
Author

Changelog file might be required?

@dataders
Copy link
Contributor

see #1138 (comment)

@kiwamizamurai
Copy link
Author

kiwamizamurai commented Mar 28, 2024

@dataders

Could you review my PR?
I have verified in my local environment with the same step.

@@ -99,6 +99,7 @@
{% macro bigquery__persist_docs(relation, model, for_relation, for_columns) -%}
{% if for_columns and config.persist_column_docs() and model.columns %}
{% do alter_column_comment(relation, model.columns) %}
{% do adapter.update_table_description(relation, model.description) %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this is within the if that has to do with column comments.

perhaps there needs to be a new if that is something like the below. that said, I'm very confused about the purpose of for_relation and for_columns

Suggested change
{% do adapter.update_table_description(relation, model.description) %}
{% if for_relation and config.persist_relation_docs() and model.description %}
{% do adapter.update_table_description(relation, model.description) %}
{% endif %}

Actually it looks like the seed materialization is already doing something very similar.

{% if config.persist_relation_docs() and 'description' in model %}
{{ adapter.update_table_description(model['database'], model['schema'], model['alias'], model['description']) }}
{% endif %}

Copy link
Author

@kiwamizamurai kiwamizamurai Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dataders
Thank you for your comment.

I modified the implementation to use an existing method in the seed materialization.

Copy link
Author

@kiwamizamurai kiwamizamurai Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dataders
let me remind you
In my local environment, I checked that the code works fine.

However, I'm not sure how to write the test, could you please help me?

import pytest
from dbt.tests.util import relation_from_name, get_connection, run_dbt

from dbt.adapters.bigquery import BigQueryRelation

_TABLE_DESCRIPTION_MODEL = """{{
  config(
    materialized='incremental',
    persist_docs={ 'relation': true }
  )
}}

select 1 as first_col
from unnest([struct(1 as dual)]) as dual

{% if is_incremental() %}
    where 1=1
{% endif %}
"""
_TABLE_DESCRIPTION_MODEL_NAME = "table_description_model"
_TABLE_DESCRIPTION = "this is not a field"
_TABLE_DESCRIPTION_MODEL_YML = """
version: 2

models:
- name: table_description_model
  description: '{{ var("table_description") }}'
"""


class TestBigqueryUpdateTableDescription:
    @pytest.fixture(scope="class")
    def project_config_update(self):
        return {"config-version": 2, "vars": {"table_description": _TABLE_DESCRIPTION}}

    @pytest.fixture(scope="class")
    def models(self):
        return {
            f"{_TABLE_DESCRIPTION_MODEL_NAME}.sql": _TABLE_DESCRIPTION_MODEL,
            "schema.yml": _TABLE_DESCRIPTION_MODEL_YML,
        }

    def test_bigquery_update_table_description(self, project):
        results = run_dbt(["run"])
        assert len(results) == 1

        # TODO update docs
        results = run_dbt(["run"])
        assert len(results) == 1
        relation: BigQueryRelation = relation_from_name(
            project.adapter, _TABLE_DESCRIPTION_MODEL_NAME
        )
        adapter = project.adapter
        with get_connection(project.adapter) as conn:
            table = conn.handle.get_table(
                adapter.connections.get_bq_table(
                    relation.database, relation.schema, relation.table
                )
            )
            assert table.description == _TABLE_DESCRIPTION

@kiwamizamurai kiwamizamurai requested a review from dataders March 29, 2024 01:13
@kiwamizamurai kiwamizamurai requested a review from a team as a code owner May 8, 2024 23:29
@martinazapletalova
Copy link

Hi @kiwamizamurai any updates on this PR?

@kiwamizamurai
Copy link
Author

@colin-rogers-dbt
@dataders
could you review this pr?

@mikealfare mikealfare added the community This PR is from a community member label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes community This PR is from a community member ok to test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Support the updating of incremental model description after initial creation
5 participants