SQL: mongokit like interface
[mediagoblin.git] / mediagoblin / db / migrations.py
CommitLineData
757f37a5 1# GNU MediaGoblin -- federated, autonomous media hosting
12a100e4 2# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
757f37a5
CAW
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
c2ddd85e 17from mediagoblin.db.util import RegisterMigration
152a3bfa 18from mediagoblin.tools.text import cleaned_markdown_conversion
5ebe69e5 19
a01d04a0 20
03d47730
E
21def 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
0c915735
CAW
32# Please see mediagoblin/tests/test_migrations.py for some examples of
33# basic migrations.
34
5ebe69e5
CAW
35
36@RegisterMigration(1)
37def 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)
fa92d52f
CAW
50
51
52@RegisterMigration(2)
53def 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)
6b9ee0ca
CAW
66
67
68@RegisterMigration(3)
84abd2bb
CFD
69def 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}},
fabdccd0 76 multi=True)
ba4858c5
CAW
77
78
79@RegisterMigration(4)
6b9ee0ca
CAW
80def mediaentry_add_queued_task_id(database):
81 """
82 Add the 'queued_task_id' field for entries that don't have it.
83 """
03d47730 84 add_table_field(database, 'media_entries', 'queued_task_id', None)
6c50c210
CAW
85
86
87@RegisterMigration(5)
88def mediaentry_add_fail_error_and_metadata(database):
89 """
90 Add 'fail_error' and 'fail_metadata' fields to media entries
91 """
03d47730
E
92 add_table_field(database, 'media_entries', 'fail_error', None)
93 add_table_field(database, 'media_entries', 'fail_metadata', {})
25ba955e
AV
94
95
96@RegisterMigration(6)
97def user_add_forgot_password_token_and_expires(database):
98 """
99 Add token and expiration fields to help recover forgotten passwords
100 """
03d47730
E
101 add_table_field(database, 'users', 'fp_verification_key', None)
102 add_table_field(database, 'users', 'fp_token_expire', None)
93bdab9d
JW
103
104
105@RegisterMigration(7)
106def 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)