Merge branch 'elrond-sql-convert_video_data'
[mediagoblin.git] / mediagoblin / db / sql / convert.py
index c6bed1e922772ef048c64d1a160ac26343e49cda..086a5c9cb0481d101a21560e74ce4565241d87c4 100644 (file)
@@ -1,13 +1,30 @@
-from sqlalchemy import create_engine
-from sqlalchemy.orm import sessionmaker
+# GNU MediaGoblin -- federated, autonomous media hosting
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 
 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()
+    Tag, MediaTag, MediaFile, MediaAttachmentFile)
+from mediagoblin.media_types.video.models import VideoData
+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
 
 
@@ -33,14 +50,14 @@ def copy_reference_attr(entry, new_entry, ref_attr):
 def convert_users(mk_db):
     session = Session()
 
-    for entry in mk_db.User.find():
+    for entry in mk_db.User.find().sort('created'):
         print entry.username
 
         new_entry = User()
         copy_attrs(entry, new_entry,
             ('username', 'email', 'created', 'pw_hash', 'email_verified',
              'status', 'verification_key', 'is_admin', 'url',
-             'bio', 'bio_html',
+             'bio',
              'fp_verification_key', 'fp_token_expire',))
         # new_entry.fp_verification_expire = entry.fp_token_expire
 
@@ -55,15 +72,15 @@ def convert_users(mk_db):
 def convert_media_entries(mk_db):
     session = Session()
 
-    for entry in mk_db.MediaEntry.find():
+    for entry in mk_db.MediaEntry.find().sort('created'):
         print repr(entry.title)
 
         new_entry = MediaEntry()
         copy_attrs(entry, new_entry,
             ('title', 'slug', 'created',
-             'description', 'description_html',
-             'media_type', 'state',
-             'fail_error',
+             'description',
+             'media_type', 'state', 'license',
+             'fail_error', 'fail_metadata',
              'queued_task_id',))
         copy_reference_attr(entry, new_entry, "uploader")
 
@@ -71,6 +88,33 @@ def convert_media_entries(mk_db):
         session.flush()
         add_obj_ids(entry, new_entry)
 
+        for key, value in entry.media_files.iteritems():
+            new_file = MediaFile(name=key, file_path=value)
+            new_file.media_entry = new_entry.id
+            Session.add(new_file)
+
+        for attachment in entry.attachment_files:
+            new_attach = MediaAttachmentFile(
+                name=attachment["name"],
+                filepath=attachment["filepath"],
+                created=attachment["created"]
+                )
+            new_attach.media_entry = new_entry.id
+            Session.add(new_attach)
+
+    session.commit()
+    session.close()
+
+
+def convert_video(mk_db):
+    session = Session()
+
+    for media in mk_db.MediaEntry.find(
+            {'media_type': 'mediagoblin.media_types.video'}).sort('created'):
+        media_data_row = VideoData(**media.media_data)
+        media_data_row.media_entry = obj_id_table[media._id]
+        session.add(media_data_row)
+
     session.commit()
     session.close()
 
@@ -79,7 +123,7 @@ def convert_media_tags(mk_db):
     session = Session()
     session.autoflush = False
 
-    for media in mk_db.MediaEntry.find():
+    for media in mk_db.MediaEntry.find().sort('created'):
         print repr(media.title)
 
         for otag in media.tags:
@@ -106,13 +150,13 @@ def convert_media_tags(mk_db):
 def convert_media_comments(mk_db):
     session = Session()
 
-    for entry in mk_db.MediaComment.find():
+    for entry in mk_db.MediaComment.find().sort('created'):
         print repr(entry.content)
 
         new_entry = MediaComment()
         copy_attrs(entry, new_entry,
             ('created',
-             'content', 'content_html',))
+             'content',))
         copy_reference_attr(entry, new_entry, "media_entry")
         copy_reference_attr(entry, new_entry, "author")
 
@@ -124,20 +168,20 @@ def convert_media_comments(mk_db):
     session.close()
 
 
-def main():
-    engine = create_engine('sqlite:///mediagoblin.db', echo=True)
-    Session.configure(bind=engine)
-
-    setup_global_and_app_config("mediagoblin.ini")
+def run_conversion(config_name):
+    global_config, app_config = setup_global_and_app_config(config_name)
 
-    mk_conn, mk_db = setup_database()
+    sql_conn, sql_db = sql_connect(app_config)
+    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_video(mk_db)
+    Session.remove()
     convert_media_tags(mk_db)
     Session.remove()
     convert_media_comments(mk_db)
@@ -145,4 +189,4 @@ def main():
 
 
 if __name__ == '__main__':
-    main()
+    run_conversion("mediagoblin.ini")