Drop fkeys also in main thread.
authorMarko Kreen <markokr@gmail.com>
Tue, 14 Aug 2007 20:57:02 +0000 (20:57 +0000)
committerMarko Kreen <markokr@gmail.com>
Tue, 14 Aug 2007 20:57:02 +0000 (20:57 +0000)
By Erik Jones

python/londiste/playback.py
python/londiste/table_copy.py

index b2f6d1b0c753174362799b2f7611701444f5e750..d9c8aab6d1b61ca3161374f27c1e9f6ef21879d2 100644 (file)
@@ -370,6 +370,12 @@ class Replicator(pgq.SerialConsumer):
         elif cnt.missing:
             # seems there is no active copy thread, launch new
             t = self.get_table_by_state(TABLE_MISSING)
+
+            # drop all foreign keys to and from this table
+            self.drop_fkeys(dst_db, t.name)
+
+            # change state after fkeys are dropped thus allowing
+            # failure inbetween
             self.change_table_state(dst_db, t, TABLE_IN_COPY)
 
             # the copy _may_ happen immidiately
@@ -645,6 +651,19 @@ class Replicator(pgq.SerialConsumer):
             q2 = "select londiste.subscriber_restore_table_fkey(%(from_table)s, %(fkey_name)s)"
             dst_curs.execute(q2, row)
             dst_db.commit()
+    
+    def drop_fkeys(self, dst_db, table_name):
+        # drop all foreign keys to and from this table
+        # they need to be dropped one at a time to avoid deadlocks with user code
+        dst_curs = dst_db.cursor()
+        q = "select * from londiste.find_table_fkeys(%s)"
+        dst_curs.execute(q, [table_name])
+        list = dst_curs.dictfetchall()
+        for row in list:
+            self.log.info('Dropping fkey: %s' % row['fkey_name'])
+            q2 = "select londiste.subscriber_drop_table_fkey(%(from_table)s, %(fkey_name)s)"
+            dst_curs.execute(q2, row)
+            dst_db.commit()
         
 if __name__ == '__main__':
     script = Replicator(sys.argv[1:])
index 1ba963b3b3f45ff67bc0e0c98825c84e388830ba..d20836e8cf26d951e29c4070c51bfc51c49e2ef7 100644 (file)
@@ -50,9 +50,6 @@ class CopyTable(Replicator):
         for c in src_struct.get_column_list():
             if c not in dlist:
                 raise Exception('Column %s does not exist on dest side' % c)
-        
-        # drop all foreign keys to and from this table
-        self.drop_fkeys(dst_db, tbl_stat.name)
 
         # drop unnecessary stuff
         objs = T_CONSTRAINT | T_INDEX | T_TRIGGER | T_RULE
@@ -109,19 +106,6 @@ class CopyTable(Replicator):
             self.log.info("%s: copy finished: %d bytes, %d rows" % (
                           tablename, stats[0], stats[1]))
 
-    def drop_fkeys(self, dst_db, table_name):
-        # drop all foreign keys to and from this table
-        # they need to be dropped one at a time to avoid deadlocks with user code
-        dst_curs = dst_db.cursor()
-        q = "select * from londiste.find_table_fkeys(%s)"
-        dst_curs.execute(q, [table_name])
-        list = dst_curs.dictfetchall()
-        for row in list:
-            self.log.info('Dropping fkey: %s' % row['fkey_name'])
-            q2 = "select londiste.subscriber_drop_table_fkey(%(from_table)s, %(fkey_name)s)"
-            dst_curs.execute(q2, row)
-            dst_db.commit()
-
 if __name__ == '__main__':
     script = CopyTable(sys.argv[1:])
     script.start()