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