tablediff.py
changeset 7 685b20d2d3ab
parent 6 4ab077c93b2d
child 9 2fcc8ef0b97d
--- a/tablediff.py	Wed Aug 10 18:34:54 2011 +0200
+++ b/tablediff.py	Fri Aug 12 14:39:49 2011 +0200
@@ -3,50 +3,38 @@
 # Print differencies between data in two tables of same schema.
 #
 # Requirements:
-#  * First column of both tables must be numerical primary key. 
+#  * 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
+from tools import pgmanager, pgbrowser, pgdatadiff, toolbase
 
 
-class TableDiffTool(SrcDstTool):
+class TableDiffTool(toolbase.SrcDstTool):
     def __init__(self):
-        SrcDstTool.__init__(self, name='tablediff', desc='Table diff.')
+        toolbase.SrcDstTool.__init__(self, name='tablediff', desc='Table diff.')
         
         self.parser.add_argument('table', metavar='table', type=str, help='Table name')
+        self.parser.add_argument('-s', dest='schema', metavar='schema', type=str, default='public', help='Schema name.')
+        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
         
         self.init()
 
     def main(self):
-        if '.' in self.args.table:
-            schema, table = self.args.table.split('.', 1)
-        else:
-            table = self.args.table
+        table = self.args.table
+        schema = self.args.schema
 
-        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;'
+        dd = pgdatadiff.PgDataDiff(self.pgm.get_conn('src'), self.pgm.get_conn('dst'))
+        dd.settable1(table, schema)
+        dd.settable2(table, schema)
         
-        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()
+        if self.args.sql:
+            dd.print_patch()
+        else:
+            dd.print_diff()
 
 
 tool = TableDiffTool()
 tool.main()
+