tablediff.py
author Radek Brich <brich.radek@ifortuna.cz>
Wed, 14 Dec 2011 16:29:33 +0100
changeset 23 dc2dbe872fc8
parent 14 a900bc629ecc
child 34 98c7809af415
permissions -rwxr-xr-x
Add keep_open parameter to create_conn.

#!/usr/bin/env python3.2
#
# Print differencies between data in two tables of same schema.
#
# Requirements:
#  * First column of both tables must be numerical primary key.
#  * Destination table must contain all columns from source table.
#    Order is not important.
#

from pgtoolkit import pgmanager, pgbrowser, pgdatadiff, toolbase


class TableDiffTool(toolbase.SrcDstTool):
    def __init__(self):
        toolbase.SrcDstTool.__init__(self, name='tablediff', desc='Table diff.')
        
        self.parser.add_argument('srctable', metavar='srctable',
            type=str, help='Source table name.')
        self.parser.add_argument('--dst-table', dest='dsttable', metavar='dsttable',
            type=str, default=None, help='Destination table (default=srctable).')
        self.parser.add_argument('-s', '--src-schema', dest='srcschema', metavar='srcschema',
            type=str, default='public', help='Schema name (default=public).')
        self.parser.add_argument('--dst-schema', dest='dstschema', metavar='dstschema',
            type=str, default=None, help='Destination schema name (default=srcschema).')
        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
        
        self.init()

    def main(self):
        srcschema = self.args.srcschema
        dstschema = self.args.dstschema if self.args.dstschema else self.args.srcschema
        
        srctable = self.args.srctable
        dsttable = self.args.dsttable if self.args.dsttable else self.args.srctable
        
        dd = pgdatadiff.PgDataDiff(self.pgm.get_conn('src'), self.pgm.get_conn('dst'))
        dd.settable1(srctable, srcschema)
        dd.settable2(dsttable, dstschema)
        
        if self.args.sql:
            dd.print_patch()
        else:
            dd.print_diff()


tool = TableDiffTool()
tool.main()