Used commit_on_success_unless_managed in loaddata.

This commit is contained in:
Aymeric Augustin 2013-03-08 11:35:15 +01:00
parent 4dbd1b2dd8
commit d04964e70d
1 changed files with 21 additions and 48 deletions

View File

@ -41,8 +41,6 @@ class Command(BaseCommand):
self.ignore = options.get('ignore') self.ignore = options.get('ignore')
self.using = options.get('database') self.using = options.get('database')
connection = connections[self.using]
if not len(fixture_labels): if not len(fixture_labels):
raise CommandError( raise CommandError(
"No database fixture specified. Please provide the path of at " "No database fixture specified. Please provide the path of at "
@ -51,13 +49,18 @@ class Command(BaseCommand):
self.verbosity = int(options.get('verbosity')) self.verbosity = int(options.get('verbosity'))
# commit is a stealth option - it isn't really useful as with transaction.commit_on_success_unless_managed(using=self.using):
# a command line option, but it can be useful when invoking self.loaddata(fixture_labels)
# loaddata from within another script.
# If commit=True, loaddata will use its own transaction; # Close the DB connection -- unless we're still in a transaction. This
# if commit=False, the data load SQL will become part of # is required as a workaround for an edge case in MySQL: if the same
# the transaction in place when loaddata was invoked. # connection is used to create tables, load data, and query, the query
commit = options.get('commit', True) # can return incorrect results. See Django #7572, MySQL #37735.
if transaction.get_autocommit(self.using):
connections[self.using].close()
def loaddata(self, fixture_labels):
connection = connections[self.using]
# Keep a count of the installed objects and fixtures # Keep a count of the installed objects and fixtures
self.fixture_count = 0 self.fixture_count = 0
@ -65,16 +68,6 @@ class Command(BaseCommand):
self.fixture_object_count = 0 self.fixture_object_count = 0
self.models = set() self.models = set()
# 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.
if commit:
transaction.enter_transaction_management(using=self.using)
class SingleZipReader(zipfile.ZipFile): class SingleZipReader(zipfile.ZipFile):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
zipfile.ZipFile.__init__(self, *args, **kwargs) zipfile.ZipFile.__init__(self, *args, **kwargs)
@ -103,7 +96,6 @@ class Command(BaseCommand):
app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths] app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
try:
with connection.constraint_checks_disabled(): with connection.constraint_checks_disabled():
for fixture_label in fixture_labels: for fixture_label in fixture_labels:
self.load_label(fixture_label, app_fixtures) self.load_label(fixture_label, app_fixtures)
@ -117,14 +109,6 @@ class Command(BaseCommand):
e.args = ("Problem installing fixtures: %s" % e,) e.args = ("Problem installing fixtures: %s" % e,)
raise raise
except (SystemExit, KeyboardInterrupt):
raise
except Exception as e:
if commit:
transaction.rollback(using=self.using)
transaction.leave_transaction_management(using=self.using)
raise
# 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 self.loaded_object_count > 0: if self.loaded_object_count > 0:
@ -135,10 +119,6 @@ class Command(BaseCommand):
for line in sequence_sql: for line in sequence_sql:
cursor.execute(line) cursor.execute(line)
if commit:
transaction.commit(using=self.using)
transaction.leave_transaction_management(using=self.using)
if self.verbosity >= 1: if self.verbosity >= 1:
if self.fixture_object_count == self.loaded_object_count: if self.fixture_object_count == self.loaded_object_count:
self.stdout.write("Installed %d object(s) from %d fixture(s)" % ( self.stdout.write("Installed %d object(s) from %d fixture(s)" % (
@ -147,13 +127,6 @@ class Command(BaseCommand):
self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)" % ( self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)" % (
self.loaded_object_count, self.fixture_object_count, self.fixture_count)) self.loaded_object_count, self.fixture_object_count, self.fixture_count))
# 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.
if commit:
connection.close()
def load_label(self, fixture_label, app_fixtures): def load_label(self, fixture_label, app_fixtures):
parts = fixture_label.split('.') parts = fixture_label.split('.')