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 | ||
f46e2a4d JW |
17 | import logging |
18 | ||
3ea1cf36 CAW |
19 | from sqlalchemy.orm import sessionmaker |
20 | ||
6eddc3b7 | 21 | from mediagoblin.db.open import setup_connection_and_db_from_config |
c130e3ee | 22 | from mediagoblin.db.migration_tools import MigrationManager |
70b44584 CAW |
23 | from mediagoblin.init import setup_global_and_app_config |
24 | from mediagoblin.tools.common import import_component | |
25 | ||
f46e2a4d JW |
26 | _log = logging.getLogger(__name__) |
27 | logging.basicConfig() | |
28 | _log.setLevel(logging.DEBUG) | |
70b44584 | 29 | |
3ea1cf36 CAW |
30 | def dbupdate_parse_setup(subparser): |
31 | pass | |
32 | ||
33 | ||
70b44584 CAW |
34 | class DatabaseData(object): |
35 | def __init__(self, name, models, migrations): | |
36 | self.name = name | |
37 | self.models = models | |
38 | self.migrations = migrations | |
39 | ||
3ea1cf36 | 40 | def make_migration_manager(self, session): |
70b44584 | 41 | return MigrationManager( |
3ea1cf36 | 42 | self.name, self.models, self.migrations, session) |
70b44584 CAW |
43 | |
44 | ||
f46e2a4d | 45 | def gather_database_data(media_types, plugins): |
70b44584 CAW |
46 | """ |
47 | Gather all database data relevant to the extensions we have | |
48 | installed so we can do migrations and table initialization. | |
49 | ||
50 | Returns a list of DatabaseData objects. | |
51 | """ | |
52 | managed_dbdata = [] | |
53 | ||
54 | # Add main first | |
b0c8328e | 55 | from mediagoblin.db.models import MODELS as MAIN_MODELS |
95369884 | 56 | from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS |
70b44584 CAW |
57 | |
58 | managed_dbdata.append( | |
59 | DatabaseData( | |
a00ac320 | 60 | u'__main__', MAIN_MODELS, MAIN_MIGRATIONS)) |
70b44584 CAW |
61 | |
62 | # Then get all registered media managers (eventually, plugins) | |
63 | for media_type in media_types: | |
64 | models = import_component('%s.models:MODELS' % media_type) | |
65 | migrations = import_component('%s.migrations:MIGRATIONS' % media_type) | |
66 | managed_dbdata.append( | |
67 | DatabaseData(media_type, models, migrations)) | |
68 | ||
f46e2a4d JW |
69 | for plugin in plugins: |
70 | try: | |
71 | models = import_component('{0}.models:MODELS'.format(plugin)) | |
72 | except ImportError as exc: | |
73 | _log.debug('No models found for {0}: {1}'.format( | |
74 | plugin, | |
75 | exc)) | |
76 | ||
77 | models = [] | |
78 | except AttributeError as exc: | |
79 | _log.warning('Could not find MODELS in {0}.models, have you \ | |
80 | forgotten to add it? ({1})'.format(plugin, exc)) | |
0ae38290 | 81 | models = [] |
f46e2a4d JW |
82 | |
83 | try: | |
84 | migrations = import_component('{0}.migrations:MIGRATIONS'.format( | |
85 | plugin)) | |
86 | except ImportError as exc: | |
87 | _log.debug('No migrations found for {0}: {1}'.format( | |
88 | plugin, | |
89 | exc)) | |
90 | ||
91 | migrations = {} | |
92 | except AttributeError as exc: | |
93 | _log.debug('Cloud not find MIGRATIONS in {0}.migrations, have you \ | |
94 | forgotten to add it? ({1})'.format(plugin, exc)) | |
0ae38290 | 95 | migrations = {} |
f46e2a4d JW |
96 | |
97 | if models: | |
98 | managed_dbdata.append( | |
99 | DatabaseData(plugin, models, migrations)) | |
100 | ||
101 | ||
70b44584 CAW |
102 | return managed_dbdata |
103 | ||
104 | ||
f46e2a4d | 105 | def run_dbupdate(app_config, global_config): |
70b44584 CAW |
106 | """ |
107 | Initialize or migrate the database as specified by the config file. | |
108 | ||
109 | Will also initialize or migrate all extensions (media types, and | |
110 | in the future, plugins) | |
111 | """ | |
70b44584 | 112 | |
4a698535 EL |
113 | # Set up the database |
114 | db = setup_connection_and_db_from_config(app_config, migrations=True) | |
115 | #Run the migrations | |
116 | run_all_migrations(db, app_config, global_config) | |
117 | ||
118 | ||
119 | def run_all_migrations(db, app_config, global_config): | |
120 | """ | |
6db23bd9 EL |
121 | Initializes or migrates a database that already has a |
122 | connection setup and also initializes or migrates all | |
123 | extensions based on the config files. | |
124 | ||
125 | It can be used to initialize an in-memory database for | |
126 | testing. | |
4a698535 | 127 | """ |
70b44584 | 128 | # Gather information from all media managers / projects |
f46e2a4d JW |
129 | dbdatas = gather_database_data( |
130 | app_config['media_types'], | |
30520c92 | 131 | global_config.get('plugins', {}).keys()) |
70b44584 | 132 | |
3ea1cf36 | 133 | Session = sessionmaker(bind=db.engine) |
70b44584 CAW |
134 | |
135 | # Setup media managers for all dbdata, run init/migrate and print info | |
136 | # For each component, create/migrate tables | |
137 | for dbdata in dbdatas: | |
3ea1cf36 | 138 | migration_manager = dbdata.make_migration_manager(Session()) |
70b44584 | 139 | migration_manager.init_or_migrate() |
d693f6bd CAW |
140 | |
141 | ||
142 | def dbupdate(args): | |
143 | global_config, app_config = setup_global_and_app_config(args.conf_file) | |
f46e2a4d | 144 | run_dbupdate(app_config, global_config) |