SQL: mongokit like interface
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 18 Dec 2011 16:02:27 +0000 (17:02 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 18 Dec 2011 16:07:15 +0000 (17:07 +0100)
In trying to ease the migration to SQL, created an
interface to sqlalchemy that looks a lot like the interface
that is currently in use.

*WARNING* Work in progress

mediagoblin/db/sql/base.py [new file with mode: 0644]
mediagoblin/db/sql/convert.py
mediagoblin/db/sql/models.py
mediagoblin/db/sql/open.py [new file with mode: 0644]

diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py
new file mode 100644 (file)
index 0000000..b8d5cc9
--- /dev/null
@@ -0,0 +1,16 @@
+from sqlalchemy.orm import scoped_session, sessionmaker
+
+
+Session = scoped_session(sessionmaker())
+
+
+class GMGTableBase(object):
+    query = Session.query_property()
+
+    @classmethod
+    def find(cls, query_dict={}):
+        return cls.query.filter_by(**query_dict)
+
+    @classmethod
+    def find_one(cls, query_dict={}):
+        return cls.query.filter_by(**query_dict).first()
index 2ffa9fd78529f495ba2c3fc5936ae06a96ed26c7..6de758ede2ed630871441806a9f5e137cceaeae3 100644 (file)
@@ -7,7 +7,8 @@ from mediagoblin.db.util import ObjectId
 from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
     Tag, MediaTag)
 
-Session = sessionmaker()
+# Session = sessionmaker()
+from mediagoblin.db.sql.base import Session
 
 
 obj_id_table = dict()
@@ -134,9 +135,13 @@ def main():
     Base.metadata.create_all(engine)
 
     convert_users(mk_db)
+    Session.remove()
     convert_media_entries(mk_db)
+    Session.remove()
     convert_media_tags(mk_db)
+    Session.remove()
     convert_media_comments(mk_db)
+    Session.remove()
 
 
 if __name__ == '__main__':
index 7723a7538a0379e50828d45bef6c839578339599..b87ff3aacfcf585ff1a46c4b2a7edaa32191822f 100644 (file)
@@ -5,8 +5,10 @@ from sqlalchemy import (
     Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
     UniqueConstraint)
 
+from mediagoblin.db.sql.base import GMGTableBase
 
-Base = declarative_base()
+
+Base = declarative_base(cls=GMGTableBase)
 
 
 class User(Base):
diff --git a/mediagoblin/db/sql/open.py b/mediagoblin/db/sql/open.py
new file mode 100644 (file)
index 0000000..57feaf5
--- /dev/null
@@ -0,0 +1,29 @@
+from sqlalchemy import create_engine
+
+from mediagoblin.db.sql.base import Session
+from mediagoblin.db.sql.models import Base
+
+
+class DatabaseMaster(object):
+    def __init__(self, engine):
+        self.engine = engine
+
+        for k,v in Base._decl_class_registry.iteritems():
+            setattr(self, k, v)
+
+    def commit(self):
+        Session.commit()
+
+    def save(self, obj):
+        Session.add(obj)
+        Session.flush()
+
+    def reset_after_request(self):
+        Session.remove()
+
+
+def setup_connection_and_db_from_config(app_config):
+    engine = create_engine(app_config['sql_engine'], echo=True)
+    Session.configure(bind=engine)
+
+    return "dummy conn", DatabaseMaster(engine)