listtables.py
author Radek Brich <brich.radek@ifortuna.cz>
Tue, 06 May 2014 18:37:43 +0200
changeset 101 2a2d0d5df03b
parent 93 b72591087495
child 104 d8ff52a0390f
permissions -rwxr-xr-x
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.

#!/usr/bin/env python3

from pgtoolkit import pgbrowser, toolbase


class ListTablesTool(toolbase.SimpleTool):
    def __init__(self):
        toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
        self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')

    def main(self):
        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))

        # scan all tables from all shemas, remember names and sizes
        tables = []
        for schema in browser.schemas.values():
            for table in schema.tables.values():
                for option in self.args.options:
                    if option in table.options:
                        tables.append(table)

        # print result
        if len(tables):
            print('Found %d tables:' % len(tables))
        else:
            print('No table meets the conditions.')
        for table in tables:
            table_name = '%s.%s' % (table.schema.name, table.name)
            print(' ', table_name)


tool = ListTablesTool()
tool.setup()
tool.main()