schemadiff.py
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Sep 2012 12:08:47 +0200
changeset 48 b82c7c2fb5af
parent 44 4fe39c59c515
child 56 94e091c23ebb
permissions -rwxr-xr-x
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.

#!/usr/bin/env python3.2
#
# Print differences in database schema.
#
# Prints changes from source to destination.
# SQL patch updates source database schema to destination schema.
#

from pgtoolkit import pgmanager, pgbrowser, pgdiff, toolbase


class SchemaDiffTool(toolbase.SrcDstTool):
    def __init__(self):
        toolbase.SrcDstTool.__init__(self, name='schemadiff', desc='Database schema diff.')
        
        self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
        self.parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
        
        self.init()

    def main(self):
        srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src'))
        dstbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('dst'))
        
        pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)

        if self.args.schema:
            pgd.filter_schemas(include=self.args.schema)
        
        if self.args.table:
            pgd.filter_tables(include=self.args.table)

        if self.args.sql:
            pgd.print_patch()
        else:
            pgd.print_diff()


tool = SchemaDiffTool()
tool.main()