tablediff.py
changeset 6 4ab077c93b2d
child 7 685b20d2d3ab
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tablediff.py	Wed Aug 10 18:34:54 2011 +0200
@@ -0,0 +1,52 @@
+#!/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()