diff --git a/CHANGELOG.md b/CHANGELOG.md index 5144ae65e..3d2918d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Improvements * `dbt-core` is now capped at `<2.0`, and `dbt-common` and `agate` are declared as direct dependencies, matching dbt-labs adapters ([#776](https://github.com/ClickHouse/dbt-clickhouse/pull/776)). +* `persist_docs` now warns about columns documented in the schema yml that do not exist in the relation ([#798](https://github.com/ClickHouse/dbt-clickhouse/pull/798)). #### Repository maintenance * The shard-local table name of distributed materializations is now resolved in one place, `clickhouse_local_relation`, instead of being rebuilt inline in each materialization and incremental strategy ([#753](https://github.com/ClickHouse/dbt-clickhouse/pull/753)). diff --git a/dbt/include/clickhouse/macros/persist_docs.sql b/dbt/include/clickhouse/macros/persist_docs.sql index 53885825e..0d4904ddd 100644 --- a/dbt/include/clickhouse/macros/persist_docs.sql +++ b/dbt/include/clickhouse/macros/persist_docs.sql @@ -20,6 +20,7 @@ {%- if for_columns and config.persist_column_docs() and model.columns -%} {% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %} + {% do validate_doc_columns(relation, model.columns, existing_columns) %} {% for column_name in model.columns if (column_name in existing_columns) %} {%- set comment = model.columns[column_name]['description'] -%} {%- if comment %} diff --git a/pyproject.toml b/pyproject.toml index 1c838de59..7512992c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ classifiers = [ ] requires-python = ">=3.10" dependencies = [ - "dbt-adapters>=1.22.0,<1.25.0", # This version should be dbt-adapters<2.0, but keeping it fixed for now to avoid unexpected issues. We need to frequently update it. + "dbt-adapters>=1.22.10,<1.25.0", # This version should be dbt-adapters<2.0, but keeping it fixed for now to avoid unexpected issues. We need to frequently update it. # installed via dbt-adapters but used directly "dbt-common>=1.36,<2.0", "agate>=1.0,<2.0", @@ -45,7 +45,7 @@ Slack = "https://clickhouse.com/slack" [dependency-groups] dev = [ "dbt-core==1.12.*", - "dbt-tests-adapter>=1.10,<2.0", + "dbt-tests-adapter>=1.20,<2.0", "pytest>=7.2.0", "pytest-dotenv==0.5.2", "pytest-timeout==2.4.0", diff --git a/tests/integration/adapter/persist_docs/fixtures.py b/tests/integration/adapter/persist_docs/fixtures.py deleted file mode 100644 index f7179bb1a..000000000 --- a/tests/integration/adapter/persist_docs/fixtures.py +++ /dev/null @@ -1,137 +0,0 @@ -_MODELS__VIEW = """ -{{ config(materialized='view') }} -select 2 as id, 'Bob' as name -""" - -_MODELS__NO_DOCS_MODEL = """ -select 1 as id, 'Alice' as name -""" - -_DOCS__MY_FUN_DOCS = """ -{% docs my_fun_doc %} -name Column description "with double quotes" -and with 'single quotes' as welll as other; -'''abc123''' -reserved -- characters --- -/* comment */ -Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - -{% enddocs %} -""" - -_MODELS__TABLE = """ -{{ config(materialized='table') }} -select 1 as id, 'Joe' as name -""" - - -_MODELS__MISSING_COLUMN = """ -{{ config(materialized='table') }} -select 1 as id, 'Ed' as name -""" - -_MODELS__MODEL_USING_QUOTE_UTIL = """ -select 1 as {{ adapter.quote("2id") }} -""" - -_PROPERTIES__QUOTE_MODEL = """ -version: 2 -models: - - name: quote_model - description: "model to test column quotes and comments" - columns: - - name: 2id - description: "XXX My description" - quote: true -""" - -_PROPERITES__SCHEMA_MISSING_COL = """ -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" -""" - -_PROPERTIES__SCHEMA_YML = """ -version: 2 - -models: - - name: table_model - description: | - Table model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - - name: name - description: | - Some stuff here and then a call to - {{ doc('my_fun_doc')}} - - name: view_model - description: | - View model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - -seeds: - - name: seed - description: | - Seed model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - - name: name - description: | - Some stuff here and then a call to - {{ doc('my_fun_doc')}} -""" - - -_SEEDS__SEED = """id,name -1,Alice -2,Bob -""" diff --git a/tests/integration/adapter/persist_docs/test_persist_docs.py b/tests/integration/adapter/persist_docs/test_persist_docs.py index 58c1ae78c..4f192e6b9 100644 --- a/tests/integration/adapter/persist_docs/test_persist_docs.py +++ b/tests/integration/adapter/persist_docs/test_persist_docs.py @@ -1,215 +1,32 @@ -import json -import os - -import pytest -from dbt.tests.util import run_dbt -from fixtures import ( - _DOCS__MY_FUN_DOCS, - _MODELS__MISSING_COLUMN, - _MODELS__MODEL_USING_QUOTE_UTIL, - _MODELS__NO_DOCS_MODEL, - _MODELS__TABLE, - _MODELS__VIEW, - _PROPERITES__SCHEMA_MISSING_COL, - _PROPERTIES__QUOTE_MODEL, - _PROPERTIES__SCHEMA_YML, - _SEEDS__SEED, +from dbt.tests.adapter.persist_docs.test_persist_docs import ( + BasePersistDocs, + BasePersistDocsAllColumnsMissing, + BasePersistDocsColumnMissing, + BasePersistDocsCommentOnQuotedColumn, + BasePersistDocsQuotedColumnCaseSensitive, + BasePersistDocsQuotedDescriptionNotAppliedOnMismatch, ) -class BasePersistDocsBase: - @pytest.fixture(scope="class", autouse=True) - def setUp(self, project): - run_dbt(["seed"]) - run_dbt() - - @pytest.fixture(scope="class") - def seeds(self): - return {"seed.csv": _SEEDS__SEED} - - @pytest.fixture(scope="class") - def models(self): - return { - "no_docs_model.sql": _MODELS__NO_DOCS_MODEL, - "table_model.sql": _MODELS__TABLE, - "view_model.sql": _MODELS__VIEW, - } - - @pytest.fixture(scope="class") - def properties(self): - return { - "my_fun_docs.md": _DOCS__MY_FUN_DOCS, - "schema.yml": _PROPERTIES__SCHEMA_YML, - } - - def _assert_common_comments(self, *comments): - for comment in comments: - assert '"with double quotes"' in comment - assert """'''abc123'''""" in comment - assert "\n" in comment - assert "Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting" in comment - assert "/* comment */" in comment - if os.name == "nt": - assert "--\r\n" in comment or "--\n" in comment - else: - assert "--\n" in comment - - def _assert_has_table_comments(self, table_node): - table_comment = table_node["metadata"]["comment"] - assert table_comment.startswith("Table model description") - - table_id_comment = table_node["columns"]["id"]["comment"] - assert table_id_comment.startswith("id Column description") - - table_name_comment = table_node["columns"]["name"]["comment"] - assert table_name_comment.startswith("Some stuff here and then a call to") - - self._assert_common_comments(table_comment, table_id_comment, table_name_comment) - - def _assert_has_view_comments( - self, view_node, has_node_comments=True, has_column_comments=True - ): - view_comment = view_node["metadata"]["comment"] - if has_node_comments: - assert view_comment.startswith("View model description") - self._assert_common_comments(view_comment) - else: - assert view_comment is None - - view_id_comment = view_node["columns"]["id"]["comment"] - if has_column_comments: - assert view_id_comment.startswith("id Column description") - self._assert_common_comments(view_id_comment) - else: - assert view_id_comment is None - - view_name_comment = view_node["columns"]["name"]["comment"] - assert view_name_comment is None - - -class BasePersistDocs(BasePersistDocsBase): - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "+persist_docs": { - "relation": True, - "columns": True, - }, - } - } - } - - def test_has_comments_pg_like(self): - if os.environ.get('DBT_CH_TEST_CLOUD', '').lower() in ('1', 'true', 'yes'): - pytest.skip('Not running comment test for cloud') - 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"]) == 4 - table_node = catalog_data["nodes"]["model.test.table_model"] - self._assert_has_table_comments(table_node) - - view_node = catalog_data["nodes"]["model.test.view_model"] - self._assert_has_view_comments(view_node) - - no_docs_node = catalog_data["nodes"]["model.test.no_docs_model"] - self._assert_has_view_comments(no_docs_node, False, False) - - -class BasePersistDocsColumnMissing(BasePersistDocsBase): - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "+persist_docs": { - "columns": True, - }, - } - } - } - - @pytest.fixture(scope="class") - def models(self): - return {"missing_column.sql": _MODELS__MISSING_COLUMN} - - @pytest.fixture(scope="class") - def properties(self): - return {"schema.yml": _PROPERITES__SCHEMA_MISSING_COL} - - def test_missing_column(self, project): - 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") - - -class BasePersistDocsCommentOnQuotedColumn: - """Covers edge case where column with comment must be quoted. - We set this using the `quote:` tag in the property file.""" - - @pytest.fixture(scope="class") - def models(self): - return {"quote_model.sql": _MODELS__MODEL_USING_QUOTE_UTIL} - - @pytest.fixture(scope="class") - def properties(self): - return {"properties.yml": _PROPERTIES__QUOTE_MODEL} - - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "materialized": "table", - "+persist_docs": { - "relation": True, - "columns": True, - }, - } - } - } - - @pytest.fixture(scope="class") - def run_has_comments(self, project): - if os.environ.get('DBT_CH_TEST_CLOUD', '').lower() in ('1', 'true', 'yes'): - pytest.skip('Not running comment test for cloud') - - def fixt(): - run_dbt() - 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"]) == 1 - column_node = catalog_data["nodes"]["model.test.quote_model"] - column_comment = column_node["columns"]["2id"]["comment"] - assert column_comment.startswith("XXX") +class TestPersistDocs(BasePersistDocs): + pass - return fixt - def test_quoted_column_comments(self, run_has_comments): - run_has_comments() +class TestPersistDocsColumnMissing(BasePersistDocsColumnMissing): + pass -""" -Code from tests is a copy from dbt-core/tests/adapter/dbt/tests/adapter/persist_docs -It can be removed after upgrading to 1.5.0 -""" +class TestPersistDocsAllColumnsMissing(BasePersistDocsAllColumnsMissing): + pass -class TestPersistDocs(BasePersistDocs): +class TestPersistDocsQuotedColumnCaseSensitive(BasePersistDocsQuotedColumnCaseSensitive): pass -class TestPersistDocsColumnMissing(BasePersistDocsColumnMissing): +class TestPersistDocsQuotedDescriptionNotAppliedOnMismatch( + BasePersistDocsQuotedDescriptionNotAppliedOnMismatch +): pass