pgtoolkit/tools/listdepends.py
author Radek Brich <brich.radek@ifortuna.cz>
Wed, 07 May 2014 18:33:50 +0200
changeset 102 fda45bdfd68d
parent 101 2a2d0d5df03b
permissions -rw-r--r--
Update ToolBase: Load cascade of config files, add -c parameter.

from pgtoolkit.toolbase import SimpleTool
from pgtoolkit import pgbrowser


class ListDependsTool(SimpleTool):

    """
    List column dependencies.

    """

    def __init__(self):
        SimpleTool.__init__(self, name='listdepends')

    def specify_args(self):
        SimpleTool.specify_args(self)
        self.parser.add_argument('table', metavar='table', type=str, help='Table name.')
        self.parser.add_argument('column', metavar='column', type=str, help='Column name.')
        self.parser.add_argument('-s', '--schema', dest='schema', metavar='schema',
            type=str, default='public', help='Schema name (default=public).')

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

        objects = browser.list_column_usage(self.args.table, self.args.column, schema=self.args.schema)
        for obj in sorted(objects, key=lambda x: (x['type'], x['schema'], x['name'])):
            print(obj['type'], ' ', obj['schema'], '.', obj['name'], sep='')


cls = ListDependsTool