Skip to main content

ClickHouse Migrations: Schema as Code with Atlas

ClickHouse, one of the prominent columnar databases, is designed for real-time analytics, providing exceptional speed and efficiency in handling large datasets, but managing its schema can be a puzzle.

If your schema contains a handful of tables that rarely change, you're probably not going to feel much of this pain. But for mission-critical applications, managing complex and interconnected schemas quickly without breaking things becomes difficult.

Enter: Atlas​

Atlas helps developers manage their database schema as code, abstracting away the intricacies of database schema management. With Atlas, users provide the desired state of the database schema and Atlas automatically plans the required migrations.

In this guide, we will set up Atlas for declarative and versioned ClickHouse schema migration and walk through both workflows:

WorkflowWhat Atlas doesCommand
DeclarativeDiffs your desired schema against the live database and applies the plan directlyatlas schema apply
VersionedWrites a timestamped migration file to review, commit, and deploy through CI/CDatlas migrate diff

Either way the schema is a file in your repository, written in HCL or plain SQL. It covers tables and their engines, partitioning, TTLs and codecs, views and materialized views, dictionaries, UDFs, and access control: see Supported Objects in ClickHouse for the full list.

Prerequisites​

  1. Docker
  2. Atlas installed on your machine:

To download and install the latest release of the Atlas CLI, simply run the following in your terminal:

curl -sSf https://atlasgo.sh | sh
  1. Optional, for coding agents: the atlas-onboard and atlas-clickhouse skills. One command installs both, with the other Atlas skills:

Install the skills from a terminal. It installs the Atlas plugin, with all its skills, for every project on this machine.

claude plugin marketplace add ariga/atlas
claude plugin install atlas@ariga

Then start the onboarding skill in Claude Code. See Claude Code with Atlas for the other ways to use the skills.

/atlas:atlas-onboard

Logging in to Atlas​

ClickHouse support is available to Pro users. To use this feature, run atlas login.

To use ClickHouse with Atlas, you'll need to log in to Atlas. If it's your first time, you will be prompted to create both an account and a workspace (organization):

atlas login

Getting Started​

note

If you already have a database in ClickHouse Cloud, follow our ClickHouse Cloud guide for setting up the connection, a dev database, and the project's atlas.hcl. You can then go straight to the Inspecting our Database section, using the URL from Step 3 for this guide.

Let's start by spinning up a local database using Docker:

docker run --rm -d --name atlas-demo -e CLICKHOUSE_DB=demo -e CLICKHOUSE_PASSWORD=pass -p 9000:9000 clickhouse/clickhouse-server:latest

For this example, we will begin with a minimal database containing a users table with id as the primary key.

