tablediff.py
author Radek Brich <radek.brich@devl.cz>
Tue, 07 Feb 2012 10:40:35 +0100
changeset 27 5fb4883604d6
parent 14 a900bc629ecc
child 34 98c7809af415
permissions -rwxr-xr-x
Add analyzeall tool. Updates, fixes.

#!/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()