tablediff.py
author Radek Brich <radek.brich@devl.cz>
Thu, 20 Oct 2011 13:54:35 +0200
changeset 14 a900bc629ecc
parent 9 2fcc8ef0b97d
child 34 98c7809af415
permissions -rwxr-xr-x
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: 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()