Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18347 -- Wrapped raw identity inserts in tests.

Added enable_identity_insert, disable_identity_insert, and
contextmanager identity_insert_enabled to DatabaseOperations for
database backends that need to take special actions to allow
identity inserts.

All raw insert statements that set a value in to the identity field
should include these guards to allow all database backends to
function.
  • Loading branch information...
commit 06d8aee7edc1ff2862ad8c64d1097090df46fdfa 1 parent 04785d2
Michael Manfre authored May 18, 2012
27  django/db/backends/__init__.py
@@ -876,6 +876,33 @@ def combine_expression(self, connector, sub_expressions):
876 876
         conn = ' %s ' % connector
877 877
         return conn.join(sub_expressions)
878 878
 
  879
+    @contextmanager
  880
+    def identity_insert_enabled(self, table):
  881
+        enabled = self.enable_identity_insert(table)        
  882
+        try:
  883
+            yield
  884
+        finally:
  885
+            if enabled:
  886
+                self.disable_identity_insert(table)
  887
+
  888
+    def enable_identity_insert(self, table):
  889
+        """
  890
+        Backends can implement as needed to enable inserts in to
  891
+        the identity column.
  892
+        
  893
+        Should return True if identity inserts have been enabled.
  894
+        """
  895
+        pass
  896
+    
  897
+    def disable_identity_insert(self, table):
  898
+        """
  899
+        Backends can implement as needed to disable inserts in to
  900
+        the identity column.
  901
+        
  902
+        Should return True if identity inserts have been disabled.
  903
+        """
  904
+        pass
  905
+
879 906
 class BaseDatabaseIntrospection(object):
880 907
     """
881 908
     This class encapsulates all backend-specific introspection utilities
14  tests/regressiontests/transactions_regress/tests.py
@@ -24,8 +24,9 @@ def test_raw_committed_on_success(self):
24 24
         @commit_on_success
25 25
         def raw_sql():
26 26
             "Write a record using raw sql under a commit_on_success decorator"
27  
-            cursor = connection.cursor()
28  
-            cursor.execute("INSERT into transactions_regress_mod (id,fld) values (17,18)")
  27
+            with connection.ops.identity_insert_enabled('transactions_regress_mod'):
  28
+                cursor = connection.cursor()
  29
+                cursor.execute("INSERT into transactions_regress_mod (id,fld) values (17,18)")
29 30
 
30 31
         raw_sql()
31 32
         # Rollback so that if the decorator didn't commit, the record is unwritten
@@ -115,10 +116,11 @@ def reuse_cursor_ref():
115 116
             (reference). All this under commit_on_success, so the second insert should
116 117
             be committed.
117 118
             """
118  
-            cursor = connection.cursor()
119  
-            cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
120  
-            transaction.rollback()
121  
-            cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
  119
+            with connection.ops.identity_insert_enabled('transactions_regress_mod'):
  120
+                cursor = connection.cursor()
  121
+                cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
  122
+                transaction.rollback()
  123
+                cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
122 124
 
123 125
         reuse_cursor_ref()
124 126
         # Rollback so that if the decorator didn't commit, the record is unwritten

0 notes on commit 06d8aee

Please sign in to comment.
Something went wrong with that request. Please try again.