Merge branch 'master' into test_submission_views_365
[mediagoblin.git] / mediagoblin / gmg_commands / migrate.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
18 from mediagoblin.db import migrations
19 from mediagoblin.db import util as db_util
20 from mediagoblin.gmg_commands import util as commands_util
21
22
23 def migrate_parser_setup(subparser):
24 subparser.add_argument(
25 '-cf', '--conf_file', default='mediagoblin.ini',
26 help="Config file used to set up environment")
27
28
29 def migrate(args):
30 mgoblin_app = commands_util.setup_app(args)
31
32 # Clear old indexes
33 print "== Clearing old indexes... =="
34 removed_indexes = db_util.remove_deprecated_indexes(mgoblin_app.db)
35
36 for collection, index_name in removed_indexes:
37 print "Removed index '%s' in collection '%s'" % (
38 index_name, collection)
39
40 # Migrate
41 print "== Applying migrations... =="
42 for model_name in migrations.MIGRATE_CLASSES:
43 model = getattr(mgoblin_app.db, model_name)
44
45 if not hasattr(model, 'migration_handler') or not model.collection:
46 continue
47
48 migration = model.migration_handler(model)
49 migration.migrate_all(collection=model.collection)
50
51 # Add new indexes
52 print "== Adding new indexes... =="
53 new_indexes = db_util.add_new_indexes(mgoblin_app.db)
54
55 for collection, index_name in new_indexes:
56 print "Added index '%s' to collection '%s'" % (
57 index_name, collection)