Andrew Godwin avatar Andrew Godwin committed a48162b

Dry-run transaction rollback patch

Comments (0)

Files changed (2)

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))

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."
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.