Skip to content

Instantly share code, notes, and snippets.

@filimonov
Last active April 1, 2020 13:45
Show Gist options
  • Save filimonov/44215eaef8c85f6e48238cd0b92aec31 to your computer and use it in GitHub Desktop.
Save filimonov/44215eaef8c85f6e48238cd0b92aec31 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
for db_sql_full_filename in /var/lib/clickhouse/metadata/*.sql
do
dbfilename="${db_sql_full_filename##*/}";
dbname="${dbfilename%.sql}"
printf "\n/*************\n ** $dbname \n *************/\n"
perl -0777 -npe 's/^ATTACH/CREATE/; s/$/;\n/' $db_sql_full_filename;
printf "USE $dbname;\n\n"
for table_sql_full_filename in /var/lib/clickhouse/metadata/$dbname/*.sql
do
perl -0777 -npe 's/^ATTACH/CREATE/; s/$/;\n/' $table_sql_full_filename;
done;
done;
clickhouse-client --query="SELECT concat('CREATE DATABASE \\\`', name, '\\\` ENGINE = ', engine, ';') FROM system.databases WHERE name <> 'system' UNION ALL WITH substring(name, 1, 7) = '.inner.' AS mv_inner_table, if(mv_inner_table, substring(name, 8), name) AS namex, argMin(create_table_query, mv_inner_table) AS create_table_queryx, argMin(engine, mv_inner_table) AS enginex SELECT concat(create_table_queryx, ';') FROM system.tables WHERE name <> 'system' GROUP BY database, namex HAVING create_table_queryx != '' ORDER BY enginex = 'MaterializedView' ASC, database ASC, namex ASC" --format=TSVRaw | while read -r line; do echo "$line" | clickhouse-format ; printf ";\n\n" ; done | perl -e '$/=undef;$_=<>;s/(\s+;)+/;/g;print $_'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment