]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added expunge() method to objectstore
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Mar 2006 00:17:51 +0000 (00:17 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Mar 2006 00:17:51 +0000 (00:17 +0000)
correction in attributes reset_history to really reset in all cases
added unit tests testing refresh()/expire() bug that was fixed by reset_history thing

lib/sqlalchemy/attributes.py
lib/sqlalchemy/mapping/objectstore.py
lib/sqlalchemy/mapping/unitofwork.py
test/mapper.py

index bd730a10917103e28fbb71dc3011b3475236c8b8..bd5868baf4870547d691586286e70dfd81ea04f7 100644 (file)
@@ -397,7 +397,10 @@ class AttributeManager(object):
             x.clear()
             del self.attribute_history(obj)[key]
         except KeyError:
-            pass
+            try:
+                del obj.__dict__[key]
+            except KeyError:
+                pass
         
     def class_managed(self, class_):
         """returns a dictionary of "history container definitions", which is attached to a 
index f978d16f727c6f68b8a703393e889221c88fc75a..6b3d7f034aaeca1d0a53d97a46d68c4cc2a1b805 100644 (file)
@@ -166,6 +166,10 @@ class Session(object):
         for o in obj:
             global_attributes.trigger_history(o, lambda: refresh(o))
 
+    def expunge(self, *obj):
+        for o in obj:
+            self.uow.expunge(obj)
+            
     def register_clean(self, obj):
         self._bind_to(obj)
         self.uow.register_clean(obj)
@@ -252,7 +256,10 @@ def expire(*obj):
     """invalidates the data in the given objects and sets them to refresh themselves
     the next time they are requested."""
     get_session().expire(*obj)
-    
+
+def expunge(*obj):
+    get_session().expunge(*obj)
+
 def delete(*obj):
     """registers the given objects as to be deleted upon the next commit"""
     s = get_session().delete(*obj)
index 2dc524897553efd6a74fd698638f1372b625cd64..1e5388933d53ecbcfc65fc5eedbe647a3bcb92b4 100644 (file)
@@ -104,6 +104,11 @@ class UnitOfWork(object):
         """returns True if the given key is present in this UnitOfWork's identity map."""
         return self.identity_map.has_key(key)
     
+    def expunge(self, obj):
+        """removes this object completely from the UnitOfWork, including the identity map,
+        and the "new", "dirty" and "deleted" lists."""
+        self._remove_deleted(obj)
+        
     def _remove_deleted(self, obj):
         if hasattr(obj, "_instance_key"):
             del self.identity_map[obj._instance_key]
@@ -119,7 +124,7 @@ class UnitOfWork(object):
             del self.new[obj]
         except KeyError:
             pass
-        self.attributes.commit(obj)
+        #self.attributes.commit(obj)
         self.attributes.remove(obj)
 
     def _validate_obj(self, obj):
index eff6566547d2fbbef2ba1a5850f5daccbe123cbd..26668df1af39dbf36c0cbe99f55f0e4e0e9ee7b5 100644 (file)
@@ -85,9 +85,14 @@ class MapperTest(MapperSuperTest):
         self.assert_(u is not u2)
 
     def testrefresh(self):
-        m = mapper(User, users)
+        m = mapper(User, users, properties={'addresses':relation(mapper(Address, addresses))})
         u = m.get(7)
         u.user_name = 'foo'
+        a = Address()
+        u.addresses.append(a)
+
+        self.assert_(a in u.addresses)
+
         objectstore.refresh(u)
         
         # its refreshed, so not dirty
@@ -96,16 +101,21 @@ class MapperTest(MapperSuperTest):
         # username is back to the DB
         self.assert_(u.user_name == 'jack')
         
+        self.assert_(a not in u.addresses)
+        
         u.user_name = 'foo'
+        u.addresses.append(a)
         # now its dirty
         self.assert_(u in objectstore.get_session().uow.dirty)
         self.assert_(u.user_name == 'foo')
+        self.assert_(a in u.addresses)
         objectstore.expire(u)
 
         # expired, but not refreshed yet.  still dirty
         self.assert_(u in objectstore.get_session().uow.dirty)
         # get the attribute, it refreshes
         self.assert_(u.user_name == 'jack')
+        self.assert_(a not in u.addresses)
         # not dirty anymore
         self.assert_(u not in objectstore.get_session().uow.dirty)