schemadiff.py
author Radek Brich <brich.radek@ifortuna.cz>
Tue, 29 Apr 2014 17:50:15 +0200
changeset 98 024299702087
parent 93 b72591087495
permissions -rwxr-xr-x
Update batchcopy: When target record exists, allow to ignore / update the error (--dst-exists parameter).

#!/usr/bin/env python3
#
# 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.', allow_reverse = True)

        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('-f', dest='function', type=str, help='Function filter (regex)')
        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
        self.parser.add_argument('--body', action='store_true', help='Output diff for function bodies.')

        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)

        try:
            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.function:
                pgd.filter_functions(self.args.function)
            if self.args.body:
                pgd.function_body_diff = True

            if self.args.sql:
                pgd.print_patch()
            else:
                pgd.print_diff()
        except pgdiff.PgDiffError as e:
            print('PgDiff error:', str(e))


tool = SchemaDiffTool()
tool.main()