SQL: fail_metadata as JSON encoded field
[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)
23 from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
24 sql_connect
25 from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
26 mongo_connect
27 from mediagoblin.db.sql.base import Session
28
29
30 obj_id_table = dict()
31
32 def add_obj_ids(entry, new_entry):
33 global obj_id_table
34 print "%r -> %r" % (entry._id, new_entry.id)
35 obj_id_table[entry._id] = new_entry.id
36
37
38 def copy_attrs(entry, new_entry, attr_list):
39 for a in attr_list:
40 val = entry[a]
41 setattr(new_entry, a, val)
42
43 def copy_reference_attr(entry, new_entry, ref_attr):
44 val = entry[ref_attr]
45 val = obj_id_table[val]
46 setattr(new_entry, ref_attr, val)
47
48
49 def convert_users(mk_db):
50 session = Session()
51
52 for entry in mk_db.User.find():
53 print entry.username
54
55 new_entry = User()
56 copy_attrs(entry, new_entry,
57 ('username', 'email', 'created', 'pw_hash', 'email_verified',
58 'status', 'verification_key', 'is_admin', 'url',
59 'bio',
60 'fp_verification_key', 'fp_token_expire',))
61 # new_entry.fp_verification_expire = entry.fp_token_expire
62
63 session.add(new_entry)
64 session.flush()
65 add_obj_ids(entry, new_entry)
66
67 session.commit()
68 session.close()
69
70
71 def convert_media_entries(mk_db):
72 session = Session()
73
74 for entry in mk_db.MediaEntry.find():
75 print repr(entry.title)
76
77 new_entry = MediaEntry()
78 copy_attrs(entry, new_entry,
79 ('title', 'slug', 'created',
80 'description',
81 'media_type', 'state', 'license',
82 'fail_error', 'fail_metadata',
83 'queued_task_id',))
84 copy_reference_attr(entry, new_entry, "uploader")
85
86 session.add(new_entry)
87 session.flush()
88 add_obj_ids(entry, new_entry)
89
90 for key, value in entry.media_files.iteritems():
91 new_file = MediaFile(name=key, file_path=value)
92 new_file.media_entry = new_entry.id
93 Session.add(new_file)
94
95 session.commit()
96 session.close()
97
98
99 def convert_media_tags(mk_db):
100 session = Session()
101 session.autoflush = False
102
103 for media in mk_db.MediaEntry.find():
104 print repr(media.title)
105
106 for otag in media.tags:
107 print " ", repr((otag["slug"], otag["name"]))
108
109 nslug = session.query(Tag).filter_by(slug=otag["slug"]).first()
110 print " ", repr(nslug)
111 if nslug is None:
112 nslug = Tag(slug=otag["slug"])
113 session.add(nslug)
114 session.flush()
115 print " ", repr(nslug), nslug.id
116
117 ntag = MediaTag()
118 ntag.tag = nslug.id
119 ntag.name = otag["name"]
120 ntag.media_entry = obj_id_table[media._id]
121 session.add(ntag)
122
123 session.commit()
124 session.close()
125
126
127 def convert_media_comments(mk_db):
128 session = Session()
129
130 for entry in mk_db.MediaComment.find():
131 print repr(entry.content)
132
133 new_entry = MediaComment()
134 copy_attrs(entry, new_entry,
135 ('created',
136 'content',))
137 copy_reference_attr(entry, new_entry, "media_entry")
138 copy_reference_attr(entry, new_entry, "author")
139
140 session.add(new_entry)
141 session.flush()
142 add_obj_ids(entry, new_entry)
143
144 session.commit()
145 session.close()
146
147
148 def main():
149 global_config, app_config = setup_global_and_app_config("mediagoblin.ini")
150
151 sql_conn, sql_db = sql_connect({'sql_engine': 'sqlite:///mediagoblin.db'})
152
153 mk_conn, mk_db = mongo_connect(app_config)
154
155 Base.metadata.create_all(sql_db.engine)
156
157 convert_users(mk_db)
158 Session.remove()
159 convert_media_entries(mk_db)
160 Session.remove()
161 convert_media_tags(mk_db)
162 Session.remove()
163 convert_media_comments(mk_db)
164 Session.remove()
165
166
167 if __name__ == '__main__':
168 main()