|
1 #!/usr/bin/env python3.2 |
|
2 # |
|
3 # Print differencies between data in two tables of same schema. |
|
4 # |
|
5 # Requirements: |
|
6 # * First column of both tables must be numerical primary key. |
|
7 # * Destination table must contain all columns from source table. |
|
8 # Order is not important. |
|
9 # |
|
10 |
|
11 from pgtools import pgmanager, pgbrowser, pgdatadiff |
|
12 from toolbase import SrcDstTool |
|
13 |
|
14 |
|
15 class TableDiffTool(SrcDstTool): |
|
16 def __init__(self): |
|
17 SrcDstTool.__init__(self, name='tablediff', desc='Table diff.') |
|
18 |
|
19 self.parser.add_argument('table', metavar='table', type=str, help='Table name') |
|
20 |
|
21 self.init() |
|
22 |
|
23 def main(self): |
|
24 if '.' in self.args.table: |
|
25 schema, table = self.args.table.split('.', 1) |
|
26 else: |
|
27 table = self.args.table |
|
28 |
|
29 srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src')) |
|
30 |
|
31 columns = srcbrowser.list_columns(schema=schema, table=table) |
|
32 columns_sel = ', '.join(['"' + x['name'] + '"' for x in columns]) |
|
33 |
|
34 table_fullname = '"' + schema + '"."'+ table + '"'; |
|
35 query = 'SELECT ' + columns_sel + ' FROM ' + table_fullname + ' ORDER BY 1;' |
|
36 |
|
37 with self.pgm.cursor('src') as curs: |
|
38 curs.execute(query) |
|
39 src_rows = curs.fetchall() |
|
40 |
|
41 with self.pgm.cursor('dst') as curs: |
|
42 curs.execute(query) |
|
43 dst_rows = curs.fetchall() |
|
44 |
|
45 pgdd = pgdatadiff.PgDataDiff(table_fullname, |
|
46 src_rows, dst_rows, [x['name'] for x in columns]) |
|
47 #pgdd.print_diff() |
|
48 pgdd.print_patch() |
|
49 |
|
50 |
|
51 tool = TableDiffTool() |
|
52 tool.main() |