Commit 4e29b70b authored by Luke Plant's avatar Luke Plant
Browse files

Fixed #17530 - Fixture loading with deferred constraints broken for Postgres...

Fixed #17530 - Fixture loading with deferred constraints broken for Postgres for fixtures from multiple dirs/files

Thanks to claudep for the review.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17372 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 64229eb3
Loading
Loading
Loading
Loading
+111 −107
Original line number Diff line number Diff line
@@ -104,6 +104,9 @@ class Command(BaseCommand):
                app_module_paths.append(app.__file__)

        app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]

        try:
            with connection.constraint_checks_disabled():
                for fixture_label in fixture_labels:
                    parts = fixture_label.split('.')

@@ -166,25 +169,24 @@ class Command(BaseCommand):
                                    self.stdout.write("No %s fixture '%s' in %s.\n" % \
                                        (format, fixture_name, humanize(fixture_dir)))
                            else:
                                try:
                                    if label_found:
                            fixture.close()
                                        self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
                                            (fixture_name, humanize(fixture_dir))))
                                        if commit:
                                            transaction.rollback(using=using)
                                            transaction.leave_transaction_management(using=using)
                                        return
                        else:

                                    fixture_count += 1
                                    objects_in_fixture = 0
                                    loaded_objects_in_fixture = 0
                                    if verbosity >= 2:
                                        self.stdout.write("Installing %s fixture '%s' from %s.\n" % \
                                            (format, fixture_name, humanize(fixture_dir)))
                            try:

                                    objects = serializers.deserialize(format, fixture, using=using)

                                with connection.constraint_checks_disabled():
                                    for obj in objects:
                                        objects_in_fixture += 1
                                        if router.allow_syncdb(using, obj.object.__class__):
@@ -201,18 +203,31 @@ class Command(BaseCommand):
                                                    }
                                                raise e.__class__, e.__class__(msg), sys.exc_info()[2]

                                    loaded_object_count += loaded_objects_in_fixture
                                    fixture_object_count += objects_in_fixture
                                    label_found = True
                                finally:
                                    fixture.close()

                                # If the fixture we loaded contains 0 objects, assume that an
                                # error was encountered during fixture loading.
                                if objects_in_fixture == 0:
                                    self.stderr.write(
                                        self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
                                            (fixture_name)))
                                    if commit:
                                        transaction.rollback(using=using)
                                        transaction.leave_transaction_management(using=using)
                                    return

            # Since we disabled constraint checks, we must manually check for
            # any invalid keys that might have been added
            table_names = [model._meta.db_table for model in models]
            connection.check_constraints(table_names=table_names)

                                loaded_object_count += loaded_objects_in_fixture
                                fixture_object_count += objects_in_fixture
                                label_found = True
        except (SystemExit, KeyboardInterrupt):
            raise
        except Exception:
                                fixture.close()
            if commit:
                transaction.rollback(using=using)
                transaction.leave_transaction_management(using=using)
@@ -224,18 +239,7 @@ class Command(BaseCommand):
                         (full_path, ''.join(traceback.format_exception(sys.exc_type,
                             sys.exc_value, sys.exc_traceback)))))
            return
                            fixture.close()

                            # If the fixture we loaded contains 0 objects, assume that an
                            # error was encountered during fixture loading.
                            if objects_in_fixture == 0:
                                self.stderr.write(
                                    self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
                                        (fixture_name)))
                                if commit:
                                    transaction.rollback(using=using)
                                    transaction.leave_transaction_management(using=using)
                                return

        # If we found even one object in a fixture, we need to reset the
        # database sequences.
+10 −0
Original line number Diff line number Diff line
[
    {
        "pk": 1,
        "model": "fixtures_regress.book",
        "fields": {
            "name": "Cryptonomicon",
            "author": 4
        }
    }
]
+9 −0
Original line number Diff line number Diff line
[
    {
        "pk": "4",
        "model": "fixtures_regress.person",
        "fields": {
            "name": "Neal Stephenson"
        }
    }
]
+20 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ from django.db import transaction
from django.db.models import signals
from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
    skipUnlessDBFeature)
from django.test.utils import override_settings

from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
    Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3,
@@ -390,6 +391,25 @@ class TestFixtures(TestCase):
            stderr.getvalue().startswith('Problem installing fixture')
        )

    _cur_dir = os.path.dirname(os.path.abspath(__file__))

    @override_settings(FIXTURE_DIRS=[os.path.join(_cur_dir, 'fixtures_1'),
                                     os.path.join(_cur_dir, 'fixtures_2')])
    def test_loaddata_forward_refs_split_fixtures(self):
        """
        Regression for #17530 - should be able to cope with forward references
        when the fixtures are not in the same files or directories.
        """
        management.call_command(
            'loaddata',
            'forward_ref_1.json',
            'forward_ref_2.json',
            verbosity=0,
            commit=False
        )
        self.assertEqual(Book.objects.all()[0].id, 1)
        self.assertEqual(Person.objects.all()[0].id, 4)

    def test_loaddata_no_fixture_specified(self):
        """
        Regression for #7043 - Error is quickly reported when no fixtures is provided in the command line.