Django

Code

Changeset 9357

Show
Ignore:
Timestamp:
11/06/08 05:19:13 (8 months ago)
Author:
russellm
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/commands/loaddata.py

    r9110 r9357  
    3636        fixture_count = 0 
    3737        object_count = 0 
    38         objects_per_fixture = [] 
    3938        models = set() 
    4039 
     
    104103                        else: 
    105104                            fixture_count += 1 
    106                             objects_per_fixture.append(0) 
     105                            objects_in_fixture = 0 
    107106                            if verbosity > 0: 
    108107                                print "Installing %s fixture '%s' from %s." % \ 
     
    111110                                objects = serializers.deserialize(format, fixture) 
    112111                                for obj in objects: 
    113                                     object_count += 1 
    114                                     objects_per_fixture[-1] += 1 
     112                                    objects_in_fixture += 1 
    115113                                    models.add(obj.object.__class__) 
    116114                                    obj.save() 
     115                                object_count += objects_in_fixture 
    117116                                label_found = True 
    118117                            except (SystemExit, KeyboardInterrupt): 
     
    132131                                return 
    133132                            fixture.close() 
     133 
     134                            # If the fixture we loaded contains 0 objects, assume that an 
     135                            # error was encountered during fixture loading. 
     136                            if objects_in_fixture == 0: 
     137                                sys.stderr.write( 
     138                                    self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % 
     139                                        (fixture_name))) 
     140                                transaction.rollback() 
     141                                transaction.leave_transaction_management() 
     142                                return 
    134143                    except: 
    135144                        if verbosity > 1: 
    136145                            print "No %s fixture '%s' in %s." % \ 
    137146                                (format, fixture_name, humanize(fixture_dir)) 
    138  
    139  
    140         # If any of the fixtures we loaded contain 0 objects, assume that an 
    141         # error was encountered during fixture loading. 
    142         if 0 in objects_per_fixture: 
    143             sys.stderr.write( 
    144                 self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % 
    145                     (fixture_name))) 
    146             transaction.rollback() 
    147             transaction.leave_transaction_management() 
    148             return 
    149147 
    150148        # If we found even one object in a fixture, we need to reset the 
  • django/trunk/tests/regressiontests/fixtures_regress/models.py

    r8515 r9357  
    88    latin_name = models.CharField(max_length=150) 
    99    count = models.IntegerField() 
    10      
     10 
    1111    def __unicode__(self): 
    1212        return self.common_name 
     
    5757    title = models.CharField(max_length=255) 
    5858    channels = models.ManyToManyField(Channel) 
    59      
     59 
    6060    class Meta: 
    6161        ordering = ('id',) 
     
    114114No fixture data found for 'bad_fixture2'. (File format may be invalid.) 
    115115 
     116# Loading a fixture file with no data returns an error 
     117>>> management.call_command('loaddata', 'empty', verbosity=0) 
     118No fixture data found for 'empty'. (File format may be invalid.) 
     119 
     120# If any of the fixtures contain an error, loading is aborted 
     121# (Regression for #9011 - error message is correct) 
     122>>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0) 
     123No fixture data found for 'bad_fixture2'. (File format may be invalid.) 
     124 
    116125>>> sys.stderr = savestderr 
    117126 
     
    124133 
    125134############################################### 
    126 # Test for ticket #7572 -- MySQL has a problem if the same connection is  
     135# Test for ticket #7572 -- MySQL has a problem if the same connection is 
    127136# used to create tables, load data, and then query over that data. 
    128137# To compensate, we close the connection after running loaddata.