From ee6bec6c092da5466be563cef1ecf3bcc45199f6 Mon Sep 17 00:00:00 2001 From: Ian Kelly Date: Fri, 12 Nov 2010 01:44:22 +0000 Subject: [PATCH] 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 --- django/db/backends/oracle/base.py | 20 ++++++------- .../fixtures_regress/fixtures/thingy.json | 9 ++++++ .../fixtures_regress/models.py | 4 +++ .../regressiontests/fixtures_regress/tests.py | 28 +++++++++++++++++-- 4 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 tests/regressiontests/fixtures_regress/fixtures/thingy.json diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index db16307866..625d95051a 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -669,19 +669,15 @@ def _get_sequence_reset_sql(): # TODO: colorize this SQL code with style.SQL_KEYWORD(), etc. return """ DECLARE - startvalue integer; - cval integer; + table_value integer; + seq_value integer; BEGIN - LOCK TABLE %(table)s IN SHARE MODE; - SELECT NVL(MAX(%(column)s), 0) INTO startvalue FROM %(table)s; - SELECT "%(sequence)s".nextval INTO cval FROM dual; - cval := startvalue - cval; - IF cval != 0 THEN - EXECUTE IMMEDIATE 'ALTER SEQUENCE "%(sequence)s" MINVALUE 0 INCREMENT BY '||cval; - SELECT "%(sequence)s".nextval INTO cval FROM dual; - EXECUTE IMMEDIATE 'ALTER SEQUENCE "%(sequence)s" INCREMENT BY 1'; - END IF; - COMMIT; + SELECT NVL(MAX(%(column)s), 0) INTO table_value FROM %(table)s; + SELECT NVL(last_number - cache_size, 0) INTO seq_value FROM user_sequences + WHERE sequence_name = '%(sequence)s'; + WHILE table_value > seq_value LOOP + SELECT "%(sequence)s".nextval INTO seq_value FROM dual; + END LOOP; END; /""" diff --git a/tests/regressiontests/fixtures_regress/fixtures/thingy.json b/tests/regressiontests/fixtures_regress/fixtures/thingy.json new file mode 100644 index 0000000000..1693177b98 --- /dev/null +++ b/tests/regressiontests/fixtures_regress/fixtures/thingy.json @@ -0,0 +1,9 @@ +[ + { + "pk": "1", + "model": "fixtures_regress.thingy", + "fields": { + "name": "Whatchamacallit" + } + } +] diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py index 7c732ce6d4..8736346a66 100644 --- a/tests/regressiontests/fixtures_regress/models.py +++ b/tests/regressiontests/fixtures_regress/models.py @@ -225,3 +225,7 @@ class ExternalDependency(models.Model): return self.name natural_key.dependencies = ['fixtures_regress.book'] + +# Model for regression test of #11101 +class Thingy(models.Model): + name = models.CharField(max_length=255) diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py index e37272aae1..5b27c3405d 100644 --- a/tests/regressiontests/fixtures_regress/tests.py +++ b/tests/regressiontests/fixtures_regress/tests.py @@ -12,7 +12,8 @@ from django.core import management from django.core.management.commands.dumpdata import sort_dependencies from django.core.management.base import CommandError from django.db.models import signals -from django.test import TestCase +from django.db import transaction +from django.test import TestCase, TransactionTestCase from models import Animal, Stuff from models import Absolute, Parent, Child @@ -21,6 +22,7 @@ from models import Store, Person, Book from models import NKChild, RefToNKChild from models import Circle1, Circle2, Circle3 from models import ExternalDependency +from models import Thingy pre_save_checks = [] @@ -57,7 +59,7 @@ class TestFixtures(TestCase): weight=2.2 ) animal.save() - self.assertEqual(animal.id, 2) + self.assertGreater(animal.id, 1) def test_pretty_print_xml(self): """ @@ -315,7 +317,8 @@ class TestFixtures(TestCase): lion_json = '{"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}' emu_json = '{"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}' - platypus_json = '{"pk": 11, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}' + platypus_json = '{"pk": %d, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}' + platypus_json = platypus_json % animal.pk self.assertEqual(len(data), len('[%s]' % ', '.join([lion_json, emu_json, platypus_json]))) self.assertTrue(lion_json in data) @@ -575,3 +578,22 @@ class NaturalKeyFixtureTests(TestCase): books.__repr__(), """[, , ]""" ) + + +class TestTicket11101(TransactionTestCase): + + def ticket_11101(self): + management.call_command( + 'loaddata', + 'thingy.json', + verbosity=0, + commit=False + ) + self.assertEqual(Thingy.objects.count(), 1) + transaction.rollback() + self.assertEqual(Thingy.objects.count(), 0) + + def test_ticket_11101(self): + """Test that fixtures can be rolled back (ticket #11101).""" + ticket_11101 = transaction.commit_manually(self.ticket_11101) + ticket_11101()