pgtoolkit/tools/tablediff.py
changeset 100 d6088dba8fea
parent 93 b72591087495
child 101 2a2d0d5df03b
equal deleted inserted replaced
99:245646538743 100:d6088dba8fea
       
     1 from pgtoolkit import toolbase, pgmanager, pgdatadiff
       
     2 from pgtoolkit.toolbase import SrcDstTablesTool
       
     3 from pycolib.ansicolor import highlight, BOLD, YELLOW
       
     4 
       
     5 
       
     6 class TableDiffTool(toolbase.SrcDstTablesTool):
       
     7 
       
     8     """
       
     9     Print differencies between data in tables.
       
    10 
       
    11     Requirements:
       
    12     * Source table must have defined PRIMARY KEY.
       
    13     * Destination table must contain all columns from source table.
       
    14       Order is not important.
       
    15 
       
    16     """
       
    17 
       
    18     def __init__(self):
       
    19         SrcDstTablesTool.__init__(self, name='tablediff', desc=self.__doc__, allow_reverse=True)
       
    20         self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
       
    21         self.parser.add_argument('--rowcount', action='store_true', help='Compare number of rows.')
       
    22 
       
    23     def main(self):
       
    24         srcconn = self.pgm.get_conn('src')
       
    25         dstconn = self.pgm.get_conn('dst')
       
    26 
       
    27         dd = pgdatadiff.PgDataDiff(srcconn, dstconn)
       
    28 
       
    29         for srcschema, srctable, dstschema, dsttable in self.tables():
       
    30             print('-- Diff from [%s] %s.%s to [%s] %s.%s' % (
       
    31                 self.args.src, srcschema, srctable,
       
    32                 self.args.dst, dstschema, dsttable))
       
    33 
       
    34             if self.args.rowcount:
       
    35                 with self.pgm.cursor('src') as curs:
       
    36                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (srcschema, srctable))
       
    37                     srccount = curs.fetchone()[0]
       
    38                 with self.pgm.cursor('dst') as curs:
       
    39                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (dstschema, dsttable))
       
    40                     dstcount = curs.fetchone()[0]
       
    41                 if srccount != dstcount:
       
    42                     print(highlight(1, BOLD | YELLOW), "Row count differs: src=%s dst=%s" % (srccount, dstcount), highlight(0), sep='')
       
    43                 continue
       
    44 
       
    45             dd.settable1(srctable, srcschema)
       
    46             dd.settable2(dsttable, dstschema)
       
    47 
       
    48             if self.args.sql:
       
    49                 dd.print_patch()
       
    50             else:
       
    51                 dd.print_diff()
       
    52 
       
    53 
       
    54 cls = TableDiffTool
       
    55