Skip to content

fix: check the tests schema with a ClickHouse-valid relation (dbt-clickhouse 1.10.4) - #1102

Closed
bim-ba wants to merge 2 commits into
elementary-data:masterfrom
bim-ba:fix/clickhouse-tests-schema-check
Closed

bim-ba wants to merge 2 commits into
elementary-data:masterfrom
bim-ba:fix/clickhouse-tests-schema-check

Conversation

@bim-ba

@bim-ba bim-ba commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

dbt-clickhouse 1.10.4 (released 2026-11-24) removed its Python check_schema_exists override (ClickHouse/dbt-clickhouse#797). The check now goes through SQLAdapter.check_schema_exists, which builds Relation.create(database=..., schema=...), and ClickHouseRelation raises when database is set and differs from schema.

On ClickHouse, get_package_database_and_schema returns [<schema>, <schema>]. In on-run-start, get_elementary_tests_schema probes the legacy tests schema with that database, so every dbt test / dbt build fails before any test runs:

on-run-start
  create_elementary_tests_schema
    get_elementary_tests_schema
      adapter.check_schema_exists("<schema>", "<schema>__tests")
        ClickHouseRelation(database="<schema>", schema="<schema>__tests")
          Runtime Error: Cannot set database <schema> in clickhouse!

Fix: both tests-schema checks go through a dispatched tests_schema_exists. The default keeps adapter.check_schema_exists(database, schema), and the ClickHouse version passes the schema as its own database:

-{% if adapter.check_schema_exists(elementary_database, legacy_tests_schema_name) %}
+{% if elementary.tests_schema_exists(elementary_database, legacy_tests_schema_name) %}

+{% macro clickhouse__tests_schema_exists(database, schema) %}
+    {% do return(adapter.check_schema_exists(schema, schema)) %}
+{% endmacro %}

I did not use the existing schema_exists because its BigQuery and Postgres versions return fixed answers across databases, and that would change behaviour on those adapters.

Evidence

Minimal project: one table model with elementary.schema_changes, dbt-core 1.12.7, ClickHouse 26.10, elementary: +schema: elementary. Steps: dbt run --select elementary, dbt run, dbt test.

package dbt-clickhouse 1.10.3 dbt-clickhouse 1.10.4
master (b7fdff1) PASS=3 Runtime Error: Cannot set database <schema>_elementary in clickhouse!
this branch PASS=3 PASS=3

On this branch with dbt-clickhouse 1.10.4 and an existing <schema>_elementary__tests database, the legacy schema is still detected and used for the test temp tables (PASS=3).

I did not run the repository's integration suite. The latest ClickHouse CI runs on master installed dbt-clickhouse 1.10.3, so they don't exercise 1.10.4 yet.

Not covered here: with a non-empty tests_schema_name var, create_elementary_tests_schema still calls api.Relation.create(database=<schema>, schema=<schema><suffix>), which fails the same way on both adapter versions.

Merge Danger

Door: two-way: macro-only change, and a revert restores the previous behaviour.

Blast Radius: small. Non-ClickHouse adapters run the same adapter.check_schema_exists(database, schema) call as before, only through a dispatch.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved detection of the tests schema across supported database adapters, including ClickHouse.
    • Schema creation and legacy-schema fallback now use consistent schema-existence checks.

dbt-clickhouse 1.10.4 dropped its check_schema_exists override, so the
check now goes through SQLAdapter, which builds a relation from the
given database and schema. Elementary passes its package database
(the Elementary schema on ClickHouse) with the tests schema, and
dbt-clickhouse rejects a relation whose database differs from its
schema, so every `dbt test` fails in on-run-start with
"Cannot set database <schema> in clickhouse!".

Check the tests schema through a dispatched macro that, on ClickHouse,
passes the schema as its own database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

👋 @bim-ba
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in the elementary repository.

@bim-ba
bim-ba requested a deployment to elementary_test_env November 25, 2026 22:53 — with GitHub Actions Waiting
@jonlevine7

jonlevine7 commented Nov 25, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2a4ecfb5-7441-4291-bff3-69cb6628eb6d
Commits

Reviewing files that changed from the base of the PR and between e79c9b2 and c2b3ee5.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The tests-schema creation and legacy-schema fallback now use elementary.tests_schema_exists. The new dispatched macro uses the default (database, schema) check or the ClickHouse (schema, schema) check.

Changes: Tests schema checks

Layer / File(s) Summary
Dispatched schema checks and call sites
macros/edr/tests/test_utils/get_elementary_tests_schema.sql, macros/edr/tests/on_run_start/create_elementary_tests_schema.sql
Adds default and ClickHouse implementations of tests_schema_exists(database, schema). The legacy-schema fallback and schema-creation macro now call elementary.tests_schema_exists.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: haritamar

Merge Risk: Minimal · previous review e79c9

This change fixes tests-schema checks with dbt-clickhouse 1.10.4 and leaves behavior on other adapters unchanged. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the tests-schema check is routed through a dispatched macro so it uses a ClickHouse-valid relation, fixing failures with dbt-clickhouse 1.10.4. The raw summary confirm…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the tests-schema check is routed through a dispatched macro so it uses a ClickHouse-valid relation, fixing failures with dbt-clickhouse 1.10.4. The raw summary confirms the dispatched tests_schema_exists macro and its ClickHouse override, which match the title. The title is 84 characters, slightly long, but it is specific and clear.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6c937ee3-b88b-47f2-8953-9576440b662e
Commits

Reviewing files that changed from the base of the PR and between b7fdff1 and e79c9b2.

Files selected for processing: 2

  • macros/edr/tests/on_run_start/create_elementary_tests_schema.sql
  • macros/edr/tests/test_utils/get_elementary_tests_schema.sql

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@EladBarkay

Copy link
Copy Markdown
Contributor

Thanks for tracking this down and for the fix! We went with a slightly different approach in #1107: on ClickHouse, get_package_database_and_schema will no longer return the schema as the database, so check_schema_exists and the other relation calls work without a ClickHouse-specific branch. We'll close this PR once #1107 is merged.

@EladBarkay

Copy link
Copy Markdown
Contributor

#1107 is now merged, so I'm closing this one. The fix will be in the next dbt package release. Thanks again @bim-ba for reporting this and pointing us to the root cause, it made the fix much easier!

@EladBarkay EladBarkay closed this Dec 1, 2026

This branch was successfully deployed

1 active deployment
elementary_test_env — c2b3ee57 Deployed Nov 27, 2026 by haritamar via approve-fork #5403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants