]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added flag to mark any attribute as "modified"
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Dec 2010 19:10:14 +0000 (14:10 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Dec 2010 19:10:14 +0000 (14:10 -0500)
lib/sqlalchemy/orm/attributes.py
test/orm/test_attributes.py

index 7f722aa444451f4de8c26ebdcd3bb2540651f31b..40a69d96ab1d2a117bfb194b492ac40676c04e51 100644 (file)
@@ -1247,3 +1247,15 @@ def del_attribute(instance, key):
     state, dict_ = instance_state(instance), instance_dict(instance)
     state.manager[key].impl.delete(state, dict_)
 
+def flag_modified(instance, key):
+    """Mark an attribute on an instance as 'modified'.
+    
+    This sets the 'modified' flag on the instance and 
+    establishes an unconditional change event for the given attribute.
+    
+    """
+    state, dict_ = instance_state(instance), instance_dict(instance)
+    impl = state.manager[key].impl
+    state.modified_event(dict_, impl, NO_VALUE)
+    
+    
\ No newline at end of file
index 9bbfa98eb95eeb438c5454f3051c194c1ba0ecb6..30156bbb74828bcb997075493eea4654ef735f94 100644 (file)
@@ -1123,7 +1123,40 @@ class HistoryTest(_base.ORMTest):
         attributes.instance_state(f).commit(attributes.instance_dict(f), ['someattr'])
         eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'foo':'old'}], ()))
 
+    def test_flag_modified(self):
+        class Foo(_base.BasicEntity):
+            pass
+
+        instrumentation.register_class(Foo)
+        attributes.register_attribute(Foo, 'someattr', uselist=False, useobject=False)
+
+        f = Foo()
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), (), ()))
+
+        f.someattr = {'a':'b'}
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([{'a':'b'},], (), ()))
 
+        attributes.instance_state(f).commit_all(attributes.instance_dict(f))
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'a':'b'},], ()))
+        
+        f.someattr['a'] = 'c'
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'a':'c'},], ()))
+        attributes.flag_modified(f, 'someattr')
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([{'a':'c'},], (), ()))
+        
+        f.someattr = ['a']
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([['a']], (), ()))
+        attributes.instance_state(f).commit_all(attributes.instance_dict(f))
+
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [['a']], ()))
+        f.someattr[0] = 'b'
+        f.someattr.append('c')
+
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [['b', 'c']], ()))
+        attributes.flag_modified(f, 'someattr')
+
+        eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([['b', 'c']], (), ()))
+        
     def test_use_object(self):
         class Foo(_base.BasicEntity):
             pass