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
This commit is contained in:
parent
18517e888a
commit
7b194a79f0
16
mediagoblin/db/sql/base.py
Normal file
16
mediagoblin/db/sql/base.py
Normal file
@ -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()
|
@ -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__':
|
||||
|
@ -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):
|
||||
|
29
mediagoblin/db/sql/open.py
Normal file
29
mediagoblin/db/sql/open.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user