diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ceeb05b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/tmp diff --git a/README.md b/README.md index 10795e4..5aeef44 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,23 @@ options: --print-statements ``` +## Examples + +Export tables from source node +``` +clickhouse_ddl_export.py --export /tmp/source_db_tables.json +clickhouse_ddl_export.py --print-statements /tmp/source_db_tables.json > /tmp/ddl.sql +``` + +In destination node +``` +clickhouse client --multiquery < /tmp/ddl.sql +``` + +Then replication is applied, and data is fetched from source node(s) + +Have fun! + ## License ```text diff --git a/clickhouse_ddl_export.py b/clickhouse_ddl_export.py index e082240..f80efb8 100755 --- a/clickhouse_ddl_export.py +++ b/clickhouse_ddl_export.py @@ -45,17 +45,24 @@ def get_databases_tables(client): def export(dumpfile=DUMPFILE): client = clickhouse_connect.get_client(host='localhost', username='default', password='') databases = get_databases_tables(client) + print(f"exporting to {dumpfile}") with open(dumpfile, "w") as f: json.dump(databases, f, indent=4) return def dump(dumpfile=DUMPFILE): - with open(dumpfile, "r") as f: - a = json.load(f) - for database,values in a.items(): - for table,c in values["tables"].items(): - print(f"# {database}.{table}") - print(f"{c}\n") + try: + with open(dumpfile, "r") as f: + a = json.load(f) + for database,values in a.items(): + print(f"--- database {database} ---") + print(f"CREATE DATABASE {database};\n") + for table,c in values["tables"].items(): + print(f"-- table {database}.{table} --") + print(f"{c};\n") + except FileNotFoundError as e: + print(e) + print("to re-create replicas, run 'clickhouse-client --multiquery < generated_file.sql' on new replica server") def main(): parser = argparse.ArgumentParser( @@ -68,8 +75,10 @@ def main(): args = parser.parse_args() if args.export: export(args.dumpfile) - if args.print_statements: + elif args.print_statements: dump(args.dumpfile) + else: + parser.print_help() if __name__ == "__main__": main()