4cafb1a3cc64a18e4104b66ff0be404a396cb53b
[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.util import cleaned_markdown_conversion
18 # from mediagoblin.db.util import RegisterMigration
19
20 from mongokit import DocumentMigration
21
22
23 # This is where the first new-style migration will be written!
24 #
25 # Please see mediagoblin/tests/test_migrations.py for some examples of
26 # basic migrations.
27
28 # @RegisterMigration(1)
29 # def do_something(database):
30 # pass
31
32
33 class MediaEntryMigration(DocumentMigration):
34 def allmigration01_uploader_to_reference(self):
35 """
36 Old MediaEntry['uploader'] accidentally embedded the User instead
37 of referencing it. Fix that!
38 """
39 # uploader is an associative array
40 self.target = {'uploader': {'$type': 3}}
41 if not self.status:
42 for doc in self.collection.find(self.target):
43 self.update = {
44 '$set': {
45 'uploader': doc['uploader']['_id']}}
46 self.collection.update(
47 self.target, self.update, multi=True, safe=True)
48
49 def allmigration02_add_description_html(self):
50 """
51 Now that we can have rich descriptions via Markdown, we should
52 update all existing entries to record the rich description versions.
53 """
54 self.target = {'description_html': {'$exists': False},
55 'description': {'$exists': True}}
56
57 if not self.status:
58 for doc in self.collection.find(self.target):
59 self.update = {
60 '$set': {
61 'description_html': cleaned_markdown_conversion(
62 doc['description'])}}
63
64 class UserMigration(DocumentMigration):
65 def allmigration01_add_bio_and_url_profile(self):
66 """
67 User can elaborate profile with home page and biography
68 """
69 self.target = {'url': {'$exists': False},
70 'bio': {'$exists': False}}
71 if not self.status:
72 for doc in self.collection.find(self.target):
73 self.update = {
74 '$set': {'url': '',
75 'bio': ''}}
76 self.collection.update(
77 self.target, self.update, multi=True, safe=True)
78
79
80 MIGRATE_CLASSES = ['MediaEntry', 'User']