1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from mediagoblin
.db
.mongo
.util
import RegisterMigration
18 from mediagoblin
.tools
.text
import cleaned_markdown_conversion
21 def add_table_field(db
, table_name
, field_name
, default_value
):
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
26 db
[table_name
].update(
27 {field_name
: {'$exists': False}},
28 {'$set': {field_name
: default_value
}},
32 def drop_table_field(db
, table_name
, field_name
):
34 Drop an old field from a table/collection
36 db
[table_name
].update(
37 {field_name
: {'$exists': True}},
38 {'$unset': {field_name
: 1}},
42 # Please see mediagoblin/tests/test_migrations.py for some examples of
47 def user_add_bio_html(database
):
49 Users now have richtext bios via Markdown, reflect appropriately.
51 collection
= database
['users']
53 target
= collection
.find(
54 {'bio_html': {'$exists': False}})
56 for document
in target
:
57 document
['bio_html'] = cleaned_markdown_conversion(
59 collection
.save(document
)
63 def mediaentry_mediafiles_main_to_original(database
):
65 Rename "main" media file to "original".
67 collection
= database
['media_entries']
68 target
= collection
.find(
69 {'media_files.main': {'$exists': True}})
71 for document
in target
:
72 original
= document
['media_files'].pop('main')
73 document
['media_files']['original'] = original
75 collection
.save(document
)
79 def mediaentry_remove_thumbnail_file(database
):
81 Use media_files['thumb'] instead of media_entries['thumbnail_file']
83 database
['media_entries'].update(
84 {'thumbnail_file': {'$exists': True}},
85 {'$unset': {'thumbnail_file': 1}},
90 def mediaentry_add_queued_task_id(database
):
92 Add the 'queued_task_id' field for entries that don't have it.
94 add_table_field(database
, 'media_entries', 'queued_task_id', None)
98 def mediaentry_add_fail_error_and_metadata(database
):
100 Add 'fail_error' and 'fail_metadata' fields to media entries
102 add_table_field(database
, 'media_entries', 'fail_error', None)
103 add_table_field(database
, 'media_entries', 'fail_metadata', {})
106 @RegisterMigration(6)
107 def user_add_forgot_password_token_and_expires(database
):
109 Add token and expiration fields to help recover forgotten passwords
111 add_table_field(database
, 'users', 'fp_verification_key', None)
112 add_table_field(database
, 'users', 'fp_token_expire', None)
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'}},
122 @RegisterMigration(8)
123 def mediaentry_add_license(database
):
125 Add the 'license' field for entries that don't have it.
127 add_table_field(database
, 'media_entries', 'license', None)
130 @RegisterMigration(9)
131 def remove_calculated_html(database
):
133 Drop pre-rendered html again and calculate things
134 on the fly (and cache):
136 - MediaEntry.description_html
137 - MediaComment.content_html
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')
143 @RegisterMigration(10)
144 def convert_video_media_data(database
):
146 Move media_data["video"] directly into media_data
148 collection
= database
['media_entries']
149 target
= collection
.find(
150 {'media_data.video': {'$exists': True}})
152 for document
in target
:
153 assert len(document
['media_data']) == 1
154 document
['media_data'] = document
['media_data']['video']
155 collection
.save(document
)
157 @RegisterMigration(11)
158 def convert_gps_media_data(database
):
160 Move media_data["gps"]["*"] to media_data["gps_*"].
161 In preparation for media_data.gps_*
163 collection
= database
['media_entries']
164 target
= collection
.find(
165 {'media_data.gps': {'$exists': True}})
167 for document
in target
:
168 print document
['_id'], "old:", document
['media_data']
169 for key
, value
in document
['media_data']['gps'].iteritems():
170 document
['media_data']['gps_' + key
] = value
171 del document
['media_data']['gps']
172 print document
['_id'], "new:", document
['media_data']
173 collection
.save(document
)