Adding DatabaseManager as an alternative to DatabaseMaster
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sun, 30 Nov 2014 19:29:59 +0000 (13:29 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 3 Dec 2014 21:40:57 +0000 (15:40 -0600)
This is another step to get to a no-globals configuration.  I'm not
sure if this code works yet or not... :)

mediagoblin/db/open.py

index 34f0bffa15857568ca5d17a642a9487a081bc533..3f86319058db2bf6c1b3a013ea133ee8bb321cff 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from sqlalchemy import create_engine, event
+from contextlib import contextmanager
 import logging
 
 import six
+from sqlalchemy import create_engine, event
 
-from mediagoblin.db.base import Base, Session
 from mediagoblin import mg_globals
+from mediagoblin.db.base import Base
 
 _log = logging.getLogger(__name__)
 
+from mediagoblin.tools.transition import DISABLE_GLOBALS
+
+if DISABLE_GLOBALS:
+    from mediagoblin.db.base import Session
+
+    class DatabaseMaster(object):
+        def __init__(self, engine):
+            self.engine = engine
+
+            for k, v in six.iteritems(Base._decl_class_registry):
+                setattr(self, k, v)
+
+        def commit(self):
+            Session.commit()
+
+        def save(self, obj):
+            Session.add(obj)
+            Session.flush()
+
+        def check_session_clean(self):
+            for dummy in Session():
+                _log.warn("STRANGE: There are elements in the sql session. "
+                          "Please report this and help us track this down.")
+                break
+
+        def reset_after_request(self):
+            Session.rollback()
+            Session.remove()
+
+else:
+    from sqlalchemy.orm import sessionmaker
+
+    class DatabaseManager(object):
+        def __init__(self, engine):
+            self.engine = engine
+            self.Session = sessionmaker(bind=engine)
+
+        @contextmanager
+        def session_scope(self):
+            session = self.Session()
+
+            #####################################
+            # Functions to emulate DatabaseMaster
+            #####################################
+            def commit():
+                session.commit()
+
+            def save(obj):
+                session.add(obj)
+                session.flush()
+
+            def check_session_clean():
+                # Is this implemented right?
+                for dummy in session:
+                    _log.warn("STRANGE: There are elements in the sql session. "
+                              "Please report this and help us track this down.")
+                    break
+
+            def reset_after_request():
+                session.rollback()
+                session.remove()
+
+            # now attach
+            session.commit = commit
+            session.save = save
+            session.check_session_clean = check_session_clean
+            session.reset_after_request = reset_after_request
+            #####################################
+
+            try:
+                yield session
+            finally:
+                session.rollback()
+                session.close()
 
-class DatabaseMaster(object):
-    def __init__(self, engine):
-        self.engine = engine
-
-        for k, v in six.iteritems(Base._decl_class_registry):
-            setattr(self, k, v)
-
-    def commit(self):
-        Session.commit()
-
-    def save(self, obj):
-        Session.add(obj)
-        Session.flush()
-
-    def check_session_clean(self):
-        for dummy in Session():
-            _log.warn("STRANGE: There are elements in the sql session. "
-                      "Please report this and help us track this down.")
-            break
-
-    def reset_after_request(self):
-        Session.rollback()
-        Session.remove()
 
 
 def load_models(app_config):