Merge remote-tracking branch 'remotes/jwandborg/master'
[mediagoblin.git] / mediagoblin / db / migrations.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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.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 # Please see mediagoblin/tests/test_migrations.py for some examples of
33 # basic migrations.
34
35
36 @RegisterMigration(1)
37 def user_add_bio_html(database):
38 """
39 Users now have richtext bios via Markdown, reflect appropriately.
40 """
41 collection = database['users']
42
43 target = collection.find(
44 {'bio_html': {'$exists': False}})
45
46 for document in target:
47 document['bio_html'] = cleaned_markdown_conversion(
48 document['bio'])
49 collection.save(document)
50
51
52 @RegisterMigration(2)
53 def mediaentry_mediafiles_main_to_original(database):
54 """
55 Rename "main" media file to "original".
56 """
57 collection = database['media_entries']
58 target = collection.find(
59 {'media_files.main': {'$exists': True}})
60
61 for document in target:
62 original = document['media_files'].pop('main')
63 document['media_files']['original'] = original
64
65 collection.save(document)
66
67
68 @RegisterMigration(3)
69 def mediaentry_remove_thumbnail_file(database):
70 """
71 Use media_files['thumb'] instead of media_entries['thumbnail_file']
72 """
73 database['media_entries'].update(
74 {'thumbnail_file': {'$exists': True}},
75 {'$unset': {'thumbnail_file': 1}},
76 multi=True)
77
78
79 @RegisterMigration(4)
80 def mediaentry_add_queued_task_id(database):
81 """
82 Add the 'queued_task_id' field for entries that don't have it.
83 """
84 add_table_field(database, 'media_entries', 'queued_task_id', None)
85
86
87 @RegisterMigration(5)
88 def mediaentry_add_fail_error_and_metadata(database):
89 """
90 Add 'fail_error' and 'fail_metadata' fields to media entries
91 """
92 add_table_field(database, 'media_entries', 'fail_error', None)
93 add_table_field(database, 'media_entries', 'fail_metadata', {})
94
95
96 @RegisterMigration(6)
97 def user_add_forgot_password_token_and_expires(database):
98 """
99 Add token and expiration fields to help recover forgotten passwords
100 """
101 add_table_field(database, 'users', 'fp_verification_key', None)
102 add_table_field(database, 'users', 'fp_token_expire', None)
103
104
105 @RegisterMigration(7)
106 def media_type_image_to_multimedia_type_image(database):
107 database['media_entries'].update(
108 {'media_type': 'image'},
109 {'$set': {'media_type': 'mediagoblin.media_types.image'}},
110 multi=True)