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
```
## 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

View File

@ -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()