Skip to content

apply ClickHouse gold-view migrations via post-install Hook Job - #1480

Merged
cyberantonz merged 1 commit into
constructorfabric:mainfrom
cyberantonz:chart-validation
Jun 25, 2026
Merged

apply ClickHouse gold-view migrations via post-install Hook Job#1480
cyberantonz merged 1 commit into
constructorfabric:mainfrom
cyberantonz:chart-validation

Conversation

@cyberantonz

@cyberantonz cyberantonz commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Problem

After #1428 dropped the bundled L2 subcharts (ClickHouse is now always external), the ClickHouse gold-view migrations (src/ingestion/scripts/migrations/*.sql) stopped being applied on the gitops path:

Net result: both clusters lost their gold views, and nothing owned re-applying them on helm install/upgrade.

Fix

Add a clickhouse-migrate Helm Hook Job (post-install,post-upgrade) that applies the migrations against the external ClickHouse — no new image or CI pipeline needed, because the existing toolbox image already bundles the SQL.

The Job runs apply-ch-migrations.sh, which mirrors the old init.sh contract:

  1. CREATE DATABASE for staging / silver / app db
  2. create-bronze-placeholders.sh — ADR-0007 placeholders so gold-view CREATE VIEW type-checks on a fresh cluster
  3. apply migrations/*.sql in lexicographic order

All over ClickHouse's HTTP interface (no pod to kubectl exec into), via a small lib/ch-exec.sh.

Key decisions

  • Hook timing post-install,post-upgrade: gold-view consumers (analytics-api) resolve view source tables lazily at query time, so views materialising shortly after pod startup is fine.
  • Failure is loud: a failed migration fails — and under the gitops --rollback-on-failure default, rolls back — the release. backoffLimit is low (2) since SQL errors are deterministic, not transient.
  • No migration ledger: every migration re-runs on each upgrade and is CREATE OR REPLACE / IF NOT EXISTS — same idempotent contract as the legacy path.
  • Password never on the command line: auth uses the X-ClickHouse-Key header, not curl -u.
  • Opt-out: gated on clickhouse.runMigrations (default true).

Changes

  • New charts/insight/templates/clickhouse-migrate-job.yaml — the Hook Job.
  • New src/ingestion/scripts/lib/ch-exec.sh — HTTP run_ch / ch_table_exists.
  • New src/ingestion/scripts/apply-ch-migrations.sh — Job entrypoint.
  • create-bronze-placeholders.sh — sourced from the lib; 700 lines of DDL untouched.
  • init.sh / run-init.sh — strip the now-dead ClickHouse section (it targeted the retired StatefulSet).
  • values.yamlclickhouse.runMigrations gate.
  • docs/domain/ingestion/specs/DESIGN.md — realign schema-migration, DB-provisioning, and password-rotation docs to the external-L2 / Hook-Job model (incl. the parallel MariaDB mariadb-init-svcdbs provisioning).
  • e2e migration_applier.py — repoint stale comments (parser logic unchanged).

Validation

  • bash -n on all scripts; offline unit-test of the HTTP statement parser (comments stripped, 59 statements parsed from gold-views.sql).
  • make validate-insight ENV=local → 15/15 resources valid (kubeconform -strict); Job renders with correct hook annotations, env, and backoffLimit: 2.

⚠️ Operational dependency

ingestion.toolboxImage must point at a toolbox build that includes the new scripts/lib/ch-exec.sh + apply-ch-migrations.sh. The CI toolbox rebuild handles that on merge; until then the Job would run an image without these files.

Out of scope

run-init.sh is otherwise orphaned (docs-only) and line 675 of the ingestion DESIGN still references the removed airbyte-toolkit/build-connector.sh — separate pre-existing drift, left for a connector-docs pass.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added automated ClickHouse migration support during Helm install/upgrade, with migrations applied against the external database when enabled.
    • Added local chart validation for Insight, including rendered manifest checks for safer deployment verification.
  • Bug Fixes

    • Updated initialization flow so ClickHouse setup and migrations run in the correct place, reducing startup confusion and avoiding duplicate migration steps.
    • Improved database setup handling to better support external ClickHouse and MariaDB deployments.

@cyberantonz
cyberantonz requested a review from a team as a code owner June 24, 2026 18:53
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@cyberantonz, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 46 minutes and 47 seconds. Learn how PR review limits work.

To continue reviewing without waiting, enable usage-based billing in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 10f9d781-eca0-43d8-ba7f-ec9c92134ded

📥 Commits

Reviewing files that changed from the base of the PR and between 1c3a14ff94e3e591ae959c29ab18e8b1a825ce9d and da378de.

📒 Files selected for processing (9)
  • charts/insight/templates/clickhouse-migrate-job.yaml
  • charts/insight/values.yaml
  • docs/domain/ingestion/specs/DESIGN.md
  • src/ingestion/run-init.sh
  • src/ingestion/scripts/apply-ch-migrations.sh
  • src/ingestion/scripts/create-bronze-placeholders.sh
  • src/ingestion/scripts/init.sh
  • src/ingestion/scripts/lib/ch-exec.sh
  • src/ingestion/tests/e2e/e2e_lib/migration_applier.py
📝 Walkthrough

Walkthrough

Adds a Helm hook Job and helper scripts to run ClickHouse gold-view migrations over HTTP, removes ClickHouse setup from the ingestion bootstrap scripts, updates ingestion design docs, and adds local chart validation in the GitOps Makefile.

Changes

ClickHouse migration hook flow

Layer / File(s) Summary
Chart hook contract
charts/insight/templates/clickhouse-migrate-job.yaml, charts/insight/values.yaml
clickhouse-migrate-job is rendered only when clickhouse.runMigrations and ingestion.toolboxImage are set, and the Job is configured as a post-install/post-upgrade hook with retry and TTL settings.
HTTP migration runner
charts/insight/templates/clickhouse-migrate-job.yaml, src/ingestion/scripts/lib/ch-exec.sh, src/ingestion/scripts/apply-ch-migrations.sh, src/ingestion/scripts/create-bronze-placeholders.sh
The job container runs apply-ch-migrations.sh, which uses lib/ch-exec.sh to create the ClickHouse databases, load bronze placeholders, and apply SQL files over HTTP; create-bronze-placeholders.sh now sources the shared helper.
Bootstrap handoff and docs
src/ingestion/run-init.sh, src/ingestion/scripts/init.sh, src/ingestion/tests/e2e/e2e_lib/migration_applier.py, docs/domain/ingestion/specs/DESIGN.md
run-init.sh and init.sh stop performing ClickHouse setup directly, the e2e migration helper docstrings point to apply-ch-migrations.sh, and the design document updates the ClickHouse, MariaDB, credential, startup, and migration sections.
Local chart validation
deploy/gitops/Makefile
deploy/gitops/Makefile adds LOCAL_CHART, a local-chart-present check, and validate-insight for rendering and validating the local Insight chart.

Sequence Diagram(s)

sequenceDiagram
  participant Helm
  participant clickhouse-migrate-job
  participant apply-ch-migrations.sh
  participant lib/ch-exec.sh
  participant ClickHouse

  Helm->>clickhouse-migrate-job: render post-install,post-upgrade hook
  clickhouse-migrate-job->>apply-ch-migrations.sh: run bash /ingestion/scripts/apply-ch-migrations.sh
  apply-ch-migrations.sh->>lib/ch-exec.sh: call run_ch for SQL statements
  lib/ch-exec.sh->>ClickHouse: POST SQL over HTTP
  ClickHouse-->>lib/ch-exec.sh: query result
  apply-ch-migrations.sh->>ClickHouse: create databases and apply migrations
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • dzarlax

Poem

I hopped through hooks with carrot cheer,
and SQL squeaked over HTTP.
The burrow now runs migrations clean,
then renders charts for all to see.
Thump-thump! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: moving ClickHouse gold-view migrations into a post-install Helm hook job.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…ook Job

Since constructorfabric#1428 dropped the bundled ClickHouse StatefulSet (CH is now always
external L2), nothing applied the gold-view migrations: the host
init.sh path `kubectl exec`'d into a pod that no longer exists, and the
clickhouse-init-svcdbs Hook only runs CREATE DATABASE. Both clusters lost
their migrations.

Add a `clickhouse-migrate` Helm Hook Job (post-install,post-upgrade) that
runs apply-ch-migrations.sh in the toolbox image (which already bundles the
SQL) and dials the external ClickHouse over its HTTP interface — no new
image/CI needed. It creates the staging/silver/app databases + ADR-0007
placeholders, then applies scripts/migrations/*.sql in glob order. A failed
migration fails (and, under gitops --rollback-on-failure, rolls back) the
release — intentional; backoffLimit is low since SQL errors are deterministic.

- lib/ch-exec.sh: HTTP-only run_ch/ch_table_exists (password via the
  X-ClickHouse-Key header, never on the command line).
- create-bronze-placeholders.sh: sourced from the lib; DDL untouched.
- init.sh / run-init.sh: strip the now-dead ClickHouse migration section
  (the kubectl-exec-into-bundled-CH path) — it targeted a retired StatefulSet.
- values.yaml: clickhouse.runMigrations gate (default true).
- DESIGN.md: realign the schema-migrations + DB-provisioning + password
  rotation docs to the external-L2 / Hook-Job model.
- e2e migration_applier.py: repoint stale comments (parser logic unchanged).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Anton Zelenov <antonz@constructor.tech>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@charts/insight/templates/clickhouse-migrate-job.yaml`:
- Around line 60-63: The app.kubernetes.io/name label in the clickhouse-migrate
job can overflow Kubernetes’ 63-character limit because the existing use of
include "insight.fullname" is already truncated before appending the
-clickhouse-migrate suffix. Update the metadata.labels in the clickhouse-migrate
job template to derive the name from .Chart.Name or insight.labels, or apply
truncation after adding the suffix, while keeping the
app.kubernetes.io/component label unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 66adba38-173e-40f3-a2e2-f696763868ed

📥 Commits

Reviewing files that changed from the base of the PR and between d031d8f and 1c3a14ff94e3e591ae959c29ab18e8b1a825ce9d.

📒 Files selected for processing (10)
  • charts/insight/templates/clickhouse-migrate-job.yaml
  • charts/insight/values.yaml
  • deploy/gitops/Makefile
  • docs/domain/ingestion/specs/DESIGN.md
  • src/ingestion/run-init.sh
  • src/ingestion/scripts/apply-ch-migrations.sh
  • src/ingestion/scripts/create-bronze-placeholders.sh
  • src/ingestion/scripts/init.sh
  • src/ingestion/scripts/lib/ch-exec.sh
  • src/ingestion/tests/e2e/e2e_lib/migration_applier.py

Comment thread charts/insight/templates/clickhouse-migrate-job.yaml Outdated
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.

2 participants