tablediff.py
author Radek Brich <radek.brich@devl.cz>
Tue, 16 Aug 2011 16:03:46 +0200
changeset 9 2fcc8ef0b97d
parent 7 685b20d2d3ab
child 14 a900bc629ecc
permissions -rwxr-xr-x
Reorganize again :-) Add setup.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3.2
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# Print differencies between data in two tables of same schema.
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
#
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
# Requirements:
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
     6
#  * First column of both tables must be numerical primary key.
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
#  * Destination table must contain all columns from source table.
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
#    Order is not important.
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
#
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    11
from pgtoolkit import pgmanager, pgbrowser, pgdatadiff, toolbase
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    14
class TableDiffTool(toolbase.SrcDstTool):
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
    def __init__(self):
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    16
        toolbase.SrcDstTool.__init__(self, name='tablediff', desc='Table diff.')
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.parser.add_argument('table', metavar='table', type=str, help='Table name')
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    19
        self.parser.add_argument('-s', dest='schema', metavar='schema', type=str, default='public', help='Schema name.')
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    20
        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        self.init()
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
    def main(self):
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    25
        table = self.args.table
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    26
        schema = self.args.schema
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    28
        dd = pgdatadiff.PgDataDiff(self.pgm.get_conn('src'), self.pgm.get_conn('dst'))
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    29
        dd.settable1(table, schema)
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    30
        dd.settable2(table, schema)
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    32
        if self.args.sql:
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    33
            dd.print_patch()
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    34
        else:
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    35
            dd.print_diff()
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
tool = TableDiffTool()
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
tool.main()
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 6
diff changeset
    40