Skip to content

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@manfre

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.

@manfre 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
@akaariai
Django member

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

@akaariai
Django member

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.

@akaariai akaariai closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 18, 2012
  1. @manfre

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

    manfre committed
    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.
Showing with 35 additions and 6 deletions.
  1. +27 −0 django/db/backends/__init__.py
  2. +8 −6 tests/regressiontests/transactions_regress/tests.py
View
27 django/db/backends/__init__.py
@@ -876,6 +876,33 @@ def combine_expression(self, connector, sub_expressions):
conn = ' %s ' % connector
return conn.join(sub_expressions)
+ @contextmanager
+ def identity_insert_enabled(self, table):
+ enabled = self.enable_identity_insert(table)
+ try:
+ yield
+ finally:
+ if enabled:
+ self.disable_identity_insert(table)
+
+ def enable_identity_insert(self, table):
+ """
+ Backends can implement as needed to enable inserts in to
+ the identity column.
+
+ Should return True if identity inserts have been enabled.
+ """
+ pass
+
+ def disable_identity_insert(self, table):
+ """
+ Backends can implement as needed to disable inserts in to
+ the identity column.
+
+ Should return True if identity inserts have been disabled.
+ """
+ pass
+
class BaseDatabaseIntrospection(object):
"""
This class encapsulates all backend-specific introspection utilities
View
14 tests/regressiontests/transactions_regress/tests.py
@@ -24,8 +24,9 @@ def test_raw_committed_on_success(self):
@commit_on_success
def raw_sql():
"Write a record using raw sql under a commit_on_success decorator"
- cursor = connection.cursor()
- cursor.execute("INSERT into transactions_regress_mod (id,fld) values (17,18)")
+ with connection.ops.identity_insert_enabled('transactions_regress_mod'):
+ cursor = connection.cursor()
+ cursor.execute("INSERT into transactions_regress_mod (id,fld) values (17,18)")
raw_sql()
# Rollback so that if the decorator didn't commit, the record is unwritten
@@ -115,10 +116,11 @@ def reuse_cursor_ref():
(reference). All this under commit_on_success, so the second insert should
be committed.
"""
- cursor = connection.cursor()
- cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
- transaction.rollback()
- cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
+ with connection.ops.identity_insert_enabled('transactions_regress_mod'):
+ cursor = connection.cursor()
+ cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
+ transaction.rollback()
+ cursor.execute("INSERT into transactions_regress_mod (id,fld) values (1,2)")
reuse_cursor_ref()
# Rollback so that if the decorator didn't commit, the record is unwritten
Something went wrong with that request. Please try again.