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.
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
)
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)
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)
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