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