Merge branch 'master' into jwandborg-f482_media_attachments
[mediagoblin.git] / mediagoblin / db / migrations.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.util import cleaned_markdown_conversion
19
20
21 # Please see mediagoblin/tests/test_migrations.py for some examples of
22 # basic migrations.
23
24
25 @RegisterMigration(1)
26 def user_add_bio_html(database):
27 """
28 Users now have richtext bios via Markdown, reflect appropriately.
29 """
30 collection = database['users']
31
32 target = collection.find(
33 {'bio_html': {'$exists': False}})
34
35 for document in target:
36 document['bio_html'] = cleaned_markdown_conversion(
37 document['bio'])
38 collection.save(document)
39
40
41 @RegisterMigration(2)
42 def mediaentry_mediafiles_main_to_original(database):
43 """
44 Rename "main" media file to "original".
45 """
46 collection = database['media_entries']
47 target = collection.find(
48 {'media_files.main': {'$exists': True}})
49
50 for document in target:
51 original = document['media_files'].pop('main')
52 document['media_files']['original'] = original
53
54 collection.save(document)
55
56
57 @RegisterMigration(3)
58 def mediaentry_remove_thumbnail_file(database):
59 """
60 Use media_files['thumb'] instead of media_entries['thumbnail_file']
61 """
62 database['media_entries'].update(
63 {'thumbnail_file': {'$exists': True}},
64 {'$unset': {'thumbnail_file': 1}},
65 multi=True)
66
67
68 @RegisterMigration(4)
69 def mediaentry_add_queued_task_id(database):
70 """
71 Add the 'queued_task_id' field for entries that don't have it.
72 """
73 collection = database['media_entries']
74 collection.update(
75 {'queued_task_id': {'$exists': False}},
76 {'$set': {'queued_task_id': None}},
77 multi=True)
78
79
80 @RegisterMigration(5)
81 def mediaentry_add_fail_error_and_metadata(database):
82 """
83 Add 'fail_error' and 'fail_metadata' fields to media entries
84 """
85 collection = database['media_entries']
86 collection.update(
87 {'fail_error': {'$exists': False}},
88 {'$set': {'fail_error': None}},
89 multi=True)
90
91 collection.update(
92 {'fail_metadata': {'$exists': False}},
93 {'$set': {'fail_metadata': {}}},
94 multi=True)