listtables.py
author Radek Brich <radek.brich@devl.cz>
Tue, 19 Mar 2013 16:46:36 +0100
changeset 79 d02a36d0a9b0
parent 68 b0d972be2631
child 83 515fadd3d286
permissions -rwxr-xr-x
Update PgManager doc.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3.2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
     3
from pgtoolkit import pgbrowser, toolbase, prettysize
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
68
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
     6
class ListTablesTool(toolbase.SimpleTool):
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
    def __init__(self):
68
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
     8
        toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
     9
        self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
        self.init()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    def main(self):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
38
d3593869d624 Update bigtables tool: scan all schemas.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    15
        # scan all tables from all shemas, remember names and sizes
68
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    16
        tables = []
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    17
        for schema in browser.schemas.values():
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    18
            for table in schema.tables.values():
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    19
                for option in self.args.options:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    20
                    if option in table.options:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    21
                        tables.append(table)
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    22
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    23
        # print result
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    24
        if len(tables):
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    25
            print('Found %d tables:' % len(tables))
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    26
        else:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    27
            print('No table meets the conditions.')
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    28
        for table in tables:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    29
            table_name = '%s.%s' % (table.schema.name, table.name)
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    30
            print(' ', table_name)
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
68
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    33
tool = ListTablesTool()
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
tool.main()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35