listdepends.py
author Radek Brich <radek.brich@devl.cz>
Mon, 05 Mar 2012 18:36:46 +0100
changeset 31 c2e6e24b83d9
child 83 515fadd3d286
permissions -rwxr-xr-x
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.

#!/usr/bin/env python3.2

from pgtoolkit import pgbrowser, toolbase, prettysize


class ListDependsTool(toolbase.SimpleTool):
    def __init__(self):
        toolbase.SimpleTool.__init__(self, name='listdepends', desc='List column dependencies.')
        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).')
        self.init()

    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='')


tool = ListDependsTool()
tool.main()