From 7b194a79f0ad789309b9c34340f19c5a962b0915 Mon Sep 17 00:00:00 2001 From: Elrond Date: Sun, 18 Dec 2011 17:02:27 +0100 Subject: [PATCH] SQL: mongokit like interface 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 | 16 ++++++++++++++++ mediagoblin/db/sql/convert.py | 7 ++++++- mediagoblin/db/sql/models.py | 4 +++- mediagoblin/db/sql/open.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 mediagoblin/db/sql/base.py create mode 100644 mediagoblin/db/sql/open.py diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py new file mode 100644 index 00000000..b8d5cc96 --- /dev/null +++ b/mediagoblin/db/sql/base.py @@ -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() diff --git a/mediagoblin/db/sql/convert.py b/mediagoblin/db/sql/convert.py index 2ffa9fd7..6de758ed 100644 --- a/mediagoblin/db/sql/convert.py +++ b/mediagoblin/db/sql/convert.py @@ -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__': diff --git a/mediagoblin/db/sql/models.py b/mediagoblin/db/sql/models.py index 7723a753..b87ff3aa 100644 --- a/mediagoblin/db/sql/models.py +++ b/mediagoblin/db/sql/models.py @@ -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 index 00000000..57feaf50 --- /dev/null +++ b/mediagoblin/db/sql/open.py @@ -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) -- 2.25.1