Merge branch 'master' into derek-moore-bug405_email_notifications_for_comments
[mediagoblin.git] / mediagoblin / gmg_commands / migrate.py
CommitLineData
757f37a5 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 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
ba04c887 17import sys
757f37a5 18
69067258 19from mediagoblin.db.mongo import util as db_util
9e554311 20from mediagoblin.db.mongo.open import setup_connection_and_db_from_config
5568a014 21from mediagoblin.init import setup_global_and_app_config
757f37a5 22
90e342f9 23# This MUST be imported so as to set up the appropriate migrations!
69067258 24from mediagoblin.db.mongo import migrations
90e342f9 25
757f37a5
CAW
26
27def migrate_parser_setup(subparser):
15ac1458 28 pass
757f37a5 29
ecf51802 30
ba04c887
CAW
31def _print_started_migration(migration_number, migration_func):
32 sys.stdout.write(
33 "Running migration %s, '%s'... " % (
34 migration_number, migration_func.func_name))
ecf51802
CAW
35 sys.stdout.flush()
36
ba04c887
CAW
37
38def _print_finished_migration(migration_number, migration_func):
ecf51802
CAW
39 sys.stdout.write("done.\n")
40 sys.stdout.flush()
ba04c887 41
757f37a5
CAW
42
43def migrate(args):
9e554311
CAW
44 run_migrate(args.conf_file)
45
46
47def run_migrate(conf_file):
48 global_config, app_config = setup_global_and_app_config(conf_file)
49
ba04c887 50 connection, db = setup_connection_and_db_from_config(
5568a014 51 app_config, use_pymongo=True)
ba04c887 52 migration_manager = db_util.MigrationManager(db)
757f37a5 53
8db03585
CAW
54 # Clear old indexes
55 print "== Clearing old indexes... =="
ba04c887 56 removed_indexes = db_util.remove_deprecated_indexes(db)
8db03585
CAW
57
58 for collection, index_name in removed_indexes:
59 print "Removed index '%s' in collection '%s'" % (
60 index_name, collection)
243c3843 61
8db03585 62 # Migrate
ba04c887
CAW
63 print "\n== Applying migrations... =="
64 migration_manager.migrate_new(
65 pre_callback=_print_started_migration,
66 post_callback=_print_finished_migration)
243c3843 67
8db03585 68 # Add new indexes
ba04c887
CAW
69 print "\n== Adding new indexes... =="
70 new_indexes = db_util.add_new_indexes(db)
8db03585
CAW
71
72 for collection, index_name in new_indexes:
73 print "Added index '%s' to collection '%s'" % (
74 index_name, collection)