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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
b72591087495 Change python3.2 to python3 in scripts.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
     1
#!/usr/bin/env python3
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
83
515fadd3d286 Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents: 68
diff changeset
     3
from pgtoolkit import pgbrowser, toolbase
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
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    def main(self):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
38
d3593869d624 Update bigtables tool: scan all schemas.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    14
        # 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
    15
        tables = []
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    16
        for schema in browser.schemas.values():
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    17
            for table in schema.tables.values():
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    18
                for option in self.args.options:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    19
                    if option in table.options:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    20
                        tables.append(table)
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    21
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    22
        # print result
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    23
        if len(tables):
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    24
            print('Found %d tables:' % len(tables))
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    25
        else:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    26
            print('No table meets the conditions.')
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    27
        for table in tables:
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    28
            table_name = '%s.%s' % (table.schema.name, table.name)
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    29
            print(' ', table_name)
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
68
b0d972be2631 Add listtables tool.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    32
tool = ListTablesTool()
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    33
tool.setup()
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