aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/sql
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/db/sql')
-rw-r--r--mediagoblin/db/sql/base.py38
-rw-r--r--mediagoblin/db/sql/convert.py27
-rw-r--r--mediagoblin/db/sql/models.py24
-rw-r--r--mediagoblin/db/sql/open.py33
4 files changed, 109 insertions, 13 deletions
diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py
new file mode 100644
index 00000000..40140327
--- /dev/null
+++ b/mediagoblin/db/sql/base.py
@@ -0,0 +1,38 @@
+from sqlalchemy.orm import scoped_session, sessionmaker, object_session
+
+
+Session = scoped_session(sessionmaker())
+
+
+def _fix_query_dict(query_dict):
+ if '_id' in query_dict:
+ query_dict['id'] = query_dict.pop('_id')
+
+
+class GMGTableBase(object):
+ query = Session.query_property()
+
+ @classmethod
+ def find(cls, query_dict={}):
+ _fix_query_dict(query_dict)
+ return cls.query.filter_by(**query_dict)
+
+ @classmethod
+ def find_one(cls, query_dict={}):
+ _fix_query_dict(query_dict)
+ return cls.query.filter_by(**query_dict).first()
+
+ @classmethod
+ def one(cls, query_dict):
+ return cls.find(query_dict).one()
+
+ def get(self, key):
+ return getattr(self, key)
+
+ def save(self, validate = True):
+ assert validate
+ sess = object_session(self)
+ if sess is None:
+ sess = Session()
+ sess.add(self)
+ sess.commit()
diff --git a/mediagoblin/db/sql/convert.py b/mediagoblin/db/sql/convert.py
index 2ffa9fd7..6698b767 100644
--- a/mediagoblin/db/sql/convert.py
+++ b/mediagoblin/db/sql/convert.py
@@ -1,13 +1,13 @@
-from sqlalchemy import create_engine
-from sqlalchemy.orm import sessionmaker
-
from mediagoblin.init import setup_global_and_app_config, setup_database
-from mediagoblin.db.util import ObjectId
+from mediagoblin.db.mongo.util import ObjectId
from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
Tag, MediaTag)
-
-Session = sessionmaker()
+from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
+ sql_connect
+from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
+ mongo_connect
+from mediagoblin.db.sql.base import Session
obj_id_table = dict()
@@ -61,7 +61,7 @@ def convert_media_entries(mk_db):
copy_attrs(entry, new_entry,
('title', 'slug', 'created',
'description', 'description_html',
- 'media_type',
+ 'media_type', 'state',
'fail_error',
'queued_task_id',))
copy_reference_attr(entry, new_entry, "uploader")
@@ -124,19 +124,22 @@ def convert_media_comments(mk_db):
def main():
- engine = create_engine('sqlite:///mediagoblin.db', echo=True)
- Session.configure(bind=engine)
+ global_config, app_config = setup_global_and_app_config("mediagoblin.ini")
- setup_global_and_app_config("mediagoblin.ini")
+ sql_conn, sql_db = sql_connect({'sql_engine': 'sqlite:///mediagoblin.db'})
- mk_conn, mk_db = setup_database()
+ mk_conn, mk_db = mongo_connect(app_config)
- Base.metadata.create_all(engine)
+ Base.metadata.create_all(sql_db.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..31a6ed3b 100644
--- a/mediagoblin/db/sql/models.py
+++ b/mediagoblin/db/sql/models.py
@@ -4,9 +4,24 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (
Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
UniqueConstraint)
+from sqlalchemy.orm import relationship
+from mediagoblin.db.sql.base import GMGTableBase
-Base = declarative_base()
+
+Base = declarative_base(cls=GMGTableBase)
+
+
+class SimpleFieldAlias(object):
+ """An alias for any field"""
+ def __init__(self, fieldname):
+ self.fieldname = fieldname
+
+ def __get__(self, instance, cls):
+ return getattr(instance, self.fieldname)
+
+ def __set__(self, instance, val):
+ setattr(instance, self.fieldname, val)
class User(Base):
@@ -30,6 +45,8 @@ class User(Base):
## TODO
# plugin data would be in a separate model
+ _id = SimpleFieldAlias("id")
+
class MediaEntry(Base):
__tablename__ = "media_entries"
@@ -42,6 +59,7 @@ class MediaEntry(Base):
description = Column(UnicodeText) # ??
description_html = Column(UnicodeText) # ??
media_type = Column(Unicode, nullable=False)
+ state = Column(Unicode, nullable=False) # or use sqlalchemy.types.Enum?
fail_error = Column(Unicode)
fail_metadata = Column(UnicodeText)
@@ -54,6 +72,8 @@ class MediaEntry(Base):
UniqueConstraint('uploader', 'slug'),
{})
+ get_uploader = relationship(User)
+
## TODO
# media_files
# media_data
@@ -95,6 +115,8 @@ class MediaComment(Base):
content = Column(UnicodeText, nullable=False)
content_html = Column(UnicodeText)
+ get_author = relationship(User)
+
def show_table_init():
from sqlalchemy import create_engine
diff --git a/mediagoblin/db/sql/open.py b/mediagoblin/db/sql/open.py
new file mode 100644
index 00000000..c682bd3b
--- /dev/null
+++ b/mediagoblin/db/sql/open.py
@@ -0,0 +1,33 @@
+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)
+
+
+def check_db_migrations_current(db):
+ pass