Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Closed
wants to merge 1 commit into from

2 participants

Michael Manfre Anssi Kääriäinen
Michael Manfre
manfre commented May 18, 2012

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.

Michael Manfre 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.
06d8aee
Anssi Kääriäinen
Owner

Three comments:

  • What is the use of the "enabled" flag in the enable_identity_inserts manager? Is there some use case where calling the disable part would be wrong depending on the return value of the enable step?
  • The naming: identity_insert_enabled -> autofield_inserts_enabled: Django does not know of identity, it deals with autofields (other references to identity should be changed to identity, too).
  • Add a little more comments to enable_identity_insert, telling that no core backend needs these methods, but for example mssql needs them.

So, seems good apart of the usual final nitpicking...

BTW it seems I will be real busy for the next couple of weeks - I intend to work on all of the MSSQL patches, but I can't do that now. Of course, if somebody else wants to commit these I have no objections to that...

Anssi Kääriäinen
Owner

I have added another patch into the ticket. I will close this PR until we see which way to take here. I can take this PR forward without any further editing by manfre if we choose this route.

Anssi Kääriäinen akaariai closed this June 08, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 18, 2012
Michael Manfre 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.
06d8aee
This page is out of date. Refresh to see the latest.
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
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.