Ticket #3615: defer_constraint_checks.diff
File defer_constraint_checks.diff, 8.5 KB (added by , 16 years ago) |
---|
-
django/db/backends/mysql/base.py
289 289 raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info()) 290 290 self.server_version = tuple([int(x) for x in m.groups()]) 291 291 return self.server_version 292 293 def begin_defer_constraint_checks(self): 294 self.cursor().execute('SET foreign_key_checks=0') 295 296 def end_defer_constraint_checks(self): 297 self.cursor().execute('SET foreign_key_checks=1') 298 No newline at end of file -
django/db/backends/__init__.py
51 51 if not self.features.uses_savepoints: 52 52 return 53 53 self.connection.cursor().execute(self.ops.savepoint_commit_sql(sid)) 54 55 def begin_defer_constraint_checks(self): 56 return None 57 58 def end_defer_constraint_checks(self): 59 return None 54 60 55 61 def close(self): 56 62 if self.connection is not None: -
django/core/management/commands/loaddata.py
76 76 if has_bz2: 77 77 compression_types['bz2'] = bz2.BZ2File 78 78 79 connection.begin_defer_constraint_checks() 80 79 81 app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] 80 82 for fixture_label in fixture_labels: 81 83 parts = fixture_label.split('.') … … 103 105 sys.stderr.write( 104 106 self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." % 105 107 (fixture_name, format))) 108 connection.end_defer_constraint_checks() 106 109 transaction.rollback() 107 110 transaction.leave_transaction_management() 108 111 return … … 136 139 fixture.close() 137 140 print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % 138 141 (fixture_name, humanize(fixture_dir))) 142 connection.end_defer_constraint_checks() 139 143 transaction.rollback() 140 144 transaction.leave_transaction_management() 141 145 return … … 158 162 except Exception: 159 163 import traceback 160 164 fixture.close() 165 connection.end_defer_constraint_checks() 161 166 transaction.rollback() 162 167 transaction.leave_transaction_management() 163 168 if show_traceback: … … 176 181 sys.stderr.write( 177 182 self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % 178 183 (fixture_name))) 184 connection.end_defer_constraint_checks() 179 185 transaction.rollback() 180 186 transaction.leave_transaction_management() 181 187 return … … 195 201 for line in sequence_sql: 196 202 cursor.execute(line) 197 203 204 connection.end_defer_constraint_checks() 198 205 if commit: 199 206 transaction.commit() 200 207 transaction.leave_transaction_management() -
tests/modeltests/mutually_referential/models.py
31 31 >>> q.bestchild = c 32 32 >>> q.save() 33 33 34 >>> from django.db import connection 35 >>> connection.begin_defer_constraint_checks() 34 36 >>> q.delete() 35 37 >>> connection.end_defer_constraint_checks() 36 38 """} 39 No newline at end of file -
tests/modeltests/serializers/models.py
164 164 165 165 # Objects ids can be referenced before they are defined in the serialization data 166 166 # However, the deserialization process will need to be contained within a transaction 167 # Furthermore since some DBs don't support deferred constraint checking, we must 168 # signal begin/end of a block that may contain forward references so that constraint 169 # checking may be temporarily disabled, if necessary to allow forward references. 167 170 >>> json = '[{"pk": 3, "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00", "categories": [4, 1], "author": 4}}, {"pk": 4, "model": "serializers.category", "fields": {"name": "Reference"}}, {"pk": 4, "model": "serializers.author", "fields": {"name": "Agnes"}}]' 168 >>> from django.db import transaction 171 >>> from django.db import transaction, connection 169 172 >>> transaction.enter_transaction_management() 170 173 >>> transaction.managed(True) 174 >>> connection.begin_defer_constraint_checks() 171 175 >>> for obj in serializers.deserialize("json", json): 172 176 ... obj.save() 173 177 178 >>> connection.end_defer_constraint_checks() 174 179 >>> transaction.commit() 175 180 >>> transaction.leave_transaction_management() 176 181 -
tests/modeltests/delete/models.py
93 93 # - remove the second set of tests (with a2, b2 etc) 94 94 95 95 >>> from django.db.models.loading import cache 96 >>> from django.db import connection 96 97 97 98 >>> def clear_rel_obj_caches(models): 98 99 ... for m in models: … … 168 169 >>> o.keys() 169 170 [<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>] 170 171 172 >>> connection.begin_defer_constraint_checks() 171 173 >>> e1.delete() 174 >>> connection.end_defer_constraint_checks() 172 175 173 176 >>> e2 = E() 174 177 >>> e2.save() … … 184 187 >>> o.keys() 185 188 [<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>] 186 189 190 >>> connection.begin_defer_constraint_checks() 187 191 >>> f2.delete() 192 >>> connection.end_defer_constraint_checks() 188 193 189 194 """ 190 195 } -
tests/regressiontests/serializers_regress/tests.py
13 13 14 14 from django.utils.functional import curry 15 15 from django.core import serializers 16 from django.db import transaction 16 from django.db import transaction, connection 17 17 from django.core import management 18 18 from django.conf import settings 19 19 … … 354 354 objects = [] 355 355 instance_count = {} 356 356 transaction.enter_transaction_management() 357 connection.begin_defer_constraint_checks() 357 358 try: 358 359 transaction.managed(True) 359 360 for (func, pk, klass, datum) in test_data: … … 362 363 transaction.commit() 363 364 except: 364 365 transaction.rollback() 366 connection.end_defer_constraint_checks() 365 367 transaction.leave_transaction_management() 366 368 raise 369 370 connection.end_defer_constraint_checks() 367 371 transaction.leave_transaction_management() 368 372 369 373 # Get a count of the number of objects created for each class … … 380 384 management.call_command('flush', verbosity=0, interactive=False) 381 385 transaction.enter_transaction_management() 382 386 try: 387 connection.begin_defer_constraint_checks() 383 388 transaction.managed(True) 384 389 for obj in serializers.deserialize(format, serialized_data): 385 390 obj.save() 386 391 transaction.commit() 387 392 except: 388 393 transaction.rollback() 394 connection.end_defer_constraint_checks() 389 395 transaction.leave_transaction_management() 390 396 raise 397 398 connection.end_defer_constraint_checks() 391 399 transaction.leave_transaction_management() 392 400 393 401 # Assert that the deserialized data is the same