tablediff.py
changeset 56 94e091c23ebb
parent 41 6aad5e35efe8
child 83 515fadd3d286
equal deleted inserted replaced
55:adc1615d8fc5 56:94e091c23ebb
    12 from pgtoolkit.highlight import *
    12 from pgtoolkit.highlight import *
    13 
    13 
    14 
    14 
    15 class TableDiffTool(toolbase.SrcDstTablesTool):
    15 class TableDiffTool(toolbase.SrcDstTablesTool):
    16     def __init__(self):
    16     def __init__(self):
    17         toolbase.SrcDstTablesTool.__init__(self, name='tablediff', desc='Table diff.')
    17         toolbase.SrcDstTablesTool.__init__(self, name='tablediff', desc='Table diff.', allow_reverse = True)
    18         
    18 
    19         self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
    19         self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
    20         self.parser.add_argument('--rowcount', action='store_true', help='Compare number of rows.')
    20         self.parser.add_argument('--rowcount', action='store_true', help='Compare number of rows.')
    21         
    21 
    22         self.init()
    22         self.init()
    23 
    23 
    24     def main(self):
    24     def main(self):
    25         srcconn = self.pgm.get_conn('src')
    25         srcconn = self.pgm.get_conn('src')
    26         dstconn = self.pgm.get_conn('dst')
    26         dstconn = self.pgm.get_conn('dst')
    27         
    27 
    28         dd = pgdatadiff.PgDataDiff(srcconn, dstconn)
    28         dd = pgdatadiff.PgDataDiff(srcconn, dstconn)
    29         
    29 
    30         for srcschema, srctable, dstschema, dsttable in self.tables():
    30         for srcschema, srctable, dstschema, dsttable in self.tables():
    31             print('-- Diff from [%s] %s.%s to [%s] %s.%s' % (
    31             print('-- Diff from [%s] %s.%s to [%s] %s.%s' % (
    32                 self.args.src, srcschema, srctable,
    32                 self.args.src, srcschema, srctable,
    33                 self.args.dst, dstschema, dsttable))
    33                 self.args.dst, dstschema, dsttable))
    34             
    34 
    35             if self.args.rowcount:
    35             if self.args.rowcount:
    36                 with self.pgm.cursor('src') as curs:
    36                 with self.pgm.cursor('src') as curs:
    37                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (srcschema, srctable))
    37                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (srcschema, srctable))
    38                     srccount = curs.fetchone()[0]
    38                     srccount = curs.fetchone()[0]
    39                 with self.pgm.cursor('dst') as curs:
    39                 with self.pgm.cursor('dst') as curs:
    40                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (dstschema, dsttable))
    40                     curs.execute('''SELECT count(*) FROM "%s"."%s"''' % (dstschema, dsttable))
    41                     dstcount = curs.fetchone()[0]
    41                     dstcount = curs.fetchone()[0]
    42                 if srccount != dstcount:
    42                 if srccount != dstcount:
    43                     print(highlight(1, BOLD | YELLOW), "Row count differs: src=%s dst=%s" % (srccount, dstcount), highlight(0), sep='')
    43                     print(highlight(1, BOLD | YELLOW), "Row count differs: src=%s dst=%s" % (srccount, dstcount), highlight(0), sep='')
    44                 continue
    44                 continue
    45             
    45 
    46             dd.settable1(srctable, srcschema)
    46             dd.settable1(srctable, srcschema)
    47             dd.settable2(dsttable, dstschema)
    47             dd.settable2(dsttable, dstschema)
    48             
    48 
    49             if self.args.sql:
    49             if self.args.sql:
    50                 dd.print_patch()
    50                 dd.print_patch()
    51             else:
    51             else:
    52                 dd.print_diff()
    52                 dd.print_diff()
    53 
    53