Ticket #9205: 9205-r9084-savepoints-in-save.diff
File 9205-r9084-savepoints-in-save.diff, 6.5 KB (added by , 16 years ago) |
---|
-
django/db/models/base.py
14 14 from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField 15 15 from django.db.models.query import delete_objects, Q, CollectedObjects 16 16 from django.db.models.options import Options 17 from django.db import connection, transaction, DatabaseError 17 from django.db import connection, transaction, DatabaseError, IntegrityError 18 18 from django.db.models import signals 19 19 from django.db.models.loading import register_models, get_model 20 20 from django.utils.functional import curry … … 304 304 if force_insert and force_update: 305 305 raise ValueError("Cannot force both insert and updating in " 306 306 "model saving.") 307 self.save_base(force_insert=force_insert, force_update=force_update)308 307 308 try: 309 sid = transaction.savepoint() 310 self.save_base(force_insert=force_insert, force_update=force_update) 311 transaction.savepoint_commit(sid) 312 except IntegrityError: 313 transaction.savepoint_rollback(sid) 314 raise 315 309 316 save.alters_data = True 310 317 311 318 def save_base(self, raw=False, cls=None, force_insert=False, -
django/db/models/query.py
329 329 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 330 330 params.update(defaults) 331 331 obj = self.model(**params) 332 sid = transaction.savepoint()333 332 obj.save(force_insert=True) 334 transaction.savepoint_commit(sid)335 333 return obj, True 336 334 except IntegrityError, e: 337 transaction.savepoint_rollback(sid)338 335 try: 339 336 return self.get(**kwargs), False 340 337 except self.model.DoesNotExist: -
django/db/backends/__init__.py
34 34 def _savepoint(self, sid): 35 35 if not self.features.uses_savepoints: 36 36 return 37 self.connection.cursor().execute(self.ops.savepoint_create_sql(sid)) 37 if self.connection is not None: 38 self.connection.cursor().execute(self.ops.savepoint_create_sql(sid)) 38 39 39 40 def _savepoint_rollback(self, sid): 40 41 if not self.features.uses_savepoints: 41 42 return 42 self.connection.cursor().execute(self.ops.savepoint_rollback_sql(sid)) 43 if self.connection is not None: 44 self.connection.cursor().execute(self.ops.savepoint_rollback_sql(sid)) 43 45 44 46 def _savepoint_commit(self, sid): 45 47 if not self.features.uses_savepoints: 46 48 return 47 self.connection.cursor().execute(self.ops.savepoint_commit_sql(sid)) 49 if self.connection is not None: 50 self.connection.cursor().execute(self.ops.savepoint_commit_sql(sid)) 48 51 49 52 def close(self): 50 53 if self.connection is not None: -
django/contrib/sessions/backends/db.py
53 53 session_data = self.encode(self._get_session(no_load=must_create)), 54 54 expire_date = self.get_expiry_date() 55 55 ) 56 sid = transaction.savepoint()57 56 try: 58 57 obj.save(force_insert=must_create) 59 58 except IntegrityError: 60 59 if must_create: 61 transaction.savepoint_rollback(sid)62 60 raise CreateError 63 61 raise 64 62 -
tests/modeltests/force_insert_update/models.py
40 40 >>> c1.save(force_insert=True) 41 41 42 42 # Won't work because we can't insert a pk of the same value. 43 >>> sid = transaction.savepoint()44 43 >>> c.value = 5 45 44 >>> try: 46 45 ... c.save(force_insert=True) … … 50 49 ... else: 51 50 ... print "Fail with %s" % type(e) 52 51 Pass 53 >>> transaction.savepoint_rollback(sid)54 52 55 53 # Trying to update should still fail, even with manual primary keys, if the 56 54 # data isn't in the database already. -
tests/modeltests/one_to_one/models.py
178 178 179 179 # This will fail because each one-to-one field must be unique (and link2=o1 was 180 180 # used for x1, above). 181 >>> sid = transaction.savepoint()182 181 >>> try: 183 182 ... MultiModel(link1=p2, link2=o1, name="x1").save() 184 183 ... except Exception, e: … … 187 186 ... else: 188 187 ... print "Fail with %s" % type(e) 189 188 Pass 190 >>> transaction.savepoint_rollback(sid)191 189 192 190 """} -
tests/modeltests/custom_pk/models.py
110 110 # The primary key must also obviously be unique, so trying to create a new 111 111 # object with the same primary key will fail. 112 112 >>> try: 113 ... sid = transaction.savepoint()114 113 ... Employee.objects.create(employee_code=123, first_name='Fred', last_name='Jones') 115 ... transaction.savepoint_commit(sid)116 114 ... except Exception, e: 117 115 ... if isinstance(e, IntegrityError): 118 ... transaction.savepoint_rollback(sid)119 116 ... print "Pass" 120 117 ... else: 121 118 ... print "Fail with %s" % type(e) … … 131 128 # The primary key must be specified, so an error is raised if you try to create 132 129 # an object without it. 133 130 >>> try: 134 ... sid = transaction.savepoint()135 131 ... Employee.objects.create(first_name='Tom', last_name='Smith') 136 132 ... print 'hello' 137 ... transaction.savepoint_commit(sid)138 133 ... print 'hello2' 139 134 ... except Exception, e: 140 135 ... if isinstance(e, IntegrityError): 141 ... transaction.savepoint_rollback(sid)142 136 ... print "Pass" 143 137 ... else: 144 138 ... print "Fail with %s" % type(e)