Commits

and...@69d324d9-c39d-4fdc-8679-7745eae9e2c8  committed 96d5c8d

Make delete_column actually work, and wrap all migrations in a transaction.

  • Participants
  • Parent commits 7ee03d4

Comments (0)

Files changed (2)

File db/generic.py

         """
         qn = connection.ops.quote_name
         params = (qn(table_name), qn(name))
-        return ['ALTER TABLE %s DROP COLUMN %s CASCADE;' % params]
+        self.execute('ALTER TABLE %s DROP COLUMN %s CASCADE;' % params, [])
 
 
     def rename_column(self, table_name, old, new):
         raise NotImplementedError("rename_column has no generic SQL syntax")
 
 
+    def start_transaction(self):
+        """
+        Makes sure the following commands are inside a transaction.
+        Must be followed by a (commit|rollback)_transaction call.
+        """
+        transaction.commit_unless_managed()
+        transaction.enter_transaction_management()
+        transaction.managed(True)
+
+
     def commit_transaction(self):
         """
         Commits the current transaction.
+        Must be preceded by a start_transaction call.
         """
         transaction.commit()
+        transaction.leave_transaction_management()
 
 
     def rollback_transaction(self):
         """
         Rolls back the current transaction.
+        Must be preceded by a start_transaction call.
         """
         transaction.rollback()
-        
+        transaction.leave_transaction_management()
+    
+    
     def send_create_signal(self, app_label, model_names):
         """
         Sends a post_syncdb signal for the model specified.

File migration.py

 import sys
 from django.conf import settings
 from models import MigrationHistory
+from south.db import db
 
 
 def get_migrations(app):
         if fake:
             print "   (faked)"
         else:
-            klass().forwards()
+            db.start_transaction()
+            try:
+                klass().forwards()
+            except:
+                db.rollback_transaction()
+                raise
+            else:
+                db.commit_transaction()
         # Record us as having done this
         record = MigrationHistory.for_migration(app_name, migration)
         record.applied = datetime.datetime.utcnow()
             if fake:
                 print "   (faked)"
             else:
-                klass().backwards()
+                db.start_transaction()
+                try:
+                    klass().backwards()
+                except:
+                    db.rollback_transaction()
+                    raise
+                else:
+                    db.commit_transaction()
             # Record us as having not done this
             record = MigrationHistory.for_migration(app_name, migration)
             record.delete()