Skip to content

Commit

Permalink
Fixed #18347 -- Wrapped raw identity inserts in tests.
Browse files Browse the repository at this point in the history
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
manfre committed May 18, 2012
1 parent 04785d2 commit 06d8aee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
27 changes: 27 additions & 0 deletions django/db/backends/__init__.py
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions tests/regressiontests/transactions_regress/tests.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 06d8aee

Please sign in to comment.