listtables.py
changeset 105 10551741f61f
parent 104 d8ff52a0390f
child 106 db4c582a2abd
equal deleted inserted replaced
104:d8ff52a0390f 105:10551741f61f
     1 #!/usr/bin/env python3
       
     2 
       
     3 from pydbkit import pgbrowser, toolbase
       
     4 
       
     5 
       
     6 class ListTablesTool(toolbase.SimpleTool):
       
     7     def __init__(self):
       
     8         toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
       
     9         self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
       
    10 
       
    11     def main(self):
       
    12         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
       
    13 
       
    14         # scan all tables from all shemas, remember names and sizes
       
    15         tables = []
       
    16         for schema in browser.schemas.values():
       
    17             for table in schema.tables.values():
       
    18                 for option in self.args.options:
       
    19                     if option in table.options:
       
    20                         tables.append(table)
       
    21 
       
    22         # print result
       
    23         if len(tables):
       
    24             print('Found %d tables:' % len(tables))
       
    25         else:
       
    26             print('No table meets the conditions.')
       
    27         for table in tables:
       
    28             table_name = '%s.%s' % (table.schema.name, table.name)
       
    29             print(' ', table_name)
       
    30 
       
    31 
       
    32 tool = ListTablesTool()
       
    33 tool.setup()
       
    34 tool.main()
       
    35