tablediff.py
author Radek Brich <radek.brich@devl.cz>
Wed, 10 Aug 2011 18:34:54 +0200
changeset 6 4ab077c93b2d
child 7 685b20d2d3ab
permissions -rwxr-xr-x
Add table diff tool.

#!/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 pgtools import pgmanager, pgbrowser, pgdatadiff
from toolbase import SrcDstTool


class TableDiffTool(SrcDstTool):
    def __init__(self):
        SrcDstTool.__init__(self, name='tablediff', desc='Table diff.')
        
        self.parser.add_argument('table', metavar='table', type=str, help='Table name')
        
        self.init()

    def main(self):
        if '.' in self.args.table:
            schema, table = self.args.table.split('.', 1)
        else:
            table = self.args.table

        srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src'))

        columns = srcbrowser.list_columns(schema=schema, table=table)
        columns_sel = ', '.join(['"' + x['name'] + '"' for x in columns])
        
        table_fullname = '"' + schema + '"."'+ table + '"';
        query = 'SELECT ' + columns_sel + ' FROM ' + table_fullname + ' ORDER BY 1;'
        
        with self.pgm.cursor('src') as curs:
            curs.execute(query)
            src_rows = curs.fetchall()

        with self.pgm.cursor('dst') as curs:
            curs.execute(query)
            dst_rows = curs.fetchall()

        pgdd = pgdatadiff.PgDataDiff(table_fullname,
            src_rows, dst_rows, [x['name'] for x in columns])
        #pgdd.print_diff()
        pgdd.print_patch()


tool = TableDiffTool()
tool.main()