Applied fix from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff to defer constraint checks when loading fixtures.

- Legacy-Id: 5818
This commit is contained in:
Henrik Levkowetz 2013-07-14 14:22:26 +00:00
parent cbfcd6bba8
commit be7e968da6
3 changed files with 34 additions and 0 deletions

View file

@ -81,6 +81,10 @@ class Command(BaseCommand):
if has_bz2:
compression_types['bz2'] = bz2.BZ2File
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.begin_defer_constraint_checks()
app_module_paths = []
for app in get_apps():
if hasattr(app, '__path__'):
@ -118,6 +122,9 @@ class Command(BaseCommand):
self.stderr.write(
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
(fixture_name, format)))
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.end_defer_constraint_checks()
if commit:
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@ -153,6 +160,9 @@ class Command(BaseCommand):
fixture.close()
self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
(fixture_name, humanize(fixture_dir))))
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.end_defer_constraint_checks()
if commit:
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@ -180,6 +190,10 @@ class Command(BaseCommand):
except Exception:
import traceback
fixture.close()
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.end_defer_constraint_checks()
if commit:
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@ -199,6 +213,9 @@ class Command(BaseCommand):
self.stderr.write(
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
(fixture_name)))
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.end_defer_constraint_checks()
if commit:
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@ -219,6 +236,9 @@ class Command(BaseCommand):
for line in sequence_sql:
cursor.execute(line)
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
connection.end_defer_constraint_checks()
if commit:
transaction.commit(using=using)
transaction.leave_transaction_management(using=using)

View file

@ -65,6 +65,13 @@ class BaseDatabaseWrapper(local):
return
self.cursor().execute(self.ops.savepoint_commit_sql(sid))
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
def begin_defer_constraint_checks(self):
return None
def end_defer_constraint_checks(self):
return None
def close(self):
if self.connection is not None:
self.connection.close()

View file

@ -316,3 +316,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
self.server_version = tuple([int(x) for x in m.groups()])
return self.server_version
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
def begin_defer_constraint_checks(self):
self.cursor().execute('SET foreign_key_checks=0')
def end_defer_constraint_checks(self):
self.cursor().execute('SET foreign_key_checks=1')