DEV Community

AdmilsonCossa
AdmilsonCossa

Posted on Edited on Fully Autonomous

One ClickHouse migration, two different schemas

Both ClickHouse workers reported Finished for the same CREATE TABLE … ON CLUSTER. One had created events with id UInt64. The other still had the table it started with, where id was a String. Which result should a deployment pipeline believe before it retries?

Here is a reproduction with the published Linux/amd64 binary of go-migrate v1.2.1 and ClickHouse 26.11.8.18. The lab has two ClickHouse server processes with distinct server UUIDs, one shard per server, one replica per shard, and one Keeper process. All three run on one machine. Each server owns its local, ordinary MergeTree table. Keeper coordinates the DDL queue, and the go-migrate journal uses KeeperMap. Release binaries · Pinned fixture configuration

One queued CREATE reaches two ClickHouse nodes. Node 1 creates an unsigned integer column. Node 2 keeps its existing string column. The runner keeps the migration dirty.

Give the second node a different starting point

To follow along, use a disposable Linux amd64 host with the v1.2.1 gomigrate binary on PATH, Bash, Git, curl, Python 3, jq, tar and GNU coreutils. The release ships the fixture configuration in its source tree. With the fixture’s loopback ports available, start it in a fresh Bash shell:

set -euo pipefail
git clone --depth 1 --branch v1.2.1 https://github.com/Go-Gen-Ecosystem/go-migrate.git
cd go-migrate
source .github/scripts/adapter-lifecycle.sh
register_fixture clickhouse
source .github/scripts/adapter-fixtures/clickhouse.sh
start_fixture
cd "$fixture_dir"
mkdir migrations
set +e  # The experiment deliberately observes commands with nonzero exit codes.
Enter fullscreen mode Exit fullscreen mode

Keep this shell open for the walkthrough. Its exit trap retires the fixture. To open a SQL prompt on either node, run "$clickhouse_binary" client --port 19001 or "$clickhouse_binary" client --port 19002. Return to Bash between prompts. Run this first statement locally on each node:

CREATE DATABASE ddl_lab ENGINE = Atomic;
Enter fullscreen mode Exit fullscreen mode

Then run this only on port 19002:

CREATE TABLE ddl_lab.events (id String)
ENGINE = MergeTree ORDER BY id;
Enter fullscreen mode Exit fullscreen mode

The migration we want to deploy instead declares an unsigned integer. Put these files in an otherwise empty migrations/ directory.

-- 001_events.up.sql
CREATE TABLE events ON CLUSTER gomigrate_fixture (id UInt64)
ENGINE = MergeTree ORDER BY id;
Enter fullscreen mode Exit fullscreen mode
-- 001_events.down.sql
DROP TABLE events ON CLUSTER gomigrate_fixture SYNC;
Enter fullscreen mode Exit fullscreen mode

The Down file completes the catalog. The experiment executes only the Up direction. Dropping the table on both nodes would also target the table that existed before this migration.

Review the plan and apply it once

The shell examples use the release binary on PATH and jq to select JSON fields. Run them from the parent of migrations/ in the fixture’s loopback network.

export GOMIGRATE_DSN='clickhouse://default@127.0.0.1:19001/ddl_lab'
gm() { gomigrate --driver clickhouse --cluster gomigrate_fixture --dir migrations -o json "$@"; }

gm bootstrap
plan=$(gm up --sql)
printf '%s\n' "$plan" | jq -r '.data.migrations[0].statements[]'
plan_id=$(printf '%s\n' "$plan" | jq -er '.data.plan_id')
# After reviewing the SQL above:
gm up --expect-plan "$plan_id"
Enter fullscreen mode Exit fullscreen mode

The preview qualified the table name and assigned a UUID. Its statement in this run was:

CREATE TABLE `ddl_lab`.`events` UUID '67b7234f-f64a-895a-a5ec-88495d13f14a' ON CLUSTER `gomigrate_fixture` (id UInt64) ENGINE = MergeTree ORDER BY id
Enter fullscreen mode Exit fullscreen mode

The apply command exited 1 with error code dirty. Its message was:

ClickHouse migration claim remains unresolved: distributed DDL failed (target_failed)
Enter fullscreen mode Exit fullscreen mode

Read the queue and the schema independently

First obtain the queue entry from the outstanding migration claim. We read it through node 2, using a fresh CLI process. This ties the investigation to the operation that go-migrate actually submitted.

export GOMIGRATE_DSN='clickhouse://default@127.0.0.1:19002/ddl_lab'
gm inspect-claim | jq '.data.claim | {version, direction, entry: .ddl.entry, state: .ddl.state}'
Enter fullscreen mode Exit fullscreen mode

