2011-08-07 08:43:26 +08:00
|
|
|
# This is necessary in Python 2.5 to enable the with statement, in 2.6
|
|
|
|
# and up it is no longer necessary.
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
import sys
|
|
|
|
import os
|
2008-12-01 10:21:33 +08:00
|
|
|
import gzip
|
|
|
|
import zipfile
|
|
|
|
from optparse import make_option
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import serializers
|
2008-12-01 10:21:33 +08:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.management.color import no_style
|
2011-09-22 12:30:20 +08:00
|
|
|
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
|
|
|
|
IntegrityError, DatabaseError)
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db.models import get_apps
|
|
|
|
from django.utils.itercompat import product
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-12-01 10:20:59 +08:00
|
|
|
try:
|
|
|
|
import bz2
|
|
|
|
has_bz2 = True
|
|
|
|
except ImportError:
|
|
|
|
has_bz2 = False
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Installs the named fixture(s) in the database.'
|
2007-09-10 05:57:59 +08:00
|
|
|
args = "fixture [fixture ...]"
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--database', action='store', dest='database',
|
|
|
|
default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load '
|
|
|
|
'fixtures into. Defaults to the "default" database.'),
|
|
|
|
)
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
def handle(self, *fixture_labels, **options):
|
2009-12-22 23:18:51 +08:00
|
|
|
using = options.get('database', DEFAULT_DB_ALIAS)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[using]
|
2007-08-16 14:06:55 +08:00
|
|
|
self.style = no_style()
|
|
|
|
|
2007-08-17 08:11:56 +08:00
|
|
|
verbosity = int(options.get('verbosity', 1))
|
2007-12-17 19:09:32 +08:00
|
|
|
show_traceback = options.get('traceback', False)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
|
|
|
# commit is a stealth option - it isn't really useful as
|
2008-08-13 22:36:40 +08:00
|
|
|
# a command line option, but it can be useful when invoking
|
2008-10-02 20:57:13 +08:00
|
|
|
# loaddata from within another script.
|
2008-08-13 22:36:40 +08:00
|
|
|
# If commit=True, loaddata will use its own transaction;
|
|
|
|
# if commit=False, the data load SQL will become part of
|
|
|
|
# the transaction in place when loaddata was invoked.
|
|
|
|
commit = options.get('commit', True)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# Keep a count of the installed objects and fixtures
|
2008-02-22 20:50:10 +08:00
|
|
|
fixture_count = 0
|
2010-08-20 21:57:24 +08:00
|
|
|
loaded_object_count = 0
|
|
|
|
fixture_object_count = 0
|
2007-08-16 14:06:55 +08:00
|
|
|
models = set()
|
|
|
|
|
|
|
|
humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
|
|
|
|
|
|
|
|
# Get a cursor (even though we don't need one yet). This has
|
|
|
|
# the side effect of initializing the test database (if
|
|
|
|
# it isn't already initialized).
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
# Start transaction management. All fixtures are installed in a
|
|
|
|
# single transaction to ensure that all references are resolved.
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
2009-12-22 23:18:51 +08:00
|
|
|
transaction.commit_unless_managed(using=using)
|
|
|
|
transaction.enter_transaction_management(using=using)
|
|
|
|
transaction.managed(True, using=using)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-11-25 04:42:09 +08:00
|
|
|
class SingleZipReader(zipfile.ZipFile):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
zipfile.ZipFile.__init__(self, *args, **kwargs)
|
|
|
|
if settings.DEBUG:
|
|
|
|
assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
|
|
|
|
def read(self):
|
|
|
|
return zipfile.ZipFile.read(self, self.namelist()[0])
|
|
|
|
|
|
|
|
compression_types = {
|
2011-09-10 00:18:38 +08:00
|
|
|
None: open,
|
2008-11-25 04:42:09 +08:00
|
|
|
'gz': gzip.GzipFile,
|
|
|
|
'zip': SingleZipReader
|
|
|
|
}
|
2008-12-01 10:20:59 +08:00
|
|
|
if has_bz2:
|
|
|
|
compression_types['bz2'] = bz2.BZ2File
|
2008-11-25 04:42:09 +08:00
|
|
|
|
2009-12-19 22:27:26 +08:00
|
|
|
app_module_paths = []
|
|
|
|
for app in get_apps():
|
|
|
|
if hasattr(app, '__path__'):
|
|
|
|
# It's a 'models/' subpackage
|
|
|
|
for path in app.__path__:
|
|
|
|
app_module_paths.append(path)
|
|
|
|
else:
|
|
|
|
# It's a models.py module
|
|
|
|
app_module_paths.append(app.__file__)
|
|
|
|
|
|
|
|
app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
|
2007-08-16 14:06:55 +08:00
|
|
|
for fixture_label in fixture_labels:
|
|
|
|
parts = fixture_label.split('.')
|
2008-11-25 04:42:09 +08:00
|
|
|
|
|
|
|
if len(parts) > 1 and parts[-1] in compression_types:
|
|
|
|
compression_formats = [parts[-1]]
|
|
|
|
parts = parts[:-1]
|
|
|
|
else:
|
|
|
|
compression_formats = compression_types.keys()
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
if len(parts) == 1:
|
2008-11-25 04:42:09 +08:00
|
|
|
fixture_name = parts[0]
|
2007-12-17 14:53:15 +08:00
|
|
|
formats = serializers.get_public_serializer_formats()
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
fixture_name, format = '.'.join(parts[:-1]), parts[-1]
|
2007-12-17 14:53:15 +08:00
|
|
|
if format in serializers.get_public_serializer_formats():
|
2007-08-16 14:06:55 +08:00
|
|
|
formats = [format]
|
|
|
|
else:
|
|
|
|
formats = []
|
|
|
|
|
2008-06-08 16:21:18 +08:00
|
|
|
if formats:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 2:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("Loading '%s' fixtures...\n" % fixture_name)
|
2008-06-08 16:21:18 +08:00
|
|
|
else:
|
2010-08-20 21:57:24 +08:00
|
|
|
self.stderr.write(
|
2010-06-05 13:26:04 +08:00
|
|
|
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
|
2008-06-08 16:21:18 +08:00
|
|
|
(fixture_name, format)))
|
2010-10-02 21:56:09 +08:00
|
|
|
if commit:
|
|
|
|
transaction.rollback(using=using)
|
|
|
|
transaction.leave_transaction_management(using=using)
|
2008-06-08 16:21:18 +08:00
|
|
|
return
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-02-22 20:50:10 +08:00
|
|
|
if os.path.isabs(fixture_name):
|
|
|
|
fixture_dirs = [fixture_name]
|
|
|
|
else:
|
|
|
|
fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
|
|
|
|
|
|
|
|
for fixture_dir in fixture_dirs:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 2:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("Checking %s for fixtures...\n" % humanize(fixture_dir))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
label_found = False
|
2009-12-22 23:18:51 +08:00
|
|
|
for combo in product([using, None], formats, compression_formats):
|
|
|
|
database, format, compression_format = combo
|
|
|
|
file_name = '.'.join(
|
|
|
|
p for p in [
|
|
|
|
fixture_name, database, format, compression_format
|
|
|
|
]
|
|
|
|
if p
|
|
|
|
)
|
|
|
|
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 3:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("Trying %s for %s fixture '%s'...\n" % \
|
|
|
|
(humanize(fixture_dir), file_name, fixture_name))
|
2009-12-22 23:18:51 +08:00
|
|
|
full_path = os.path.join(fixture_dir, file_name)
|
|
|
|
open_method = compression_types[compression_format]
|
|
|
|
try:
|
|
|
|
fixture = open_method(full_path, 'r')
|
|
|
|
if label_found:
|
|
|
|
fixture.close()
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
|
|
|
|
(fixture_name, humanize(fixture_dir))))
|
2010-10-02 21:56:09 +08:00
|
|
|
if commit:
|
|
|
|
transaction.rollback(using=using)
|
|
|
|
transaction.leave_transaction_management(using=using)
|
2009-12-22 23:18:51 +08:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
fixture_count += 1
|
|
|
|
objects_in_fixture = 0
|
2010-08-20 21:57:24 +08:00
|
|
|
loaded_objects_in_fixture = 0
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 2:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("Installing %s fixture '%s' from %s.\n" % \
|
|
|
|
(format, fixture_name, humanize(fixture_dir)))
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
|
|
|
objects = serializers.deserialize(format, fixture, using=using)
|
2011-08-07 08:43:26 +08:00
|
|
|
|
|
|
|
with connection.constraint_checks_disabled():
|
|
|
|
for obj in objects:
|
|
|
|
objects_in_fixture += 1
|
|
|
|
if router.allow_syncdb(using, obj.object.__class__):
|
|
|
|
loaded_objects_in_fixture += 1
|
|
|
|
models.add(obj.object.__class__)
|
2011-09-22 12:30:20 +08:00
|
|
|
try:
|
|
|
|
obj.save(using=using)
|
|
|
|
except (DatabaseError, IntegrityError), e:
|
|
|
|
msg = "Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % {
|
|
|
|
'app_label': obj.object._meta.app_label,
|
|
|
|
'object_name': obj.object._meta.object_name,
|
|
|
|
'pk': obj.object.pk,
|
|
|
|
'error_msg': e
|
|
|
|
}
|
|
|
|
raise e.__class__, e.__class__(msg), sys.exc_info()[2]
|
2011-08-07 08:43:26 +08:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2010-08-20 21:57:24 +08:00
|
|
|
loaded_object_count += loaded_objects_in_fixture
|
|
|
|
fixture_object_count += objects_in_fixture
|
2009-12-22 23:18:51 +08:00
|
|
|
label_found = True
|
|
|
|
except (SystemExit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
import traceback
|
2008-11-25 04:42:09 +08:00
|
|
|
fixture.close()
|
2010-10-02 21:56:09 +08:00
|
|
|
if commit:
|
|
|
|
transaction.rollback(using=using)
|
|
|
|
transaction.leave_transaction_management(using=using)
|
2009-12-22 23:18:51 +08:00
|
|
|
if show_traceback:
|
|
|
|
traceback.print_exc()
|
|
|
|
else:
|
2010-08-20 21:57:24 +08:00
|
|
|
self.stderr.write(
|
2009-12-22 23:18:51 +08:00
|
|
|
self.style.ERROR("Problem installing fixture '%s': %s\n" %
|
|
|
|
(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:
|
2010-08-20 21:57:24 +08:00
|
|
|
self.stderr.write(
|
2010-06-05 13:26:04 +08:00
|
|
|
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
|
2009-12-22 23:18:51 +08:00
|
|
|
(fixture_name)))
|
2010-10-02 21:56:09 +08:00
|
|
|
if commit:
|
|
|
|
transaction.rollback(using=using)
|
|
|
|
transaction.leave_transaction_management(using=using)
|
2009-12-22 23:18:51 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
except Exception, e:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 2:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("No %s fixture '%s' in %s.\n" % \
|
|
|
|
(format, fixture_name, humanize(fixture_dir)))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-08-13 22:36:40 +08:00
|
|
|
# If we found even one object in a fixture, we need to reset the
|
2008-06-08 16:21:18 +08:00
|
|
|
# database sequences.
|
2010-08-20 21:57:24 +08:00
|
|
|
if loaded_object_count > 0:
|
2007-08-20 08:21:10 +08:00
|
|
|
sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
|
2007-08-16 14:06:55 +08:00
|
|
|
if sequence_sql:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 2:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("Resetting sequences\n")
|
2007-08-16 14:06:55 +08:00
|
|
|
for line in sequence_sql:
|
|
|
|
cursor.execute(line)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
2009-12-22 23:18:51 +08:00
|
|
|
transaction.commit(using=using)
|
|
|
|
transaction.leave_transaction_management(using=using)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2010-08-20 21:57:24 +08:00
|
|
|
if fixture_object_count == 0:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 1:
|
2010-06-05 13:26:04 +08:00
|
|
|
self.stdout.write("No fixtures found.\n")
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
2010-08-07 14:58:14 +08:00
|
|
|
if verbosity >= 1:
|
2010-08-20 21:57:24 +08:00
|
|
|
if fixture_object_count == loaded_object_count:
|
|
|
|
self.stdout.write("Installed %d object(s) from %d fixture(s)\n" % (
|
|
|
|
loaded_object_count, fixture_count))
|
|
|
|
else:
|
|
|
|
self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)\n" % (
|
|
|
|
loaded_object_count, fixture_object_count, fixture_count))
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2008-06-30 20:18:29 +08:00
|
|
|
# Close the DB connection. This is required as a workaround for an
|
|
|
|
# edge case in MySQL: if the same connection is used to
|
|
|
|
# create tables, load data, and query, the query can return
|
|
|
|
# incorrect results. See Django #7572, MySQL #37735.
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
|
|
|
connection.close()
|