Commit | Line | Data |
---|---|---|
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 |
17 | from sqlalchemy.orm import sessionmaker |
18 | ||
70b44584 CAW |
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 | ||
3ea1cf36 CAW |
26 | def dbupdate_parse_setup(subparser): |
27 | pass | |
28 | ||
29 | ||
70b44584 CAW |
30 | class DatabaseData(object): |
31 | def __init__(self, name, models, migrations): | |
32 | self.name = name | |
33 | self.models = models | |
34 | self.migrations = migrations | |
35 | ||
3ea1cf36 | 36 | def make_migration_manager(self, session): |
70b44584 | 37 | return MigrationManager( |
3ea1cf36 | 38 | self.name, self.models, self.migrations, session) |
70b44584 CAW |
39 | |
40 | ||
3f2c6f96 | 41 | def gather_database_data(media_types): |
70b44584 CAW |
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 | ||
3ea1cf36 | 83 | Session = sessionmaker(bind=db.engine) |
70b44584 CAW |
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: | |
3ea1cf36 | 88 | migration_manager = dbdata.make_migration_manager(Session()) |
70b44584 | 89 | migration_manager.init_or_migrate() |