Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11101: Rewrote the sequence reset SQL for Oracle to prevent it…

… from performing an implicit commit that caused all fixtures to be automatically committed, causing a large number of test failures.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14537 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ee6bec6c092da5466be563cef1ecf3bcc45199f6 1 parent 590bde8
Ian Kelly authored November 12, 2010
20  django/db/backends/oracle/base.py
@@ -669,19 +669,15 @@ def _get_sequence_reset_sql():
669 669
     # TODO: colorize this SQL code with style.SQL_KEYWORD(), etc.
670 670
     return """
671 671
 DECLARE
672  
-    startvalue integer;
673  
-    cval integer;
  672
+    table_value integer;
  673
+    seq_value integer;
674 674
 BEGIN
675  
-    LOCK TABLE %(table)s IN SHARE MODE;
676  
-    SELECT NVL(MAX(%(column)s), 0) INTO startvalue FROM %(table)s;
677  
-    SELECT "%(sequence)s".nextval INTO cval FROM dual;
678  
-    cval := startvalue - cval;
679  
-    IF cval != 0 THEN
680  
-        EXECUTE IMMEDIATE 'ALTER SEQUENCE "%(sequence)s" MINVALUE 0 INCREMENT BY '||cval;
681  
-        SELECT "%(sequence)s".nextval INTO cval FROM dual;
682  
-        EXECUTE IMMEDIATE 'ALTER SEQUENCE "%(sequence)s" INCREMENT BY 1';
683  
-    END IF;
684  
-    COMMIT;
  675
+    SELECT NVL(MAX(%(column)s), 0) INTO table_value FROM %(table)s;
  676
+    SELECT NVL(last_number - cache_size, 0) INTO seq_value FROM user_sequences
  677
+           WHERE sequence_name = '%(sequence)s';
  678
+    WHILE table_value > seq_value LOOP
  679
+        SELECT "%(sequence)s".nextval INTO seq_value FROM dual;
  680
+    END LOOP;
685 681
 END;
686 682
 /"""
687 683
 
9  tests/regressiontests/fixtures_regress/fixtures/thingy.json
... ...
@@ -0,0 +1,9 @@
  1
+[
  2
+    {
  3
+        "pk": "1",
  4
+        "model": "fixtures_regress.thingy",
  5
+        "fields": {
  6
+            "name": "Whatchamacallit"
  7
+        }
  8
+    }
  9
+]
4  tests/regressiontests/fixtures_regress/models.py
@@ -225,3 +225,7 @@ def natural_key(self):
225 225
         return self.name
226 226
     natural_key.dependencies = ['fixtures_regress.book']
227 227
 
  228
+
  229
+# Model for regression test of #11101
  230
+class Thingy(models.Model):
  231
+    name = models.CharField(max_length=255)
28  tests/regressiontests/fixtures_regress/tests.py
@@ -12,7 +12,8 @@
12 12
 from django.core.management.commands.dumpdata import sort_dependencies
13 13
 from django.core.management.base import CommandError
14 14
 from django.db.models import signals
15  
-from django.test import TestCase
  15
+from django.db import transaction
  16
+from django.test import TestCase, TransactionTestCase
16 17
 
17 18
 from models import Animal, Stuff
18 19
 from models import Absolute, Parent, Child
@@ -21,6 +22,7 @@
21 22
 from models import NKChild, RefToNKChild
22 23
 from models import Circle1, Circle2, Circle3
23 24
 from models import ExternalDependency
  25
+from models import Thingy
24 26
 
25 27
 
26 28
 pre_save_checks = []
@@ -57,7 +59,7 @@ def test_duplicate_pk(self):
57 59
             weight=2.2
58 60
         )
59 61
         animal.save()
60  
-        self.assertEqual(animal.id, 2)
  62
+        self.assertGreater(animal.id, 1)
61 63
 
62 64
     def test_pretty_print_xml(self):
63 65
         """
@@ -315,7 +317,8 @@ def test_dumpdata_uses_default_manager(self):
315 317
 
316 318
         lion_json = '{"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}'
317 319
         emu_json = '{"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}'
318  
-        platypus_json = '{"pk": 11, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}'
  320
+        platypus_json = '{"pk": %d, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}'
  321
+        platypus_json = platypus_json % animal.pk
319 322
 
320 323
         self.assertEqual(len(data), len('[%s]' % ', '.join([lion_json, emu_json, platypus_json])))
321 324
         self.assertTrue(lion_json in data)
@@ -575,3 +578,22 @@ def test_normal_pk(self):
575 578
             books.__repr__(),
576 579
             """[<Book: Cryptonomicon by Neal Stephenson (available at Amazon, Borders)>, <Book: Ender's Game by Orson Scott Card (available at Collins Bookstore)>, <Book: Permutation City by Greg Egan (available at Angus and Robertson)>]"""
577 580
         )
  581
+
  582
+
  583
+class TestTicket11101(TransactionTestCase):
  584
+
  585
+    def ticket_11101(self):
  586
+        management.call_command(
  587
+            'loaddata',
  588
+            'thingy.json',
  589
+            verbosity=0,
  590
+            commit=False
  591
+        )
  592
+        self.assertEqual(Thingy.objects.count(), 1)
  593
+        transaction.rollback()
  594
+        self.assertEqual(Thingy.objects.count(), 0)
  595
+
  596
+    def test_ticket_11101(self):
  597
+        """Test that fixtures can be rolled back (ticket #11101)."""
  598
+        ticket_11101 = transaction.commit_manually(self.ticket_11101)
  599
+        ticket_11101()

0 notes on commit ee6bec6

Please sign in to comment.
Something went wrong with that request. Please try again.