1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 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
24 class DatabaseData(object):
25 def __init__(self
, name
, models
, migrations
):
28 self
.migrations
= migrations
30 def make_migration_manager(self
, db
):
31 return MigrationManager(
32 self
.name
, self
.models
, self
.migrations
, db
)
35 def gather_database_data(media_types
):
37 Gather all database data relevant to the extensions we have
38 installed so we can do migrations and table initialization.
40 Returns a list of DatabaseData objects.
45 from mediagoblin
.db
.sql
.models
import MODELS
as MAIN_MODELS
46 from mediagoblin
.db
.sql
.migrations
import MIGRATIONS
as MAIN_MIGRATIONS
48 managed_dbdata
.append(
50 '__main__', MAIN_MODELS
, MAIN_MIGRATIONS
))
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
))
64 Initialize or migrate the database as specified by the config file.
66 Will also initialize or migrate all extensions (media types, and
67 in the future, plugins)
69 globa_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
71 # Gather information from all media managers / projects
72 dbdatas
= gather_database_data(app_config
['media_types'])
75 connection
, db
= setup_connection_and_db_from_config(app_config
)
77 # If migrations table not made, make it!
78 assure_migrations_table_setup(db
)
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()