added support for database UUIDs

This commit is contained in:
Paul 2024-07-14 11:15:39 +02:00
parent c9bc6db145
commit 69a8fbe7d8

View File

@ -11,21 +11,22 @@ import clickhouse_connect
DUMPFILE = "/tmp/ch_export" DUMPFILE = "/tmp/ch_export"
createre = re.compile("(?P<begin>CREATE (TABLE|(MATERIALIZED )?VIEW)) (?P<database>\w+)\.(?P<table>\w+) (?P<query>.*)") createre = re.compile("(?P<begin>CREATE (TABLE|(MATERIALIZED )?VIEW)) (?P<database>\w+)\.(?P<table>\w+) (?P<query>.*)")
database_exclude = { database_exclude = [
"default": True, "default",
"INFORMATION_SCHEMA": True, "INFORMATION_SCHEMA",
"information_schema": True, "information_schema",
"system": True, "system",
} ]
def get_databases_tables(client): def get_databases_tables(client):
databases = {} databases = {}
q_db = client.query("show databases;") db_exclude_join = ["'{}'".format(i) for i in database_exclude]
q_db_exclude = "({})".format(",".join(db_exclude_join))
q_db = client.query(f"SELECT database,toString(uuid) FROM system.databases WHERE database NOT IN {q_db_exclude};")
for d in q_db.result_rows: for d in q_db.result_rows:
dbname = d[0] dbname = d[0]
if database_exclude.get(dbname): dbuuid = d[1]
continue databases[dbname] = {"ddl": f"CREATE DATABASE {dbname} UUID '{dbuuid}'", "tables":{}}
databases[dbname] = {"schema":"", "tables":{}}
q_tables = client.query(f"show tables from {dbname};") q_tables = client.query(f"show tables from {dbname};")
for t in q_tables.result_rows: for t in q_tables.result_rows:
tablename = t[0] tablename = t[0]
@ -56,13 +57,13 @@ def dump(dumpfile=DUMPFILE):
a = json.load(f) a = json.load(f)
for database,values in a.items(): for database,values in a.items():
print(f"--- database {database} ---") print(f"--- database {database} ---")
print(f"CREATE DATABASE {database};\n") print("{};\n".format(values["ddl"]))
for table,c in values["tables"].items(): for table,c in values["tables"].items():
print(f"-- table {database}.{table} --") print(f"-- table {database}.{table} --")
print(f"{c};\n") print(f"{c};\n")
except FileNotFoundError as e: except FileNotFoundError as e:
print(e) print(e)
print("to re-create replicas, run 'clickhouse-client --multiquery < generated_file.sql' on new replica server") 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(