Fixed #10200 -- Raised CommandError when errors happen in loaddata.
This commit is contained in:
parent
4464bbba15
commit
6fd1950a4e
|
@ -7,7 +7,7 @@ import traceback
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.core.management.color import no_style
|
from django.core.management.color import no_style
|
||||||
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
|
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
|
||||||
IntegrityError, DatabaseError)
|
IntegrityError, DatabaseError)
|
||||||
|
@ -36,11 +36,10 @@ class Command(BaseCommand):
|
||||||
connection = connections[using]
|
connection = connections[using]
|
||||||
|
|
||||||
if not len(fixture_labels):
|
if not len(fixture_labels):
|
||||||
self.stderr.write(
|
raise CommandError(
|
||||||
"No database fixture specified. Please provide the path of at "
|
"No database fixture specified. Please provide the path of at "
|
||||||
"least one fixture in the command line."
|
"least one fixture in the command line."
|
||||||
)
|
)
|
||||||
return
|
|
||||||
|
|
||||||
verbosity = int(options.get('verbosity'))
|
verbosity = int(options.get('verbosity'))
|
||||||
show_traceback = options.get('traceback')
|
show_traceback = options.get('traceback')
|
||||||
|
@ -126,13 +125,9 @@ class Command(BaseCommand):
|
||||||
if verbosity >= 2:
|
if verbosity >= 2:
|
||||||
self.stdout.write("Loading '%s' fixtures..." % fixture_name)
|
self.stdout.write("Loading '%s' fixtures..." % fixture_name)
|
||||||
else:
|
else:
|
||||||
self.stderr.write(
|
raise CommandError(
|
||||||
"Problem installing fixture '%s': %s is not a known serialization format." %
|
"Problem installing fixture '%s': %s is not a known serialization format." %
|
||||||
(fixture_name, format))
|
(fixture_name, format))
|
||||||
if commit:
|
|
||||||
transaction.rollback(using=using)
|
|
||||||
transaction.leave_transaction_management(using=using)
|
|
||||||
return
|
|
||||||
|
|
||||||
if os.path.isabs(fixture_name):
|
if os.path.isabs(fixture_name):
|
||||||
fixture_dirs = [fixture_name]
|
fixture_dirs = [fixture_name]
|
||||||
|
@ -167,12 +162,8 @@ class Command(BaseCommand):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if label_found:
|
if label_found:
|
||||||
self.stderr.write("Multiple fixtures named '%s' in %s. Aborting." %
|
raise CommandError("Multiple fixtures named '%s' in %s. Aborting." %
|
||||||
(fixture_name, humanize(fixture_dir)))
|
(fixture_name, humanize(fixture_dir)))
|
||||||
if commit:
|
|
||||||
transaction.rollback(using=using)
|
|
||||||
transaction.leave_transaction_management(using=using)
|
|
||||||
return
|
|
||||||
|
|
||||||
fixture_count += 1
|
fixture_count += 1
|
||||||
objects_in_fixture = 0
|
objects_in_fixture = 0
|
||||||
|
@ -191,13 +182,13 @@ class Command(BaseCommand):
|
||||||
try:
|
try:
|
||||||
obj.save(using=using)
|
obj.save(using=using)
|
||||||
except (DatabaseError, IntegrityError) as e:
|
except (DatabaseError, IntegrityError) as e:
|
||||||
msg = "Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % {
|
e.args = ("Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % {
|
||||||
'app_label': obj.object._meta.app_label,
|
'app_label': obj.object._meta.app_label,
|
||||||
'object_name': obj.object._meta.object_name,
|
'object_name': obj.object._meta.object_name,
|
||||||
'pk': obj.object.pk,
|
'pk': obj.object.pk,
|
||||||
'error_msg': e
|
'error_msg': e
|
||||||
}
|
},)
|
||||||
raise e.__class__, e.__class__(msg), sys.exc_info()[2]
|
raise
|
||||||
|
|
||||||
loaded_object_count += loaded_objects_in_fixture
|
loaded_object_count += loaded_objects_in_fixture
|
||||||
fixture_object_count += objects_in_fixture
|
fixture_object_count += objects_in_fixture
|
||||||
|
@ -208,13 +199,9 @@ class Command(BaseCommand):
|
||||||
# If the fixture we loaded contains 0 objects, assume that an
|
# If the fixture we loaded contains 0 objects, assume that an
|
||||||
# error was encountered during fixture loading.
|
# error was encountered during fixture loading.
|
||||||
if objects_in_fixture == 0:
|
if objects_in_fixture == 0:
|
||||||
self.stderr.write(
|
raise CommandError(
|
||||||
"No fixture data found for '%s'. (File format may be invalid.)" %
|
"No fixture data found for '%s'. (File format may be invalid.)" %
|
||||||
(fixture_name))
|
(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
|
# Since we disabled constraint checks, we must manually check for
|
||||||
# any invalid keys that might have been added
|
# any invalid keys that might have been added
|
||||||
|
@ -223,19 +210,13 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
except (SystemExit, KeyboardInterrupt):
|
except (SystemExit, KeyboardInterrupt):
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception as e:
|
||||||
if commit:
|
if commit:
|
||||||
transaction.rollback(using=using)
|
transaction.rollback(using=using)
|
||||||
transaction.leave_transaction_management(using=using)
|
transaction.leave_transaction_management(using=using)
|
||||||
if show_traceback:
|
if not isinstance(e, CommandError):
|
||||||
traceback.print_exc()
|
e.args = ("Problem installing fixture '%s': %s" % (full_path, e),)
|
||||||
else:
|
raise
|
||||||
self.stderr.write(
|
|
||||||
"Problem installing fixture '%s': %s" %
|
|
||||||
(full_path, ''.join(traceback.format_exception(sys.exc_type,
|
|
||||||
sys.exc_value, sys.exc_traceback))))
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# If we found even one object in a fixture, we need to reset the
|
# If we found even one object in a fixture, we need to reset the
|
||||||
# database sequences.
|
# database sequences.
|
||||||
|
|
|
@ -4,7 +4,7 @@ import StringIO
|
||||||
|
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.db import connection
|
from django.db import connection, IntegrityError
|
||||||
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
||||||
|
|
||||||
from .models import Article, Book, Spy, Tag, Visa
|
from .models import Article, Book, Spy, Tag, Visa
|
||||||
|
@ -232,11 +232,9 @@ class FixtureLoadingTests(TestCase):
|
||||||
|
|
||||||
def test_ambiguous_compressed_fixture(self):
|
def test_ambiguous_compressed_fixture(self):
|
||||||
# The name "fixture5" is ambigous, so loading it will raise an error
|
# The name "fixture5" is ambigous, so loading it will raise an error
|
||||||
new_io = StringIO.StringIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command('loaddata', 'fixture5', verbosity=0, stderr=new_io, commit=False)
|
"Multiple fixtures named 'fixture5'"):
|
||||||
output = new_io.getvalue().strip().split('\n')
|
management.call_command('loaddata', 'fixture5', verbosity=0, commit=False)
|
||||||
self.assertEqual(len(output), 1)
|
|
||||||
self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture5'"))
|
|
||||||
|
|
||||||
def test_db_loading(self):
|
def test_db_loading(self):
|
||||||
# Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
|
# Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
|
||||||
|
@ -258,10 +256,9 @@ class FixtureLoadingTests(TestCase):
|
||||||
# is closed at the end of each test.
|
# is closed at the end of each test.
|
||||||
if connection.vendor == 'mysql':
|
if connection.vendor == 'mysql':
|
||||||
connection.cursor().execute("SET sql_mode = 'TRADITIONAL'")
|
connection.cursor().execute("SET sql_mode = 'TRADITIONAL'")
|
||||||
new_io = StringIO.StringIO()
|
with self.assertRaisesRegexp(IntegrityError,
|
||||||
management.call_command('loaddata', 'invalid.json', verbosity=0, stderr=new_io, commit=False)
|
"Could not load fixtures.Article\(pk=1\): .*$"):
|
||||||
output = new_io.getvalue().strip().split('\n')
|
management.call_command('loaddata', 'invalid.json', verbosity=0, commit=False)
|
||||||
self.assertRegexpMatches(output[-1], "Error: Could not load fixtures.Article\(pk=1\): .*$")
|
|
||||||
|
|
||||||
def test_loading_using(self):
|
def test_loading_using(self):
|
||||||
# Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
|
# Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
|
||||||
|
@ -316,11 +313,9 @@ class FixtureTransactionTests(TransactionTestCase):
|
||||||
|
|
||||||
# Try to load fixture 2 using format discovery; this will fail
|
# Try to load fixture 2 using format discovery; this will fail
|
||||||
# because there are two fixture2's in the fixtures directory
|
# because there are two fixture2's in the fixtures directory
|
||||||
new_io = StringIO.StringIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command('loaddata', 'fixture2', verbosity=0, stderr=new_io)
|
"Multiple fixtures named 'fixture2'"):
|
||||||
output = new_io.getvalue().strip().split('\n')
|
management.call_command('loaddata', 'fixture2', verbosity=0)
|
||||||
self.assertEqual(len(output), 1)
|
|
||||||
self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture2'"))
|
|
||||||
|
|
||||||
# object list is unaffected
|
# object list is unaffected
|
||||||
self.assertQuerysetEqual(Article.objects.all(), [
|
self.assertQuerysetEqual(Article.objects.all(), [
|
||||||
|
|
|
@ -9,7 +9,7 @@ from io import BytesIO
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.core.management.base import CommandError
|
from django.core.management.base import CommandError
|
||||||
from django.core.management.commands.dumpdata import sort_dependencies
|
from django.core.management.commands.dumpdata import sort_dependencies
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.db.models import signals
|
from django.db.models import signals
|
||||||
from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
|
from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
|
||||||
skipUnlessDBFeature)
|
skipUnlessDBFeature)
|
||||||
|
@ -116,18 +116,15 @@ class TestFixtures(TestCase):
|
||||||
Test for ticket #4371 -- Loading data of an unknown format should fail
|
Test for ticket #4371 -- Loading data of an unknown format should fail
|
||||||
Validate that error conditions are caught correctly
|
Validate that error conditions are caught correctly
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"Problem installing fixture 'bad_fixture1': "
|
||||||
'loaddata',
|
"unkn is not a known serialization format."):
|
||||||
'bad_fixture1.unkn',
|
management.call_command(
|
||||||
verbosity=0,
|
'loaddata',
|
||||||
commit=False,
|
'bad_fixture1.unkn',
|
||||||
stderr=stderr,
|
verbosity=0,
|
||||||
)
|
commit=False,
|
||||||
self.assertEqual(
|
)
|
||||||
stderr.getvalue(),
|
|
||||||
"Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_invalid_data(self):
|
def test_invalid_data(self):
|
||||||
"""
|
"""
|
||||||
|
@ -135,18 +132,14 @@ class TestFixtures(TestCase):
|
||||||
using explicit filename.
|
using explicit filename.
|
||||||
Validate that error conditions are caught correctly
|
Validate that error conditions are caught correctly
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'bad_fixture2.xml',
|
'loaddata',
|
||||||
verbosity=0,
|
'bad_fixture2.xml',
|
||||||
commit=False,
|
verbosity=0,
|
||||||
stderr=stderr,
|
commit=False,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
|
||||||
stderr.getvalue(),
|
|
||||||
"No fixture data found for 'bad_fixture2'. (File format may be invalid.)\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_invalid_data_no_ext(self):
|
def test_invalid_data_no_ext(self):
|
||||||
"""
|
"""
|
||||||
|
@ -154,54 +147,42 @@ class TestFixtures(TestCase):
|
||||||
without file extension.
|
without file extension.
|
||||||
Validate that error conditions are caught correctly
|
Validate that error conditions are caught correctly
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'bad_fixture2',
|
'loaddata',
|
||||||
verbosity=0,
|
'bad_fixture2',
|
||||||
commit=False,
|
verbosity=0,
|
||||||
stderr=stderr,
|
commit=False,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
|
||||||
stderr.getvalue(),
|
|
||||||
"No fixture data found for 'bad_fixture2'. (File format may be invalid.)\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
"""
|
"""
|
||||||
Test for ticket #4371 -- Loading a fixture file with no data returns an error.
|
Test for ticket #4371 -- Loading a fixture file with no data returns an error.
|
||||||
Validate that error conditions are caught correctly
|
Validate that error conditions are caught correctly
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"No fixture data found for 'empty'. \(File format may be invalid.\)"):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'empty',
|
'loaddata',
|
||||||
verbosity=0,
|
'empty',
|
||||||
commit=False,
|
verbosity=0,
|
||||||
stderr=stderr,
|
commit=False,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
|
||||||
stderr.getvalue(),
|
|
||||||
"No fixture data found for 'empty'. (File format may be invalid.)\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_error_message(self):
|
def test_error_message(self):
|
||||||
"""
|
"""
|
||||||
(Regression for #9011 - error message is correct)
|
(Regression for #9011 - error message is correct)
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"^No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)$"):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'bad_fixture2',
|
'loaddata',
|
||||||
'animal',
|
'bad_fixture2',
|
||||||
verbosity=0,
|
'animal',
|
||||||
commit=False,
|
verbosity=0,
|
||||||
stderr=stderr,
|
commit=False,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
|
||||||
stderr.getvalue(),
|
|
||||||
"No fixture data found for 'bad_fixture2'. (File format may be invalid.)\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_pg_sequence_resetting_checks(self):
|
def test_pg_sequence_resetting_checks(self):
|
||||||
"""
|
"""
|
||||||
|
@ -357,17 +338,14 @@ class TestFixtures(TestCase):
|
||||||
"""
|
"""
|
||||||
Regression for #3615 - Ensure data with nonexistent child key references raises error
|
Regression for #3615 - Ensure data with nonexistent child key references raises error
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(IntegrityError,
|
||||||
management.call_command(
|
"Problem installing fixture"):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'forward_ref_bad_data.json',
|
'loaddata',
|
||||||
verbosity=0,
|
'forward_ref_bad_data.json',
|
||||||
commit=False,
|
verbosity=0,
|
||||||
stderr=stderr,
|
commit=False,
|
||||||
)
|
)
|
||||||
self.assertTrue(
|
|
||||||
stderr.getvalue().startswith('Problem installing fixture')
|
|
||||||
)
|
|
||||||
|
|
||||||
_cur_dir = os.path.dirname(os.path.abspath(__file__))
|
_cur_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
@ -392,16 +370,14 @@ class TestFixtures(TestCase):
|
||||||
"""
|
"""
|
||||||
Regression for #7043 - Error is quickly reported when no fixtures is provided in the command line.
|
Regression for #7043 - Error is quickly reported when no fixtures is provided in the command line.
|
||||||
"""
|
"""
|
||||||
stderr = BytesIO()
|
with self.assertRaisesRegexp(management.CommandError,
|
||||||
management.call_command(
|
"No database fixture specified. Please provide the path of "
|
||||||
'loaddata',
|
"at least one fixture in the command line."):
|
||||||
verbosity=0,
|
management.call_command(
|
||||||
commit=False,
|
'loaddata',
|
||||||
stderr=stderr,
|
verbosity=0,
|
||||||
)
|
commit=False,
|
||||||
self.assertEqual(
|
)
|
||||||
stderr.getvalue(), 'No database fixture specified. Please provide the path of at least one fixture in the command line.\n'
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_loaddata_not_existant_fixture_file(self):
|
def test_loaddata_not_existant_fixture_file(self):
|
||||||
stdout_output = BytesIO()
|
stdout_output = BytesIO()
|
||||||
|
|
Loading…
Reference in New Issue