updated help, fixed tables

This commit is contained in:
Paul 2024-07-14 10:53:19 +02:00
parent 0f2cfd9454
commit c9bc6db145
3 changed files with 34 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/tmp

View File

@ -16,6 +16,23 @@ options:
--print-statements --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 ## License
```text ```text

View File

@ -45,17 +45,24 @@ def get_databases_tables(client):
def export(dumpfile=DUMPFILE): def export(dumpfile=DUMPFILE):
client = clickhouse_connect.get_client(host='localhost', username='default', password='') client = clickhouse_connect.get_client(host='localhost', username='default', password='')
databases = get_databases_tables(client) databases = get_databases_tables(client)
print(f"exporting to {dumpfile}")
with open(dumpfile, "w") as f: with open(dumpfile, "w") as f:
json.dump(databases, f, indent=4) json.dump(databases, f, indent=4)
return return
def dump(dumpfile=DUMPFILE): def dump(dumpfile=DUMPFILE):
with open(dumpfile, "r") as f: try:
a = json.load(f) with open(dumpfile, "r") as f:
for database,values in a.items(): a = json.load(f)
for table,c in values["tables"].items(): for database,values in a.items():
print(f"# {database}.{table}") print(f"--- database {database} ---")
print(f"{c}\n") 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(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -68,8 +75,10 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if args.export: if args.export:
export(args.dumpfile) export(args.dumpfile)
if args.print_statements: elif args.print_statements:
dump(args.dumpfile) dump(args.dumpfile)
else:
parser.print_help()
if __name__ == "__main__": if __name__ == "__main__":
main() main()