migration-patterns
Installation
SKILL.md
Migration Patterns
When to use this skill
Load when users ask about schema changes, migrations, or engine upgrades.
ALTER TABLE Operations
- Add column:
ALTER TABLE t ADD COLUMN col Type [DEFAULT expr] [AFTER existing_col] - Drop column:
ALTER TABLE t DROP COLUMN col - Modify type:
ALTER TABLE t MODIFY COLUMN col NewType(must be compatible) - Rename:
ALTER TABLE t RENAME COLUMN old TO new - These are metadata-only operations — instant for most changes
Engine Changes
- Cannot ALTER engine directly
- Pattern: create new table → insert from old → rename
CREATE TABLE t_new ENGINE = ReplacingMergeTree() ORDER BY id AS SELECT * FROM t_old;
RENAME TABLE t_old TO t_backup, t_new TO t_old;
- For large tables: use
INSERT INTO ... SELECTwith batching
Zero-Downtime Migrations
- Create new table with desired schema
- Create materialized view to capture new inserts:
CREATE MATERIALIZED VIEW mv TO t_new AS SELECT ... FROM t_old - Backfill historical data:
INSERT INTO t_new SELECT ... FROM t_old - Verify data consistency
- Switch application to new table
- Drop old table and materialized view
Data Backfill Patterns
- Batch by partition:
INSERT INTO new SELECT * FROM old WHERE toYYYYMM(date) = 202301 - Use
max_insert_block_sizeandmax_threadsfor throughput control - Monitor with
system.processesandsystem.merges - Verify row counts match after backfill
Partition Operations
ALTER TABLE t ATTACH PARTITION id FROM other_table— zero-copy if same structureALTER TABLE t REPLACE PARTITION id FROM other_table— atomic swapALTER TABLE t MOVE PARTITION id TO TABLE other_table— move data
Common Pitfalls
- Nullable to non-Nullable requires default value for existing NULLs
- Changing ORDER BY requires table recreation
- Mutations (UPDATE/DELETE) rewrite all parts — schedule off-peak
- Test migrations on staging with production data volumes
Related skills
More from duyet/clickhouse-monitoring
troubleshooting
Diagnose and resolve common ClickHouse issues: OOM, slow merges, replication lag, disk full, stuck mutations, and query failures.
2replication-guide
ReplicatedMergeTree operations, failover procedures, lag diagnosis, quorum writes, and Keeper management.
2query-optimization
Query optimization strategies: PREWHERE, JOIN patterns, materialized views, EXPLAIN analysis, index usage, and query profiling.
2storage-optimization
Compression codecs, TTL policies, tiered storage, part management, and disk space optimization.
2security-hardening
RBAC configuration, row policies, quotas, network security, audit logging, and access control best practices.
2cluster-operations
Distributed table management, resharding, node addition/removal, and cluster topology operations.
2