]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- scoped_session emits a warning when configure() is
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 21 Sep 2010 13:33:30 +0000 (09:33 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 21 Sep 2010 13:33:30 +0000 (09:33 -0400)
called if a Session is already present (checks only the
current thread) [ticket:1924]

CHANGES
lib/sqlalchemy/orm/scoping.py
test/orm/test_scoping.py

diff --git a/CHANGES b/CHANGES
index 04457ef447e90ae4ead6ee3512633003818062ee..4e49bb9237659536b0d4f994a069f5830510a157 100644 (file)
--- 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.
index c1a5fd577142bafe1043b3c5ac9aaafa3db25764..140328e2484b6a7e0c21f7209d11216380ba8abb 100644 (file)
@@ -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)
 
index 7db74308b30bac4d645ee9b64de0c8a76b1a847f..1682e0f7e007c82995d008dc7314d17bcb1f150b 100644 (file)
@@ -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