Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joshleecreates/4f6f5085a3a2af9f5534fc7f93bc0a21 to your computer and use it in GitHub Desktop.
Save joshleecreates/4f6f5085a3a2af9f5534fc7f93bc0a21 to your computer and use it in GitHub Desktop.
Migrates schema and tables one-by-one, for moving from a cluster with an older version of CH
#!/bin/bash
table='...'
database='...'
local='...' # clickhouse host - name/IP
remote='...' # clickhouse host - name/IP
CH="clickhouse-client" # you may add auth here
settings="
max_insert_threads=20, # tune threads and block sizes if needed
max_threads=20,
min_insert_block_size_bytes = 536870912,
min_insert_block_size_rows = 16777216,
max_insert_block_size = 16777216,
optimize_on_insert=0
"
# need it to create temp table with same structure (suitable for attach)
params=$($CH -h $remote -q "select partition_key,sorting_key,primary_key from system.tables where table='$table' and database = '$database' " -f TSV)
IFS=$'\t' read -r partition_key sorting_key primary_key <<< $params
$CH -h $local \ # get list of source partitions
-q "select distinct partition from system.parts where table='$table' and database = '$database' "
while read -r partition; do
# check that the partition is already copied
if [ `$CH -h $remote -q " select count() from system.parts table='$table' and database = '$database' and partition='$partition'"` -eq 0 ] ; then
$CH -n -h $remote -q "
create temporary table temp as $database.$table engine=MergeTree
partition by ($partition_key) primary key ($primary_key) order by ($sorting_key);
SYSTEM STOP MERGES temp;
set $settings;
insert into temp select * from remote($local,$database.$table) where _partition='$partition'
;
alter table $database.$table attach partition $partition from temp
"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment