From: Mike Bayer Date: Sat, 19 Jul 2008 15:47:21 +0000 (+0000) Subject: - Added a new SessionExtension hook called after_attach(). X-Git-Tag: rel_0_4_7~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d6acc9fcea35067327c9472d6bc23da2aa908fd4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Added a new SessionExtension hook called after_attach(). This is called at the point of attachment for objects via save(), update(), delete(), and merge() (backported from 0.5). --- diff --git a/CHANGES b/CHANGES index ebc85b4297..102e71c94f 100644 --- a/CHANGES +++ b/CHANGES @@ -31,6 +31,11 @@ CHANGES - Removed erroneous 'self' reference when raising UnmappedColumnError during flush() operation. + - Added a new SessionExtension hook called after_attach(). + This is called at the point of attachment for objects + via save(), update(), delete(), and merge() + (backported from 0.5). + - sql - Fixed bug when calling select([literal('foo')]) or select([bindparam('foo')]). diff --git a/VERSION b/VERSION index ef52a64807..f905682709 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.6 +0.4.7 diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 3dfe0411c3..641f87df4b 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -135,6 +135,9 @@ class SessionExtension(object): engine level transaction is begun on a connection. """ + def after_attach(self, session, instance): + """Execute after an instance is attached to a session.""" + class SessionTransaction(object): """Represents a Session-level Transaction. @@ -1123,6 +1126,9 @@ class Session(object): self.identity_map[key] = instance instance._sa_session_id = self.hash_key + if self.extension is not None: + self.extension.after_attach(self, instance) + def _unattach(self, instance): if instance._sa_session_id == self.hash_key: del instance._sa_session_id diff --git a/test/orm/session.py b/test/orm/session.py index eae0eab112..ca9aae835d 100644 --- a/test/orm/session.py +++ b/test/orm/session.py @@ -890,18 +890,21 @@ class SessionTest(TestBase, AssertsExecutionResults): log.append('after_flush_postexec') def after_begin(self, session, transaction, connection): log.append('after_begin') + def after_attach(self, session, instance): + log.append('after_attach') + sess = create_session(extension = MyExt()) u = User() sess.save(u) sess.flush() - assert log == ['before_flush', 'after_begin', 'after_flush', 'before_commit', 'after_commit', 'after_flush_postexec'] + assert log == ['after_attach', 'before_flush', 'after_begin', 'after_flush', 'before_commit', 'after_commit', 'after_flush_postexec'] log = [] sess = create_session(transactional=True, extension=MyExt()) u = User() sess.save(u) sess.flush() - assert log == ['before_flush', 'after_begin', 'after_flush', 'after_flush_postexec'] + assert log == ['after_attach', 'before_flush', 'after_begin', 'after_flush', 'after_flush_postexec'] log = [] u.user_name = 'ed'