Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Used commit_on_success_unless_managed in loaddata.

  • Loading branch information...
commit d04964e70d5c6277e79874ba8950e23c224b323b 1 parent 4dbd1b2
@aaugustin aaugustin authored
Showing with 21 additions and 48 deletions.
  1. +21 −48 django/core/management/commands/loaddata.py
View
69 django/core/management/commands/loaddata.py
@@ -41,8 +41,6 @@ def handle(self, *fixture_labels, **options):
self.ignore = options.get('ignore')
self.using = options.get('database')
- connection = connections[self.using]
-
if not len(fixture_labels):
raise CommandError(
"No database fixture specified. Please provide the path of at "
@@ -51,13 +49,18 @@ def handle(self, *fixture_labels, **options):
self.verbosity = int(options.get('verbosity'))
- # commit is a stealth option - it isn't really useful as
- # a command line option, but it can be useful when invoking
- # loaddata from within another script.
- # 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)
+ with transaction.commit_on_success_unless_managed(using=self.using):
+ self.loaddata(fixture_labels)
+
+ # Close the DB connection -- unless we're still in a transaction. 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 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
self.fixture_count = 0
@@ -65,16 +68,6 @@ def handle(self, *fixture_labels, **options):
self.fixture_object_count = 0
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):
def __init__(self, *args, **kwargs):
zipfile.ZipFile.__init__(self, *args, **kwargs)
@@ -103,26 +96,17 @@ def read(self):
app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
- try:
- with connection.constraint_checks_disabled():
- for fixture_label in fixture_labels:
- self.load_label(fixture_label, app_fixtures)
-
- # 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 self.models]
- try:
- connection.check_constraints(table_names=table_names)
- except Exception as e:
- e.args = ("Problem installing fixtures: %s" % e,)
- raise
+ with connection.constraint_checks_disabled():
+ for fixture_label in fixture_labels:
+ self.load_label(fixture_label, app_fixtures)
- except (SystemExit, KeyboardInterrupt):
- raise
+ # 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 self.models]
+ try:
+ connection.check_constraints(table_names=table_names)
except Exception as e:
- if commit:
- transaction.rollback(using=self.using)
- transaction.leave_transaction_management(using=self.using)
+ e.args = ("Problem installing fixtures: %s" % e,)
raise
# If we found even one object in a fixture, we need to reset the
@@ -135,10 +119,6 @@ def read(self):
for line in sequence_sql:
cursor.execute(line)
- if commit:
- transaction.commit(using=self.using)
- transaction.leave_transaction_management(using=self.using)
-
if self.verbosity >= 1:
if self.fixture_object_count == self.loaded_object_count:
self.stdout.write("Installed %d object(s) from %d fixture(s)" % (
@@ -147,13 +127,6 @@ def read(self):
self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)" % (
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):
parts = fixture_label.split('.')
Please sign in to comment.
Something went wrong with that request. Please try again.