Commits

Andrew Godwin committed a48162b

Dry-run transaction rollback patch

  • Participants
  • Parent commits 1473cbf

Comments (0)

Files changed (2)

File south/db/generic.py

         self.debug = False
         self.deferred_sql = []
         self.dry_run = False
+        self.pending_transactions = 0
         self.pending_create_signals = []
     
 
         Must be followed by a (commit|rollback)_transaction call.
         """
         if self.dry_run:
-            return
+            self.pending_transactions += 1
         transaction.commit_unless_managed()
         transaction.enter_transaction_management()
         transaction.managed(True)
         Must be preceded by a start_transaction call.
         """
         if self.dry_run:
-            return
+            self.pending_transactions -= 1
         transaction.rollback()
         transaction.leave_transaction_management()
 
+    def rollback_transactions_dry_run(self):
+        """
+        Rolls back all pending_transactions during this dry run.
+        """
+        if not self.dry_run:
+            return
+        while self.pending_transactions > 0:
+            self.rollback_transaction()
+        if transaction.is_dirty():
+            # Force an exception, if we're still in a dirty transaction.
+            # This means we are missing a COMMIT/ROLLBACK.
+            transaction.leave_transaction_management()
+
 
     def send_create_signal(self, app_label, model_names):
         self.pending_create_signals.append((app_label, model_names))

File south/migration.py

                     db.dry_run = True
                     db.debug, old_debug = False, db.debug
                     pending_creates = db.get_pending_creates()
+                    db.start_transaction()
                     try:
                         if len(args[0]) == 1:  # They don't want an ORM param
                             runfunc()
                         else:
                             runfunc(orm)
+                            db.rollback_transactions_dry_run()
                     except:
                         traceback.print_exc()
                         print " ! Error found during dry run of migration! Aborting."