Improved the API of set_autocommit.

This commit is contained in:
Aymeric Augustin 2013-03-11 15:10:58 +01:00
parent f32100939e
commit e654180ce2
8 changed files with 24 additions and 24 deletions

View File

@ -108,7 +108,7 @@ class BaseDatabaseWrapper(object):
self.connection = self.get_new_connection(conn_params) self.connection = self.get_new_connection(conn_params)
self.init_connection_state() self.init_connection_state()
if self.settings_dict['AUTOCOMMIT']: if self.settings_dict['AUTOCOMMIT']:
self.set_autocommit() self.set_autocommit(True)
connection_created.send(sender=self.__class__, connection=self) connection_created.send(sender=self.__class__, connection=self)
def ensure_connection(self): def ensure_connection(self):
@ -314,7 +314,7 @@ class BaseDatabaseWrapper(object):
if managed == self.autocommit: if managed == self.autocommit:
self.set_autocommit(not managed) self.set_autocommit(not managed)
def set_autocommit(self, autocommit=True): def set_autocommit(self, autocommit):
""" """
Enable or disable autocommit. Enable or disable autocommit.
""" """

View File

@ -466,7 +466,7 @@ class BaseDatabaseCreation(object):
warnings.warn( warnings.warn(
"set_autocommit was moved from BaseDatabaseCreation to " "set_autocommit was moved from BaseDatabaseCreation to "
"BaseDatabaseWrapper.", PendingDeprecationWarning, stacklevel=2) "BaseDatabaseWrapper.", PendingDeprecationWarning, stacklevel=2)
return self.connection.set_autocommit() return self.connection.set_autocommit(True)
def sql_table_creation_suffix(self): def sql_table_creation_suffix(self):
""" """

View File

@ -124,7 +124,7 @@ def get_autocommit(using=None):
""" """
return get_connection(using).autocommit return get_connection(using).autocommit
def set_autocommit(using=None, autocommit=True): def set_autocommit(autocommit, using=None):
""" """
Set the autocommit status of the connection. Set the autocommit status of the connection.
""" """

View File

@ -255,7 +255,7 @@ database connection, if you need to.
.. function:: get_autocommit(using=None) .. function:: get_autocommit(using=None)
.. function:: set_autocommit(using=None, autocommit=True) .. function:: set_autocommit(autocommit, using=None)
These functions take a ``using`` argument which should be the name of a These functions take a ``using`` argument which should be the name of a
database. If it isn't provided, Django uses the ``"default"`` database. database. If it isn't provided, Django uses the ``"default"`` database.
@ -600,11 +600,11 @@ To disable autocommit temporarily, instead of::
you should now use:: you should now use::
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
# do stuff # do stuff
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
To enable autocommit temporarily, instead of:: To enable autocommit temporarily, instead of::
@ -613,11 +613,11 @@ To enable autocommit temporarily, instead of::
you should now use:: you should now use::
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
try: try:
# do stuff # do stuff
finally: finally:
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
Disabling transaction management Disabling transaction management
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -522,7 +522,7 @@ class FkConstraintsTests(TransactionTestCase):
""" """
When constraint checks are disabled, should be able to write bad data without IntegrityErrors. When constraint checks are disabled, should be able to write bad data without IntegrityErrors.
""" """
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
# Create an Article. # Create an Article.
models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
@ -538,13 +538,13 @@ class FkConstraintsTests(TransactionTestCase):
finally: finally:
transaction.rollback() transaction.rollback()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
def test_disable_constraint_checks_context_manager(self): def test_disable_constraint_checks_context_manager(self):
""" """
When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors. When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors.
""" """
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
# Create an Article. # Create an Article.
models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
@ -559,14 +559,14 @@ class FkConstraintsTests(TransactionTestCase):
finally: finally:
transaction.rollback() transaction.rollback()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
def test_check_constraints(self): def test_check_constraints(self):
""" """
Constraint checks should raise an IntegrityError when bad data is in the DB. Constraint checks should raise an IntegrityError when bad data is in the DB.
""" """
try: try:
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
# Create an Article. # Create an Article.
models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
# Retrive it from the DB # Retrive it from the DB
@ -580,7 +580,7 @@ class FkConstraintsTests(TransactionTestCase):
finally: finally:
transaction.rollback() transaction.rollback()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
class ThreadTests(TestCase): class ThreadTests(TestCase):

View File

@ -25,7 +25,7 @@ class SampleTestCase(TestCase):
class TestNoInitialDataLoading(TransactionTestCase): class TestNoInitialDataLoading(TransactionTestCase):
def test_syncdb(self): def test_syncdb(self):
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
Book.objects.all().delete() Book.objects.all().delete()
@ -37,7 +37,7 @@ class TestNoInitialDataLoading(TransactionTestCase):
self.assertQuerysetEqual(Book.objects.all(), []) self.assertQuerysetEqual(Book.objects.all(), [])
transaction.rollback() transaction.rollback()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
def test_flush(self): def test_flush(self):
@ -49,7 +49,7 @@ class TestNoInitialDataLoading(TransactionTestCase):
lambda a: a.name lambda a: a.name
) )
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
management.call_command( management.call_command(
'flush', 'flush',
@ -61,7 +61,7 @@ class TestNoInitialDataLoading(TransactionTestCase):
self.assertQuerysetEqual(Book.objects.all(), []) self.assertQuerysetEqual(Book.objects.all(), [])
transaction.rollback() transaction.rollback()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
class FixtureTestCase(TestCase): class FixtureTestCase(TestCase):

View File

@ -684,8 +684,8 @@ class TestTicket11101(TransactionTestCase):
@skipUnlessDBFeature('supports_transactions') @skipUnlessDBFeature('supports_transactions')
def test_ticket_11101(self): def test_ticket_11101(self):
"""Test that fixtures can be rolled back (ticket #11101).""" """Test that fixtures can be rolled back (ticket #11101)."""
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
self.ticket_11101() self.ticket_11101()
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)

View File

@ -269,19 +269,19 @@ class AtomicMergeTests(TransactionTestCase):
class AtomicErrorsTests(TransactionTestCase): class AtomicErrorsTests(TransactionTestCase):
def test_atomic_requires_autocommit(self): def test_atomic_requires_autocommit(self):
transaction.set_autocommit(autocommit=False) transaction.set_autocommit(False)
try: try:
with self.assertRaises(transaction.TransactionManagementError): with self.assertRaises(transaction.TransactionManagementError):
with transaction.atomic(): with transaction.atomic():
pass pass
finally: finally:
transaction.set_autocommit(autocommit=True) transaction.set_autocommit(True)
def test_atomic_prevents_disabling_autocommit(self): def test_atomic_prevents_disabling_autocommit(self):
autocommit = transaction.get_autocommit() autocommit = transaction.get_autocommit()
with transaction.atomic(): with transaction.atomic():
with self.assertRaises(transaction.TransactionManagementError): with self.assertRaises(transaction.TransactionManagementError):
transaction.set_autocommit(autocommit=not autocommit) transaction.set_autocommit(not autocommit)
# Make sure autocommit wasn't changed. # Make sure autocommit wasn't changed.
self.assertEqual(connection.autocommit, autocommit) self.assertEqual(connection.autocommit, autocommit)