Ticket #9479: 9479-r10858.diff

File 9479-r10858.diff, 3.3 KB (added by russellm, 6 years ago)

Test case demonstrating problem

  • new file tests/regressiontests/delete_regress/__init__.py

    diff --git a/tests/regressiontests/delete_regress/__init__.py b/tests/regressiontests/delete_regress/__init__.py
    new file mode 100644
    index 0000000..8b13789
    - +  
     1
  • new file tests/regressiontests/delete_regress/models.py

    diff --git a/tests/regressiontests/delete_regress/models.py b/tests/regressiontests/delete_regress/models.py
    new file mode 100644
    index 0000000..20d41ac
    - +  
     1from django.conf import settings
     2from django.db import models, backend, connection, transaction
     3from django.test import TransactionTestCase
     4
     5class Child(models.Model):
     6    data = models.IntegerField()
     7
     8class Parent(models.Model):
     9    ref = models.ForeignKey(Child)
     10
     11if settings.DATABASE_ENGINE != 'sqlite3':
     12    class DeleteLockingTest(TransactionTestCase):
     13        def setUp(self):
     14            # Create a second connection to the database
     15            self.conn2 = backend.DatabaseWrapper({
     16                'DATABASE_HOST': settings.DATABASE_HOST,
     17                'DATABASE_NAME': settings.DATABASE_NAME,
     18                'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
     19                'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
     20                'DATABASE_PORT': settings.DATABASE_PORT,
     21                'DATABASE_USER': settings.DATABASE_USER,
     22                'TIME_ZONE': settings.TIME_ZONE,
     23            })
     24
     25            # Put both DB connections into managed transaction mode
     26            transaction.enter_transaction_management(True)
     27            self.conn2._enter_transaction_management(True)
     28
     29        def tearDown(self):
     30            # Close down the second connection.
     31            self.conn2.close()
     32
     33        def test_concurrent_delete(self):
     34            "Deletes on concurrent transactions don't collide and lock the database"
     35
     36            # Create some dummy data
     37            c1 = Child(id=1, data=1)
     38            c2 = Child(id=2, data=2)
     39            c1.save()
     40            c2.save()
     41
     42            p1 = Parent(id=1)
     43            p2 = Parent(id=2)
     44
     45            p1.ref = c1
     46            p2.ref = c2
     47
     48            p1.save()
     49            p2.save()
     50            transaction.commit()
     51
     52            self.assertEquals(2, Child.objects.count())
     53            self.assertEquals(2, Parent.objects.count())
     54
     55            # Now with connection 1 committed, delete something using connection 2
     56            # ... but don't commit the deletion.
     57
     58            cursor2 = self.conn2.cursor()
     59
     60            cursor2.execute('DELETE from delete_regress_parent WHERE id=1')
     61            cursor2.execute('DELETE from delete_regress_child WHERE id=1')
     62
     63            # NOTE! If you uncomment this line, the test will pass.
     64            # However, the test _should_ pass without the need for this commit
     65            # self.conn2._commit()
     66
     67            # Back on connection 1, try to delete some objects that will result
     68            # in the bulk deletion collect operation finding the same objects
     69            # that have just been deleted in the second connection.
     70
     71            # This command will not return lock
     72            Child.objects.filter(pk=1).delete()
     73
     74            self.conn2._commit()
     75            transaction.commit()
     76
     77            self.assertEquals(1, Child.objects.count())
     78            self.assertEquals(1, Parent.objects.count())
     79 No newline at end of file
Back to Top