PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
authorRadek Brich <radek.brich@devl.cz>
Fri, 17 Aug 2012 11:14:31 +0200
changeset 41 6aad5e35efe8
parent 40 922d7fb63384
child 42 9e3775460792
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
pgtoolkit/pgdatadiff.py
pgtoolkit/pgmanager.py
tablediff.py
--- a/pgtoolkit/pgdatadiff.py	Fri Aug 17 11:07:22 2012 +0200
+++ b/pgtoolkit/pgdatadiff.py	Fri Aug 17 11:14:31 2012 +0200
@@ -162,26 +162,29 @@
         '''
         curs1, curs2 = self._select()
         
-        row1 = curs1.fetchone_adapted()
-        row2 = curs2.fetchone_adapted()
+        row1 = curs1.fetchone_dict()
+        row2 = curs2.fetchone_dict()
         
         while True:
             if row1 is None and row2 is None:
                 break
-            diff = self._compare_row(row1, row2)
+            diff = self._compare_row(row1, row2, curs1.adapt, curs2.adapt)
             
             if diff:
                 yield diff
             
                 if diff.change == '-':
-                    row1 = curs1.fetchone_adapted()
+                    row1 = curs1.fetchone_dict()
                     continue
                 if diff.change == '+':
-                    row2 = curs2.fetchone_adapted()
+                    row2 = curs2.fetchone_dict()
                     continue
             # change == '*' or not diff
-            row1 = curs1.fetchone_adapted()
-            row2 = curs2.fetchone_adapted()
+            row1 = curs1.fetchone_dict()
+            row2 = curs2.fetchone_dict()
+        
+        curs1.close()
+        curs2.close()
     
     def print_diff(self):
         '''Print differencies between data of two tables.
@@ -222,8 +225,8 @@
         query1 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable1 + ' ORDER BY ' + pkey_sel
         query2 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable2 + ' ORDER BY ' + pkey_sel
         
-        curs1 = self.conn1.cursor()
-        curs2 = self.conn2.cursor()
+        curs1 = self.conn1.cursor('curs1')
+        curs2 = self.conn2.cursor('curs2')
         
         curs1.execute(query1)
         curs2.execute(query2)
@@ -243,21 +246,26 @@
         
         return None
     
-    def _compare_row(self, row1, row2):
+    def _compare_row(self, row1, row2, adapt1, adapt2):
         if row2 is None:
+            row1 = adapt1(row1)
             key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
             return DiffData('-', row1, None, key=key)
         if row1 is None:
+            row2 = adapt2(row2)
             return DiffData('+', None, row2)
         
-        
         for keyname in self.pkeycolnames:
             if row1[keyname] < row2[keyname]:
+                row1 = adapt1(row1)
                 key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
                 return DiffData('-', row1, None, key=key)
         for keyname in self.pkeycolnames:
             if row1[keyname] > row2[keyname]:
+                row2 = adapt2(row2)
                 return DiffData('+', None, row2)
         
+        row1 = adapt1(row1)
+        row2 = adapt2(row2)
         return self._compare_data(row1, row2)
 
--- a/pgtoolkit/pgmanager.py	Fri Aug 17 11:07:22 2012 +0200
+++ b/pgtoolkit/pgmanager.py	Fri Aug 17 11:14:31 2012 +0200
@@ -147,6 +147,17 @@
         rows = super(Cursor, self).fetchall()
         return [self.row_dict(row, lstrip) for row in rows]
 
+    def adapt(self, row):
+        if isinstance(row, RowDict):
+            # dict
+            adapted = RowDict()
+            for key in row.keys():
+                adapted[key] = self.mogrify('%s', [row[key]]).decode('utf8')
+        else:
+            # list
+            adapted = [self.mogrify('%s', [x]).decode('utf8') for x in row]
+        return adapted
+
     def fetchone_adapted(self, lstrip=None):
         '''Like fetchone_dict() but values are quoted for direct inclusion in SQL query.
         
--- a/tablediff.py	Fri Aug 17 11:07:22 2012 +0200
+++ b/tablediff.py	Fri Aug 17 11:14:31 2012 +0200
@@ -1,9 +1,9 @@
 #!/usr/bin/env python3.2
 #
-# Print differencies between data in two tables of same schema.
+# Print differencies between data in tables.
 #
 # Requirements:
-#  * First column of both tables must be numerical primary key.
+#  * Source table must have defined PRIMARY KEY.
 #  * Destination table must contain all columns from source table.
 #    Order is not important.
 #