@aragornhq/ch-migration
v1.1.2
Published
Production-grade ClickHouse migration CLI with rollback, test coverage, and GitHub Actions support
Maintainers
Readme
@aragornhq/ch-migration
⚔️ Production-grade CLI for managing ClickHouse schema migrations with raw SQL, rollback, integrity tracking, strict mode, and GitHub automation.
🚀 Features
- ✅ Native ClickHouse support using
@clickhouse/client - ✅ Fully typed CLI (TypeScript)
- ✅ Supports
migration:create,migration:up,migration:down,dump - ✅ Optional dry run to validate migrations without applying them
- ✅ Rollback support using
-- ROLLBACK BELOW --separator - ✅ SHA-256 hash tracking for applied migrations
- ✅ Enforced one-statement-per-file (recommended)
- ✅ Optional config via
ch-migration.json - ✅
${CH_CLUSTER}placeholder replaced with theCH_CLUSTERenvironment variable - ✅ Uses
ReplicatedReplacingMergeTreefor migration tracking whenCH_CLUSTERis set - ✅ Validates
CREATE TABLEmigrations useON CLUSTERwith aReplicatedengine whenCH_CLUSTERis set
📦 Installation
npm install --save-dev @aragornhq/ch-migration🔧 Setup
- Set the ClickHouse connection using environment variables (prefixed with
CH_):
CH_HOST=localhost
CH_PORT=8123
CH_DB=default
CH_USER=default
CH_PASSWORD=
# set to "true" when using HTTPS
CH_USE_TLS=false
# optional: set cluster name for `${CH_CLUSTER}`
CH_CLUSTER=- Specify where your migration files live via a
ch-migration.jsonfile:
{
"path": "db/migrations"
}Create the folder if it does not already exist.
🛠️ Usage
Run the CLI with npx or via an npm script. The executable name is ch-migrate:
npx ch-migrate <command> [options]Commands
migration:create <name> --path=<folder>– create a timestamped migration file. The--pathoption is optional when the path is defined inch-migration.json.migration:up --path=<folder> [--dry-run]– apply all pending migrations. Use--dry-runto preview without applying.migration:down --file=<filename.sql> --path=<folder>– roll back a single migration.dump --out=<file>– exportCREATEstatements for all tables in the current database. Each statement includesIF NOT EXISTSand noDROPstatements so rerunning is safe.
Each file should contain your SQL up statement followed by -- ROLLBACK BELOW -- and the down statement. Only one SQL statement per section is enforced.
-- 20250101_create_table.sql
CREATE TABLE example (id UInt8) ENGINE = MergeTree;
-- ROLLBACK BELOW --
DROP TABLE example;Applied migrations are recorded in a migrations table together with a SHA‑256 hash. If a hash changes, the run fails to prevent drift.
