1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from sqlalchemy
.orm
import sessionmaker
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
26 def dbupdate_parse_setup(subparser
):
30 class DatabaseData(object):
31 def __init__(self
, name
, models
, migrations
):
34 self
.migrations
= migrations
36 def make_migration_manager(self
, session
):
37 return MigrationManager(
38 self
.name
, self
.models
, self
.migrations
, session
)
41 def gather_database_data(media_types
):
43 Gather all database data relevant to the extensions we have
44 installed so we can do migrations and table initialization.
46 Returns a list of DatabaseData objects.
51 from mediagoblin
.db
.sql
.models
import MODELS
as MAIN_MODELS
52 from mediagoblin
.db
.sql
.migrations
import MIGRATIONS
as MAIN_MIGRATIONS
54 managed_dbdata
.append(
56 '__main__', MAIN_MODELS
, MAIN_MIGRATIONS
))
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
))
70 Initialize or migrate the database as specified by the config file.
72 Will also initialize or migrate all extensions (media types, and
73 in the future, plugins)
75 globa_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
77 # Gather information from all media managers / projects
78 dbdatas
= gather_database_data(app_config
['media_types'])
81 connection
, db
= setup_connection_and_db_from_config(app_config
)
83 Session
= sessionmaker(bind
=db
.engine
)
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()