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