Merge branch 'elrond-sql-convert_video_data'
[mediagoblin.git] / mediagoblin / db / sql / convert.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 from mediagoblin.init import setup_global_and_app_config, setup_database
19 from mediagoblin.db.mongo.util import ObjectId
20
21 from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
22 Tag, MediaTag, MediaFile, MediaAttachmentFile)
23 from mediagoblin.media_types.video.models import VideoData
24 from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
25 sql_connect
26 from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
27 mongo_connect
28 from mediagoblin.db.sql.base import Session
29
30
31 obj_id_table = dict()
32
33 def add_obj_ids(entry, new_entry):
34 global obj_id_table
35 print "%r -> %r" % (entry._id, new_entry.id)
36 obj_id_table[entry._id] = new_entry.id
37
38
39 def copy_attrs(entry, new_entry, attr_list):
40 for a in attr_list:
41 val = entry[a]
42 setattr(new_entry, a, val)
43
44 def copy_reference_attr(entry, new_entry, ref_attr):
45 val = entry[ref_attr]
46 val = obj_id_table[val]
47 setattr(new_entry, ref_attr, val)
48
49
50 def convert_users(mk_db):
51 session = Session()
52
53 for entry in mk_db.User.find().sort('created'):
54 print entry.username
55
56 new_entry = User()
57 copy_attrs(entry, new_entry,
58 ('username', 'email', 'created', 'pw_hash', 'email_verified',
59 'status', 'verification_key', 'is_admin', 'url',
60 'bio',
61 'fp_verification_key', 'fp_token_expire',))
62 # new_entry.fp_verification_expire = entry.fp_token_expire
63
64 session.add(new_entry)
65 session.flush()
66 add_obj_ids(entry, new_entry)
67
68 session.commit()
69 session.close()
70
71
72 def convert_media_entries(mk_db):
73 session = Session()
74
75 for entry in mk_db.MediaEntry.find().sort('created'):
76 print repr(entry.title)
77
78 new_entry = MediaEntry()
79 copy_attrs(entry, new_entry,
80 ('title', 'slug', 'created',
81 'description',
82 'media_type', 'state', 'license',
83 'fail_error', 'fail_metadata',
84 'queued_task_id',))
85 copy_reference_attr(entry, new_entry, "uploader")
86
87 session.add(new_entry)
88 session.flush()
89 add_obj_ids(entry, new_entry)
90
91 for key, value in entry.media_files.iteritems():
92 new_file = MediaFile(name=key, file_path=value)
93 new_file.media_entry = new_entry.id
94 Session.add(new_file)
95
96 for attachment in entry.attachment_files:
97 new_attach = MediaAttachmentFile(
98 name=attachment["name"],
99 filepath=attachment["filepath"],
100 created=attachment["created"]
101 )
102 new_attach.media_entry = new_entry.id
103 Session.add(new_attach)
104
105 session.commit()
106 session.close()
107
108
109 def convert_video(mk_db):
110 session = Session()
111
112 for media in mk_db.MediaEntry.find(
113 {'media_type': 'mediagoblin.media_types.video'}).sort('created'):
114 media_data_row = VideoData(**media.media_data)
115 media_data_row.media_entry = obj_id_table[media._id]
116 session.add(media_data_row)
117
118 session.commit()
119 session.close()
120
121
122 def convert_media_tags(mk_db):
123 session = Session()
124 session.autoflush = False
125
126 for media in mk_db.MediaEntry.find().sort('created'):
127 print repr(media.title)
128
129 for otag in media.tags:
130 print " ", repr((otag["slug"], otag["name"]))
131
132 nslug = session.query(Tag).filter_by(slug=otag["slug"]).first()
133 print " ", repr(nslug)
134 if nslug is None:
135 nslug = Tag(slug=otag["slug"])
136 session.add(nslug)
137 session.flush()
138 print " ", repr(nslug), nslug.id
139
140 ntag = MediaTag()
141 ntag.tag = nslug.id
142 ntag.name = otag["name"]
143 ntag.media_entry = obj_id_table[media._id]
144 session.add(ntag)
145
146 session.commit()
147 session.close()
148
149
150 def convert_media_comments(mk_db):
151 session = Session()
152
153 for entry in mk_db.MediaComment.find().sort('created'):
154 print repr(entry.content)
155
156 new_entry = MediaComment()
157 copy_attrs(entry, new_entry,
158 ('created',
159 'content',))
160 copy_reference_attr(entry, new_entry, "media_entry")
161 copy_reference_attr(entry, new_entry, "author")
162
163 session.add(new_entry)
164 session.flush()
165 add_obj_ids(entry, new_entry)
166
167 session.commit()
168 session.close()
169
170
171 def run_conversion(config_name):
172 global_config, app_config = setup_global_and_app_config(config_name)
173
174 sql_conn, sql_db = sql_connect(app_config)
175 mk_conn, mk_db = mongo_connect(app_config)
176
177 Base.metadata.create_all(sql_db.engine)
178
179 convert_users(mk_db)
180 Session.remove()
181 convert_media_entries(mk_db)
182 Session.remove()
183 convert_video(mk_db)
184 Session.remove()
185 convert_media_tags(mk_db)
186 Session.remove()
187 convert_media_comments(mk_db)
188 Session.remove()
189
190
191 if __name__ == '__main__':
192 run_conversion("mediagoblin.ini")