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