114
115
if verbosity > 1:
115
print "Loading '%s' fixtures..." % fixture_name
116
self.stdout.write("Loading '%s' fixtures...\n" % fixture_name)
118
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
119
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
119
120
(fixture_name, format)))
120
121
transaction.rollback(using=using)
121
122
transaction.leave_transaction_management(using=using)
129
130
for fixture_dir in fixture_dirs:
130
131
if verbosity > 1:
131
print "Checking %s for fixtures..." % humanize(fixture_dir)
132
self.stdout.write("Checking %s for fixtures...\n" % humanize(fixture_dir))
133
134
label_found = False
134
135
for combo in product([using, None], formats, compression_formats):
143
144
if verbosity > 1:
144
print "Trying %s for %s fixture '%s'..." % \
145
(humanize(fixture_dir), file_name, fixture_name)
145
self.stdout.write("Trying %s for %s fixture '%s'...\n" % \
146
(humanize(fixture_dir), file_name, fixture_name))
146
147
full_path = os.path.join(fixture_dir, file_name)
147
148
open_method = compression_types[compression_format]
149
150
fixture = open_method(full_path, 'r')
152
print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
153
(fixture_name, humanize(fixture_dir)))
153
self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
154
(fixture_name, humanize(fixture_dir))))
154
155
transaction.rollback(using=using)
155
156
transaction.leave_transaction_management(using=using)
158
159
fixture_count += 1
159
160
objects_in_fixture = 0
161
loaded_objects_in_fixture = 0
160
162
if verbosity > 0:
161
print "Installing %s fixture '%s' from %s." % \
162
(format, fixture_name, humanize(fixture_dir))
163
self.stdout.write("Installing %s fixture '%s' from %s.\n" % \
164
(format, fixture_name, humanize(fixture_dir)))
164
166
objects = serializers.deserialize(format, fixture, using=using)
165
167
for obj in objects:
168
objects_in_fixture += 1
166
169
if router.allow_syncdb(using, obj.object.__class__):
167
objects_in_fixture += 1
170
loaded_objects_in_fixture += 1
168
171
models.add(obj.object.__class__)
169
172
obj.save(using=using)
170
object_count += objects_in_fixture
173
loaded_object_count += loaded_objects_in_fixture
174
fixture_object_count += objects_in_fixture
171
175
label_found = True
172
176
except (SystemExit, KeyboardInterrupt):
179
183
if show_traceback:
180
184
traceback.print_exc()
183
187
self.style.ERROR("Problem installing fixture '%s': %s\n" %
184
188
(full_path, ''.join(traceback.format_exception(sys.exc_type,
185
189
sys.exc_value, sys.exc_traceback)))))
189
193
# If the fixture we loaded contains 0 objects, assume that an
190
194
# error was encountered during fixture loading.
191
195
if objects_in_fixture == 0:
193
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
197
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
195
199
transaction.rollback(using=using)
196
200
transaction.leave_transaction_management(using=using)
199
203
except Exception, e:
200
204
if verbosity > 1:
201
print "No %s fixture '%s' in %s." % \
202
(format, fixture_name, humanize(fixture_dir))
205
self.stdout.write("No %s fixture '%s' in %s.\n" % \
206
(format, fixture_name, humanize(fixture_dir)))
204
208
# If we found even one object in a fixture, we need to reset the
205
209
# database sequences.
210
if loaded_object_count > 0:
207
211
sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
209
213
if verbosity > 1:
210
print "Resetting sequences"
214
self.stdout.write("Resetting sequences\n")
211
215
for line in sequence_sql:
212
216
cursor.execute(line)
215
219
transaction.commit(using=using)
216
220
transaction.leave_transaction_management(using=using)
218
if object_count == 0:
222
if fixture_object_count == 0:
219
223
if verbosity > 0:
220
print "No fixtures found."
224
self.stdout.write("No fixtures found.\n")
222
226
if verbosity > 0:
223
print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
227
if fixture_object_count == loaded_object_count:
228
self.stdout.write("Installed %d object(s) from %d fixture(s)\n" % (
229
loaded_object_count, fixture_count))
231
self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)\n" % (
232
loaded_object_count, fixture_object_count, fixture_count))
225
234
# Close the DB connection. This is required as a workaround for an
226
235
# edge case in MySQL: if the same connection is used to