[1.0.X] Fixed #9011 -- Corrected handling of fixture files that contain errors to correctly report the broken fixture name. Thanks to jlrivitti@gmail.com for the report and initial patch.
Merge of [9357] and [9358] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9359 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
697f73f488
commit
6be20bf64c
|
@ -28,19 +28,18 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
verbosity = int(options.get('verbosity', 1))
|
verbosity = int(options.get('verbosity', 1))
|
||||||
show_traceback = options.get('traceback', False)
|
show_traceback = options.get('traceback', False)
|
||||||
|
|
||||||
# commit is a stealth option - it isn't really useful as
|
# commit is a stealth option - it isn't really useful as
|
||||||
# a command line option, but it can be useful when invoking
|
# a command line option, but it can be useful when invoking
|
||||||
# loaddata from within another script.
|
# loaddata from within another script.
|
||||||
# If commit=True, loaddata will use its own transaction;
|
# If commit=True, loaddata will use its own transaction;
|
||||||
# if commit=False, the data load SQL will become part of
|
# if commit=False, the data load SQL will become part of
|
||||||
# the transaction in place when loaddata was invoked.
|
# the transaction in place when loaddata was invoked.
|
||||||
commit = options.get('commit', True)
|
commit = options.get('commit', True)
|
||||||
|
|
||||||
# Keep a count of the installed objects and fixtures
|
# Keep a count of the installed objects and fixtures
|
||||||
fixture_count = 0
|
fixture_count = 0
|
||||||
object_count = 0
|
object_count = 0
|
||||||
objects_per_fixture = []
|
|
||||||
models = set()
|
models = set()
|
||||||
|
|
||||||
humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
|
humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
|
||||||
|
@ -108,17 +107,17 @@ class Command(BaseCommand):
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
fixture_count += 1
|
fixture_count += 1
|
||||||
objects_per_fixture.append(0)
|
objects_in_fixture = 0
|
||||||
if verbosity > 0:
|
if verbosity > 0:
|
||||||
print "Installing %s fixture '%s' from %s." % \
|
print "Installing %s fixture '%s' from %s." % \
|
||||||
(format, fixture_name, humanize(fixture_dir))
|
(format, fixture_name, humanize(fixture_dir))
|
||||||
try:
|
try:
|
||||||
objects = serializers.deserialize(format, fixture)
|
objects = serializers.deserialize(format, fixture)
|
||||||
for obj in objects:
|
for obj in objects:
|
||||||
object_count += 1
|
objects_in_fixture += 1
|
||||||
objects_per_fixture[-1] += 1
|
|
||||||
models.add(obj.object.__class__)
|
models.add(obj.object.__class__)
|
||||||
obj.save()
|
obj.save()
|
||||||
|
object_count += objects_in_fixture
|
||||||
label_found = True
|
label_found = True
|
||||||
except (SystemExit, KeyboardInterrupt):
|
except (SystemExit, KeyboardInterrupt):
|
||||||
raise
|
raise
|
||||||
|
@ -136,22 +135,21 @@ class Command(BaseCommand):
|
||||||
(full_path, traceback.format_exc())))
|
(full_path, traceback.format_exc())))
|
||||||
return
|
return
|
||||||
fixture.close()
|
fixture.close()
|
||||||
|
|
||||||
|
# If the fixture we loaded contains 0 objects, assume that an
|
||||||
|
# error was encountered during fixture loading.
|
||||||
|
if objects_in_fixture == 0:
|
||||||
|
sys.stderr.write(
|
||||||
|
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
|
||||||
|
(fixture_name)))
|
||||||
|
transaction.rollback()
|
||||||
|
transaction.leave_transaction_management()
|
||||||
|
return
|
||||||
except:
|
except:
|
||||||
if verbosity > 1:
|
if verbosity > 1:
|
||||||
print "No %s fixture '%s' in %s." % \
|
print "No %s fixture '%s' in %s." % \
|
||||||
(format, fixture_name, humanize(fixture_dir))
|
(format, fixture_name, humanize(fixture_dir))
|
||||||
|
|
||||||
|
|
||||||
# If any of the fixtures we loaded contain 0 objects, assume that an
|
|
||||||
# error was encountered during fixture loading.
|
|
||||||
if 0 in objects_per_fixture:
|
|
||||||
sys.stderr.write(
|
|
||||||
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
|
|
||||||
(fixture_name)))
|
|
||||||
transaction.rollback()
|
|
||||||
transaction.leave_transaction_management()
|
|
||||||
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.
|
||||||
if object_count > 0:
|
if object_count > 0:
|
||||||
|
@ -161,7 +159,7 @@ class Command(BaseCommand):
|
||||||
print "Resetting sequences"
|
print "Resetting sequences"
|
||||||
for line in sequence_sql:
|
for line in sequence_sql:
|
||||||
cursor.execute(line)
|
cursor.execute(line)
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
transaction.commit()
|
transaction.commit()
|
||||||
transaction.leave_transaction_management()
|
transaction.leave_transaction_management()
|
||||||
|
@ -172,7 +170,7 @@ class Command(BaseCommand):
|
||||||
else:
|
else:
|
||||||
if verbosity > 0:
|
if verbosity > 0:
|
||||||
print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
|
print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
|
||||||
|
|
||||||
# Close the DB connection. This is required as a workaround for an
|
# Close the DB connection. This is required as a workaround for an
|
||||||
# edge case in MySQL: if the same connection is used to
|
# edge case in MySQL: if the same connection is used to
|
||||||
# create tables, load data, and query, the query can return
|
# create tables, load data, and query, the query can return
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
[]
|
|
@ -7,7 +7,7 @@ class Animal(models.Model):
|
||||||
name = models.CharField(max_length=150)
|
name = models.CharField(max_length=150)
|
||||||
latin_name = models.CharField(max_length=150)
|
latin_name = models.CharField(max_length=150)
|
||||||
count = models.IntegerField()
|
count = models.IntegerField()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.common_name
|
return self.common_name
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class Channel(models.Model):
|
||||||
class Article(models.Model):
|
class Article(models.Model):
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
channels = models.ManyToManyField(Channel)
|
channels = models.ManyToManyField(Channel)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ('id',)
|
ordering = ('id',)
|
||||||
|
|
||||||
|
@ -113,6 +113,15 @@ No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
||||||
>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
|
>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
|
||||||
No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
||||||
|
|
||||||
|
# Loading a fixture file with no data returns an error
|
||||||
|
>>> management.call_command('loaddata', 'empty', verbosity=0)
|
||||||
|
No fixture data found for 'empty'. (File format may be invalid.)
|
||||||
|
|
||||||
|
# If any of the fixtures contain an error, loading is aborted
|
||||||
|
# (Regression for #9011 - error message is correct)
|
||||||
|
>>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0)
|
||||||
|
No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
||||||
|
|
||||||
>>> sys.stderr = savestderr
|
>>> sys.stderr = savestderr
|
||||||
|
|
||||||
###############################################
|
###############################################
|
||||||
|
@ -123,7 +132,7 @@ No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
||||||
>>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0)
|
>>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0)
|
||||||
|
|
||||||
###############################################
|
###############################################
|
||||||
# Test for ticket #7572 -- MySQL has a problem if the same connection is
|
# Test for ticket #7572 -- MySQL has a problem if the same connection is
|
||||||
# used to create tables, load data, and then query over that data.
|
# used to create tables, load data, and then query over that data.
|
||||||
# To compensate, we close the connection after running loaddata.
|
# To compensate, we close the connection after running loaddata.
|
||||||
# This ensures that a new connection is opened when test queries are issued.
|
# This ensures that a new connection is opened when test queries are issued.
|
||||||
|
|
Loading…
Reference in New Issue