From: Mike Bayer Date: Sat, 26 Jul 2008 16:30:59 +0000 (+0000) Subject: - added add() and add_all() methods to Session, X-Git-Tag: rel_0_4_7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=deeee6eb33ffa392d200d5c4e8924b85c6ba33b6;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - added add() and add_all() methods to Session, establishing forwards compatibility with 0.5. --- diff --git a/CHANGES b/CHANGES index be67d3aed6..c6a9e0bd2b 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,9 @@ CHANGES "modified" history to be properly cleared after a flush() (backported from 0.5). + - added add() and add_all() methods to Session, + establishing forwards compatibility with 0.5. + - fixed bug preventing merge() from functioning in conjunction with a comparable_property() diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 641f87df4b..9efe52d847 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -930,6 +930,23 @@ class Session(object): self._save_or_update_impl(instance, entity_name=entity_name) self._cascade_save_or_update(instance) + def add(self, instance, entity_name=None): + """Add the given instance into this ``Session``. + + This provides forwards compatibility with 0.5. + + """ + self.save_or_update(instance, entity_name) + + def add_all(self, instances): + """Add the given collection of instances to this ``Session``. + + This provides forwards compatibility with 0.5. + """ + + for instance in instances: + self.add(instance) + def _cascade_save_or_update(self, instance): for obj, mapper in _cascade_iterator('save-update', instance, halt_on=lambda c:c in self): self._save_or_update_impl(obj, mapper.entity_name) diff --git a/test/orm/session.py b/test/orm/session.py index ca9aae835d..d9c11745f4 100644 --- a/test/orm/session.py +++ b/test/orm/session.py @@ -147,7 +147,23 @@ class SessionTest(TestBase, AssertsExecutionResults): session.flush(objects=set()) session.flush(objects=()) session.flush(objects=iter([])) - + + def test_forwards_compat_add(self): + class User(object):pass + mapper(User, users) + sess = create_session() + u1 = User() + sess.add(u1) + assert u1 in sess + u2, u3 = User(), User() + sess.add_all([u2, u3]) + assert u2, u3 in sess + sess.flush() + sess.expunge(u1) + assert u1 not in sess + sess.add(u1) + assert u1 in sess + @testing.unsupported('sqlite', 'mssql') # TEMP: test causes mssql to hang @engines.close_open_connections def test_autoflush(self):