From: Mike Bayer Date: Tue, 21 Sep 2010 13:33:30 +0000 (-0400) Subject: - scoped_session emits a warning when configure() is X-Git-Tag: rel_0_6_5~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=634d54742523883316bd7768c8d2918e8410aa62;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - scoped_session emits a warning when configure() is called if a Session is already present (checks only the current thread) [ticket:1924] --- diff --git a/CHANGES b/CHANGES index 04457ef447..4e49bb9237 100644 --- a/CHANGES +++ b/CHANGES @@ -84,6 +84,10 @@ CHANGES of the query only, instead of repeatedly. May make some more adjustments to this. + - scoped_session emits a warning when configure() is + called if a Session is already present (checks only the + current thread) [ticket:1924] + - sql - Table.tometadata() now copies Index objects associated with the Table as well. diff --git a/lib/sqlalchemy/orm/scoping.py b/lib/sqlalchemy/orm/scoping.py index c1a5fd5771..140328e248 100644 --- a/lib/sqlalchemy/orm/scoping.py +++ b/lib/sqlalchemy/orm/scoping.py @@ -6,7 +6,8 @@ import sqlalchemy.exceptions as sa_exc from sqlalchemy.util import ScopedRegistry, ThreadLocalRegistry, \ - to_list, get_cls_kwargs, deprecated + to_list, get_cls_kwargs, deprecated,\ + warn from sqlalchemy.orm import ( EXT_CONTINUE, MapperExtension, class_mapper, object_session ) @@ -45,7 +46,8 @@ class ScopedSession(object): scope = kwargs.pop('scope', False) if scope is not None: if self.registry.has(): - raise sa_exc.InvalidRequestError("Scoped session is already present; no new arguments may be specified.") + raise sa_exc.InvalidRequestError("Scoped session is already present; " + "no new arguments may be specified.") else: sess = self.session_factory(**kwargs) self.registry.set(sess) @@ -85,6 +87,11 @@ class ScopedSession(object): def configure(self, **kwargs): """reconfigure the sessionmaker used by this ScopedSession.""" + + if self.registry.has(): + warn('At least one scoped session is already present. ' + ' configure() can not affect sessions that have ' + 'already been created.') self.session_factory.configure(**kwargs) diff --git a/test/orm/test_scoping.py b/test/orm/test_scoping.py index 7db74308b3..1682e0f7e0 100644 --- a/test/orm/test_scoping.py +++ b/test/orm/test_scoping.py @@ -76,7 +76,22 @@ class ScopedSessionTest(_base.MappedTest): assert not isinstance(SomeOtherObject.query, CustomQuery) assert isinstance(SomeOtherObject.custom_query, query.Query) - + def test_config_errors(self): + Session = scoped_session(sa.orm.sessionmaker()) + + s = Session() + assert_raises_message( + sa.exc.InvalidRequestError, + "Scoped session is already present", + Session, bind=testing.db + ) + + assert_raises_message( + sa.exc.SAWarning, + "At least one scoped session is already present. ", + Session.configure, bind=testing.db + ) + class ScopedMapperTest(_ScopedTest): @classmethod