Removing print statements from convert_gps_media_data migration
[mediagoblin.git] / mediagoblin / db / mongo / migrations.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 from mediagoblin.db.mongo.util import RegisterMigration
18 from mediagoblin.tools.text import cleaned_markdown_conversion
19
20
21 def add_table_field(db, table_name, field_name, default_value):
22 """
23 Add a new field to the table/collection named table_name.
24 The field will have the name field_name and the value default_value
25 """
26 db[table_name].update(
27 {field_name: {'$exists': False}},
28 {'$set': {field_name: default_value}},
29 multi=True)
30
31
32 def drop_table_field(db, table_name, field_name):
33 """
34 Drop an old field from a table/collection
35 """
36 db[table_name].update(
37 {field_name: {'$exists': True}},
38 {'$unset': {field_name: 1}},
39 multi=True)
40
41
42 # Please see mediagoblin/tests/test_migrations.py for some examples of
43 # basic migrations.
44
45
46 @RegisterMigration(1)
47 def user_add_bio_html(database):
48 """
49 Users now have richtext bios via Markdown, reflect appropriately.
50 """
51 collection = database['users']
52
53 target = collection.find(
54 {'bio_html': {'$exists': False}})
55
56 for document in target:
57 document['bio_html'] = cleaned_markdown_conversion(
58 document['bio'])
59 collection.save(document)
60
61
62 @RegisterMigration(2)
63 def mediaentry_mediafiles_main_to_original(database):
64 """
65 Rename "main" media file to "original".
66 """
67 collection = database['media_entries']
68 target = collection.find(
69 {'media_files.main': {'$exists': True}})
70
71 for document in target:
72 original = document['media_files'].pop('main')
73 document['media_files']['original'] = original
74
75 collection.save(document)
76
77
78 @RegisterMigration(3)
79 def mediaentry_remove_thumbnail_file(database):
80 """
81 Use media_files['thumb'] instead of media_entries['thumbnail_file']
82 """
83 database['media_entries'].update(
84 {'thumbnail_file': {'$exists': True}},
85 {'$unset': {'thumbnail_file': 1}},
86 multi=True)
87
88
89 @RegisterMigration(4)
90 def mediaentry_add_queued_task_id(database):
91 """
92 Add the 'queued_task_id' field for entries that don't have it.
93 """
94 add_table_field(database, 'media_entries', 'queued_task_id', None)
95
96
97 @RegisterMigration(5)
98 def mediaentry_add_fail_error_and_metadata(database):
99 """
100 Add 'fail_error' and 'fail_metadata' fields to media entries
101 """
102 add_table_field(database, 'media_entries', 'fail_error', None)
103 add_table_field(database, 'media_entries', 'fail_metadata', {})
104
105
106 @RegisterMigration(6)
107 def user_add_forgot_password_token_and_expires(database):
108 """
109 Add token and expiration fields to help recover forgotten passwords
110 """
111 add_table_field(database, 'users', 'fp_verification_key', None)
112 add_table_field(database, 'users', 'fp_token_expire', None)
113
114
115 @RegisterMigration(7)
116 def media_type_image_to_multimedia_type_image(database):
117 database['media_entries'].update(
118 {'media_type': 'image'},
119 {'$set': {'media_type': 'mediagoblin.media_types.image'}},
120 multi=True)
121
122 @RegisterMigration(8)
123 def mediaentry_add_license(database):
124 """
125 Add the 'license' field for entries that don't have it.
126 """
127 add_table_field(database, 'media_entries', 'license', None)
128
129
130 @RegisterMigration(9)
131 def remove_calculated_html(database):
132 """
133 Drop pre-rendered html again and calculate things
134 on the fly (and cache):
135 - User.bio_html
136 - MediaEntry.description_html
137 - MediaComment.content_html
138 """
139 drop_table_field(database, 'users', 'bio_html')
140 drop_table_field(database, 'media_entries', 'description_html')
141 drop_table_field(database, 'media_comments', 'content_html')
142
143 @RegisterMigration(10)
144 def convert_video_media_data(database):
145 """
146 Move media_data["video"] directly into media_data
147 """
148 collection = database['media_entries']
149 target = collection.find(
150 {'media_data.video': {'$exists': True}})
151
152 for document in target:
153 assert len(document['media_data']) == 1
154 document['media_data'] = document['media_data']['video']
155 collection.save(document)
156
157 @RegisterMigration(11)
158 def convert_gps_media_data(database):
159 """
160 Move media_data["gps"]["*"] to media_data["gps_*"].
161 In preparation for media_data.gps_*
162 """
163 collection = database['media_entries']
164 target = collection.find(
165 {'media_data.gps': {'$exists': True}})
166
167 for document in target:
168 for key, value in document['media_data']['gps'].iteritems():
169 document['media_data']['gps_' + key] = value
170 del document['media_data']['gps']
171 collection.save(document)