Our entry was query-0000000001. Substitute the entry returned by your run. We issued the following query on node 1:

SELECT entry, host, port, status, exception_code
FROM system.distributed_ddl_queue
WHERE cluster = 'gomigrate_fixture' AND entry = 'query-0000000001'
ORDER BY host, port;
Enter fullscreen mode Exit fullscreen mode
entry host port status exception_code
query-0000000001 127.0.0.1 19001 Finished 0
query-0000000001 127.0.0.1 19002 Finished 57

Both workers reported Finished. To see why one worker failed, we read its exception from the same entry:

SELECT port, splitByChar('\n', ifNull(exception_text, ''))[1] AS first_line
FROM system.distributed_ddl_queue
WHERE cluster = 'gomigrate_fixture'
  AND entry = 'query-0000000001'
  AND exception_code != 0;
Enter fullscreen mode Exit fullscreen mode

The row for port 19002 contained this first line:

Code: 57. DB::Exception: Table ddl_lab.events already exists. (TABLE_ALREADY_EXISTS) (version 26.11.8.18 (official build))
Enter fullscreen mode Exit fullscreen mode

The existing table name caused the rejection. We chose different column types so we could identify each schema afterwards. ClickHouse exposes the worker status and exception code as separate fields. Distributed DDL queue reference

Next, connect directly to each native port and run the same local query. We ran it before the migration, after apply, and again after retry and resume.

SELECT name, type
FROM system.columns
WHERE database = 'ddl_lab' AND table = 'events' AND name = 'id';
Enter fullscreen mode Exit fullscreen mode
Native endpoint Before apply After apply After retry and resume
127.0.0.1:19001 zero rows id · UInt64 id · UInt64
127.0.0.1:19002 id · String id · String id · String

This is the partial change. A schema check on node 1 alone would have found exactly what the migration requested. On node 2, even a simple table-existence check would have passed. Checking the column type on both endpoints exposed the difference. ClickHouse column metadata

Did a retry enqueue another change?

gm status returned dirty: true and pending: 0. The migration list marked version 1 as failed, and the response omitted the current field. Zero pending migrations would have been a misleading deployment check on its own. With the same reviewed plan ID, we tried the following separately from node 2:

gm up --expect-plan "$plan_id"
gm resume
Enter fullscreen mode Exit fullscreen mode

Each invocation exited 1 with code dirty. A fresh inspect-claim returned the same claim token and queue entry. We also counted distinct queue entries for the test table, so a second submission with a new entry ID would have shown up. This count uses a text filter only within our isolated database, which had no other writer or concurrent DDL. For diagnosis on a shared cluster, use the exact claim and entry as above.

SELECT uniqExact(entry) AS tasks
FROM system.distributed_ddl_queue
WHERE cluster = 'gomigrate_fixture'
  AND position(query, 'ddl_lab') > 0
  AND position(query, 'events') > 0;
Enter fullscreen mode Exit fullscreen mode
Before apply After failed apply After approved retry After resume
0 1 1 1

As a control, we ran the same catalog against a second database, ddl_control, on the same two servers. Both started without events. This time apply exited 0, both nodes had id UInt64, and go-migrate reported current version 1 with dirty: false.

What I would check during a deployment

I would keep the plan ID and queue entry, compare the worker result on every expected endpoint, and read the actual table definition on any node that disagreed. Before choosing a repair, I would establish who owns the pre-existing table and what data it contains. In this experiment, resume preserved the evidence of a failed operation. It did not reconcile the different column types.

We ran one conflict case and one clean control on a self-managed cluster. Queue evidence has a retention window. The observations apply to ordinary MergeTree tables while Keeper was available and the queue entry was retained. Replicated tables, Keeper quorum loss and the managed ClickHouse Cloud adapter require separate experiments. ClickHouse distributed DDL

Reproduce the versioned regression. After exiting the walkthrough shell, return to the root of the clean v1.2.1 checkout. With Go 1.26.11 and a C toolchain for the race detector installed, run the repository’s ClickHouse conformance fixture. It provisions the two servers and Keeper, runs the native tests, and retires them on exit. Its test database and table names are generated per run. The walkthrough above used the published CLI and fixed names to make the observations readable.

GOMIGRATE_NATIVE_SOURCE_SHA="$(git rev-parse HEAD)" bash .github/scripts/adapter-conformance.sh clickhouse
Enter fullscreen mode Exit fullscreen mode

Partial-failure regression · CLI integration test

I maintain go-migrate. AI assisted with the experiment harness, diagram and draft.

Top comments (0)