Merge branch 'master' into sqlmigrate
[mediagoblin.git] / mediagoblin / gmg_commands / dbupdate.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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.db.sql.open import setup_connection_and_db_from_config
18 from mediagoblin.db.sql.util import (
19 MigrationManager, assure_migrations_table_setup)
20 from mediagoblin.init import setup_global_and_app_config
21 from mediagoblin.tools.common import import_component
22
23
24 class DatabaseData(object):
25 def __init__(self, name, models, migrations):
26 self.name = name
27 self.models = models
28 self.migrations = migrations
29
30 def make_migration_manager(self, db):
31 return MigrationManager(
32 self.name, self.models, self.migrations, db)
33
34
35 def gather_database_data(media_types):
36 """
37 Gather all database data relevant to the extensions we have
38 installed so we can do migrations and table initialization.
39
40 Returns a list of DatabaseData objects.
41 """
42 managed_dbdata = []
43
44 # Add main first
45 from mediagoblin.db.sql.models import MODELS as MAIN_MODELS
46 from mediagoblin.db.sql.migrations import MIGRATIONS as MAIN_MIGRATIONS
47
48 managed_dbdata.append(
49 DatabaseData(
50 '__main__', MAIN_MODELS, MAIN_MIGRATIONS))
51
52 # Then get all registered media managers (eventually, plugins)
53 for media_type in media_types:
54 models = import_component('%s.models:MODELS' % media_type)
55 migrations = import_component('%s.migrations:MIGRATIONS' % media_type)
56 managed_dbdata.append(
57 DatabaseData(media_type, models, migrations))
58
59 return managed_dbdata
60
61
62 def dbupdate(args):
63 """
64 Initialize or migrate the database as specified by the config file.
65
66 Will also initialize or migrate all extensions (media types, and
67 in the future, plugins)
68 """
69 globa_config, app_config = setup_global_and_app_config(args.conf_file)
70
71 # Gather information from all media managers / projects
72 dbdatas = gather_database_data(app_config['media_types'])
73
74 # Set up the database
75 connection, db = setup_connection_and_db_from_config(app_config)
76
77 # If migrations table not made, make it!
78 assure_migrations_table_setup(db)
79
80 # Setup media managers for all dbdata, run init/migrate and print info
81 # For each component, create/migrate tables
82 for dbdata in dbdatas:
83 migration_manager = dbdata.make_migration_manager(db)
84 migration_manager.init_or_migrate()