Skip to content

Instantly share code, notes, and snippets.

@turbo-ele
Created April 10, 2018 10:14
Show Gist options
  • Save turbo-ele/90044ff433b41c2518a7f98b7748f4a3 to your computer and use it in GitHub Desktop.
Save turbo-ele/90044ff433b41c2518a7f98b7748f4a3 to your computer and use it in GitHub Desktop.
CrateDB: Print "SHOW CREATE TABLE" for whole database
# vi: set fileencoding=utf-8
# -*- coding: utf-8; -*-
import sys
import argparse
from crate import client
def main():
parser = argparse.ArgumentParser(
description='collect table schemas and write to stdout'
)
parser.add_argument(
'--host',
type=str,
default='localhost:4200')
parser.add_argument(
'--out',
'-o',
type=argparse.FileType('w'),
default=sys.stdout
)
args = parser.parse_args()
host = args.host
connection = client.connect(host)
cursor = connection.cursor()
cursor.execute('''
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'sys', 'information_schema')
''')
result = cursor.fetchall()
for row in result:
schema, name = row
cursor.execute('SHOW CREATE TABLE %s.%s' % (schema, name))
create_table = cursor.fetchone()
args.out.write(''.join(create_table))
args.out.write(';\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment