A couple of tests related to migrations_to_run()
[mediagoblin.git] / mediagoblin / gmg_commands / migrate.py
CommitLineData
757f37a5
CAW
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
ba04c887 17import sys
757f37a5
CAW
18
19from mediagoblin.db import migrations
8db03585 20from mediagoblin.db import util as db_util
ba04c887
CAW
21from mediagoblin.db.open import setup_connection_and_db_from_config
22from mediagoblin.init.config import read_mediagoblin_config
757f37a5
CAW
23
24
25def migrate_parser_setup(subparser):
26 subparser.add_argument(
27 '-cf', '--conf_file', default='mediagoblin.ini',
28 help="Config file used to set up environment")
757f37a5 29
ba04c887
CAW
30def _print_started_migration(migration_number, migration_func):
31 sys.stdout.write(
32 "Running migration %s, '%s'... " % (
33 migration_number, migration_func.func_name))
34
35def _print_finished_migration(migration_number, migration_func):
36 print "done."
37
757f37a5
CAW
38
39def migrate(args):
ba04c887
CAW
40 config, validation_result = read_mediagoblin_config(args.conf_file)
41 connection, db = setup_connection_and_db_from_config(
42 config['mediagoblin'], use_pymongo=True)
43 migration_manager = db_util.MigrationManager(db)
757f37a5 44
8db03585
CAW
45 # Clear old indexes
46 print "== Clearing old indexes... =="
ba04c887 47 removed_indexes = db_util.remove_deprecated_indexes(db)
8db03585
CAW
48
49 for collection, index_name in removed_indexes:
50 print "Removed index '%s' in collection '%s'" % (
51 index_name, collection)
52
53 # Migrate
ba04c887
CAW
54 print "\n== Applying migrations... =="
55 migration_manager.migrate_new(
56 pre_callback=_print_started_migration,
57 post_callback=_print_finished_migration)
757f37a5 58
8db03585 59 # Add new indexes
ba04c887
CAW
60 print "\n== Adding new indexes... =="
61 new_indexes = db_util.add_new_indexes(db)
8db03585
CAW
62
63 for collection, index_name in new_indexes:
64 print "Added index '%s' to collection '%s'" % (
65 index_name, collection)