Ticket #3615: defer_constraint_checks.diff

File defer_constraint_checks.diff, 8.5 KB (added by Karen Tracey, 16 years ago)
  • django/db/backends/mysql/base.py

     
    289289                raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
    290290            self.server_version = tuple([int(x) for x in m.groups()])
    291291        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

     
    5151        if not self.features.uses_savepoints:
    5252            return
    5353        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
    5460
    5561    def close(self):
    5662        if self.connection is not None:
  • django/core/management/commands/loaddata.py

     
    7676        if has_bz2:
    7777            compression_types['bz2'] = bz2.BZ2File
    7878
     79        connection.begin_defer_constraint_checks()
     80
    7981        app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
    8082        for fixture_label in fixture_labels:
    8183            parts = fixture_label.split('.')
     
    103105                sys.stderr.write(
    104106                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
    105107                        (fixture_name, format)))
     108                connection.end_defer_constraint_checks()
    106109                transaction.rollback()
    107110                transaction.leave_transaction_management()
    108111                return
     
    136139                                fixture.close()
    137140                                print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
    138141                                    (fixture_name, humanize(fixture_dir)))
     142                                connection.end_defer_constraint_checks()
    139143                                transaction.rollback()
    140144                                transaction.leave_transaction_management()
    141145                                return
     
    158162                                except Exception:
    159163                                    import traceback
    160164                                    fixture.close()
     165                                    connection.end_defer_constraint_checks()
    161166                                    transaction.rollback()
    162167                                    transaction.leave_transaction_management()
    163168                                    if show_traceback:
     
    176181                                    sys.stderr.write(
    177182                                        self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
    178183                                            (fixture_name)))
     184                                    connection.end_defer_constraint_checks()
    179185                                    transaction.rollback()
    180186                                    transaction.leave_transaction_management()
    181187                                    return
     
    195201                for line in sequence_sql:
    196202                    cursor.execute(line)
    197203
     204        connection.end_defer_constraint_checks()
    198205        if commit:
    199206            transaction.commit()
    200207            transaction.leave_transaction_management()
  • tests/modeltests/mutually_referential/models.py

     
    3131>>> q.bestchild = c
    3232>>> q.save()
    3333
     34>>> from django.db import connection
     35>>> connection.begin_defer_constraint_checks()
    3436>>> q.delete()
    35 
     37>>> connection.end_defer_constraint_checks()
    3638"""}
     39 No newline at end of file
  • tests/modeltests/serializers/models.py

     
    164164
    165165# Objects ids can be referenced before they are defined in the serialization data
    166166# 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.
    167170>>> 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
    169172>>> transaction.enter_transaction_management()
    170173>>> transaction.managed(True)
     174>>> connection.begin_defer_constraint_checks()
    171175>>> for obj in serializers.deserialize("json", json):
    172176...     obj.save()
    173177
     178>>> connection.end_defer_constraint_checks()
    174179>>> transaction.commit()
    175180>>> transaction.leave_transaction_management()
    176181
  • tests/modeltests/delete/models.py

     
    9393#  - remove the second set of tests (with a2, b2 etc)
    9494
    9595>>> from django.db.models.loading import cache
     96>>> from django.db import connection
    9697
    9798>>> def clear_rel_obj_caches(models):
    9899...     for m in models:
     
    168169>>> o.keys()
    169170[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
    170171
     172>>> connection.begin_defer_constraint_checks()
    171173>>> e1.delete()
     174>>> connection.end_defer_constraint_checks()
    172175
    173176>>> e2 = E()
    174177>>> e2.save()
     
    184187>>> o.keys()
    185188[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
    186189
     190>>> connection.begin_defer_constraint_checks()
    187191>>> f2.delete()
     192>>> connection.end_defer_constraint_checks()
    188193
    189194"""
    190195}
  • tests/regressiontests/serializers_regress/tests.py

     
    1313
    1414from django.utils.functional import curry
    1515from django.core import serializers
    16 from django.db import transaction
     16from django.db import transaction, connection
    1717from django.core import management
    1818from django.conf import settings
    1919
     
    354354    objects = []
    355355    instance_count = {}
    356356    transaction.enter_transaction_management()
     357    connection.begin_defer_constraint_checks()
    357358    try:
    358359        transaction.managed(True)
    359360        for (func, pk, klass, datum) in test_data:
     
    362363        transaction.commit()
    363364    except:
    364365        transaction.rollback()
     366        connection.end_defer_constraint_checks()
    365367        transaction.leave_transaction_management()
    366368        raise
     369   
     370    connection.end_defer_constraint_checks()
    367371    transaction.leave_transaction_management()
    368372
    369373    # Get a count of the number of objects created for each class
     
    380384    management.call_command('flush', verbosity=0, interactive=False)
    381385    transaction.enter_transaction_management()
    382386    try:
     387        connection.begin_defer_constraint_checks()
    383388        transaction.managed(True)
    384389        for obj in serializers.deserialize(format, serialized_data):
    385390            obj.save()
    386391        transaction.commit()
    387392    except:
    388393        transaction.rollback()
     394        connection.end_defer_constraint_checks()
    389395        transaction.leave_transaction_management()
    390396        raise
     397   
     398    connection.end_defer_constraint_checks()   
    391399    transaction.leave_transaction_management()
    392400
    393401    # Assert that the deserialized data is the same
Back to Top