CREATE TABLE `users` (
`id` UInt64,
`name` String NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MergeTree() ORDER BY id;

To create this in our local database, run the following command:

docker exec -it atlas-demo clickhouse-client --query 'CREATE TABLE demo.users (id UInt64, name String NOT NULL, PRIMARY KEY (id)) ENGINE = MergeTree() ORDER BY id'

Inspecting our Database​

The atlas schema inspect command takes a URL and outputs its schema definition. In this guide, we will demonstrate this flow using both the Atlas DDL (default) and SQL formats for the output.

To inspect our locally-running ClickHouse instance, use the -u flag and write the output to a file named schema.hcl:

atlas schema inspect -u "clickhouse://default:pass@localhost:9000/demo" > schema.hcl

Open the schema.hcl file to view the Atlas schema that describes our database.

schema.hcl
table "users" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "name" {
null = false
type = String
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
schema "demo" {
engine = Atomic
}

This first block represents a table resource with id and name columns. The schema field references the demo schema that is defined in the block below. The primary_key sub-block establishes the id column as the primary key for the table.

Atlas mimics the syntax of the database that the user is working against. In this case, the type for the id column is UInt64, and the name column's is String.

info

To learn more about using the atlas schema inspect command, such as inspecting specific schemas, handling multiple schemas concurrently, excluding tables, and more, see Inspecting Existing Database Schemas.

To generate a visual representation of our schema as an Entity Relationship Diagram (ERD) on Atlas Cloud, we can add the -w flag to the inspect command:

atlas schema inspect -u "clickhouse://default:pass@localhost:9000/demo" -w

Entity relationship diagram of ClickHouse schema (users)

Declarative Migrations​

Using the declarative approach, users define the desired state of their database schema as code. Atlas then inspects the target database's current state and calculates an execution plan to reach the desired state.

We will start off by making a change to our schema file by adding a repos table:

schema.hcl
table "users" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "name" {
null = false
type = String
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
table "repos" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "name" {
null = false
type = String
}
column "owner_id" {
null = false
type = Int64
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
schema "demo" {
engine = Atomic
}

Now that our desired state has changed, we run atlas schema apply to get Atlas to plan the migration that will apply these changes to our database:

atlas schema apply \
-u "clickhouse://default:pass@localhost:9000/demo" \
--to file://schema.hcl \
--dev-url "docker://clickhouse/latest/dev"

Approve the proposed changes, and that's it! You have successfully run a declarative migration.

Applying approved migration (1 statement in total):

-- create "repos" table
-> CREATE TABLE `repos` (
`id` UInt64,
`name` String,
`owner_id` Int64
) ENGINE = MergeTree
PRIMARY KEY (`id`) ORDER BY (`id`) SETTINGS index_granularity = 8192;
-- ok (7.641333ms)

-------------------------
-- 8.875833ms
-- 1 migration
-- 1 sql statement

The plan has one statement, CREATE TABLE repos. The users table already matches the desired state, so Atlas leaves it unchanged.

info

For a more detailed description of the atlas schema apply command, see Declarative Schema Migrations.

To ensure that the changes have been made to the schema, let's run the inspect command with the -w flag once more and view the ERD:

Entity relationship diagram of ClickHouse schema (users and repos)

Pushing schemas to Atlas​

Similar to how Docker images are pushed to Docker Hub, you can push your schema to Atlas Cloud for versioning, collaboration, and deployment:

atlas schema push app \
--url file://schema.hcl \
--dev-url "docker://clickhouse/latest/dev"

Once pushed, Atlas prints a URL to the schema. You can then apply it to any database using the schema URL in --to:

atlas schema apply \
-u "clickhouse://default:pass@localhost:9000/demo" \
--to "atlas://app" \
--dev-url "docker://clickhouse/latest/dev"

This workflow allows you to manage your schema centrally and deploy it to multiple environments without having the schema files locally.

For more advanced workflows, you can use atlas schema plan to pre-plan and review migrations before applying them. This enables teams to plan, lint, and review changes during the PR stage, edit generated SQL if needed, and ensure no human intervention is required during deployment.

Versioned Migrations​

Alternatively, there is the versioned migrations workflow, sometimes called change-based migrations, where each migration is saved in source control to be evaluated during code review and applied later. Users still benefit from Atlas intelligently planning migrations for them, but they are not automatically applied.

Creating the first migration​

When using the versioned migration workflow, our database state is managed by a migration directory. The migration directory holds all of the migration files created by Atlas, and the sum of all files in lexicographical order represents the current state of the database.

To create our first migration file, we will run the atlas migrate diff command with the necessary parameters:

  • --dir: The URL to the migration directory, file://migrations by default.
  • --to: The URL of the desired state. A state can be specified using a database URL, an HCL or SQL schema definition, or another migration directory.
  • --dev-url: A URL to a Dev Database that will be used to compute the diff.
atlas migrate diff initial \
--to file://schema.hcl \
--dev-url "docker://clickhouse/latest/dev"

Run ls migrations, and you'll notice that Atlas has automatically created a migration directory for us, as well as two files:

-- Create "repos" table
CREATE TABLE `repos` (
`id` UInt64,
`name` String,
`owner_id` Int64
) ENGINE = MergeTree
PRIMARY KEY (`id`) ORDER BY (`id`) SETTINGS index_granularity = 8192;
-- Create "users" table
CREATE TABLE `users` (
`id` UInt64,
`name` String
) ENGINE = MergeTree
PRIMARY KEY (`id`) ORDER BY (`id`) SETTINGS index_granularity = 8192;

The migration file represents the current state of our database, and the sum file is used by Atlas to maintain the integrity of the migration directory.

Pushing migration directories to Atlas​

Now that we have our first migration, we can push the migration directory to Atlas Cloud, much like how Docker images are pushed to Docker Hub.

Let's name our new migration project app and run atlas migrate push:

atlas migrate push app \
--dev-url "docker://clickhouse/latest/dev"

Migration Directory created with atlas migrate push

Once the migration directory is pushed, Atlas prints a URL to the created directory, similar to the one shown in the image above.

Applying migrations​

Once our app migration directory has been pushed, we can apply it to a database from any CD platform without necessarily having our directory there.

We'll create a simple Atlas configuration file (atlas.hcl) to store the settings for our local environment:

atlas.hcl
env "local" {
url = "clickhouse://default:pass@localhost:9000/demo"
migration {
baseline = "20261129094334"
dir = "atlas://app"
}
}

The baseline attribute marks 20261129094334_initial.sql as already applied. The demo database already has the users and repos tables from the declarative section, so running the initial migration would fail on tables that exist. With the baseline, Atlas starts the migration history at that version and applies only the files after it. See Existing Databases for more on baselines.

Run atlas migrate apply with the --env flag to instruct Atlas to select the environment configuration from the atlas.hcl file:

atlas migrate apply --env local

After applying the migration, you should receive a link to the deployment and the database where the migration was applied. Here's an example of what it should look like:

Migration deployment report created with atlas migrate apply

The report shows No Action and the current version 20261129094334: the baseline is recorded, and there are no pending files to execute.

Generating another migration​

After applying the first migration, it's time to update our schema definition file and tell Atlas to generate another migration.

Let's make two changes to our schema:

  • Add a new description column to our repos table
  • Add a new commits table
schema.hcl
table "users" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "name" {
null = false
type = String
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
table "repos" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "name" {
null = false
type = String
}
column "description" {
null = true
type = sql("Nullable(String)")
}
column "owner_id" {
null = false
type = Int64
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
table "commits" {
schema = schema.demo
engine = MergeTree
settings = {
index_granularity = 8192
}
column "id" {
null = false
type = UInt64
}
column "message" {
null = false
type = String
}
column "repo_id" {
null = false
type = Int64
}
column "author_id" {
null = false
type = Int64
}
primary_key {
columns = [column.id]
}
sort {
columns = [column.id]
}
}
schema "demo" {
engine = Atomic
}

Next, run the atlas migrate diff command again:

atlas migrate diff add_commits \
--to file://schema.hcl \
--dev-url "docker://clickhouse/latest/dev"

Run ls migrations, and you'll notice that a new migration file has been generated.

20261129095812_add_commits.sql
ALTER TABLE `repos` ADD COLUMN `description` Nullable(String) AFTER `name`;
-- Create "commits" table
CREATE TABLE `commits` (
`id` UInt64,
`message` String,
`repo_id` Int64,
`author_id` Int64
) ENGINE = MergeTree
PRIMARY KEY (`id`) ORDER BY (`id`) SETTINGS index_granularity = 8192;

If you run atlas migrate push again, you can observe the new file on the migration directory page.

atlas migrate push app \
--dev-url "docker://clickhouse/latest/dev"

Migration Directory created with atlas migrate push

Finally, run atlas migrate apply again to apply the new migration to the database:

atlas migrate apply --env local
-- migrating version 20261129095812
-> ALTER TABLE `repos` ADD COLUMN `description` Nullable(String) AFTER `name`;
-> CREATE TABLE `commits` (
`id` UInt64,
`message` String,
`repo_id` Int64,
`author_id` Int64
) ENGINE = MergeTree
PRIMARY KEY (`id`) ORDER BY (`id`) SETTINGS index_granularity = 8192;
-- ok (33.282458ms)

-------------------------
-- 114.589708ms
-- 1 migration
-- 2 sql statements

Atlas runs only 20261129095812_add_commits.sql, the one file after the baseline. The summary lists 1 migration with 2 statements: the ALTER TABLE on repos and the CREATE TABLE for commits.

Video Tutorial​

Check out the video below for a basic demonstration of using Atlas in CI/CD to manage your ClickHouse database using the declarative workflow.

GitHub Repository​

We also put together a GitHub repository with more workflow setup instructions and example files to help you get started using Atlas.

Next Step: Set Up CI/CD​

Once you have your migration directory set up, the next step is to integrate Atlas into your CI/CD pipeline. Atlas provides native integrations for popular platforms:

Wrapping Up​

In this guide, we demonstrated how to set up Atlas to manage your ClickHouse database schema. We covered both declarative and versioned migration workflows, and showed how to generate migrations, push them to an Atlas workspace, and apply them to your databases.

To keep going:

Have questions? Feedback? Message our team on Intercom.