Commit | Line | Data |
---|---|---|
a67fec81 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
a67fec81 E |
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 | ||
6eddc3b7 | 17 | |
afbfd405 | 18 | from sqlalchemy import create_engine, event |
6eddc3b7 SS |
19 | import logging |
20 | ||
cf3b5926 BP |
21 | import six |
22 | ||
39dc3bf8 | 23 | from mediagoblin.db.base import Base, Session |
6eddc3b7 SS |
24 | from mediagoblin import mg_globals |
25 | ||
26 | _log = logging.getLogger(__name__) | |
27 | ||
28 | ||
29 | class DatabaseMaster(object): | |
30 | def __init__(self, engine): | |
31 | self.engine = engine | |
32 | ||
cf3b5926 | 33 | for k, v in six.iteritems(Base._decl_class_registry): |
6eddc3b7 SS |
34 | setattr(self, k, v) |
35 | ||
36 | def commit(self): | |
37 | Session.commit() | |
38 | ||
39 | def save(self, obj): | |
40 | Session.add(obj) | |
41 | Session.flush() | |
42 | ||
43 | def check_session_clean(self): | |
44 | for dummy in Session(): | |
45 | _log.warn("STRANGE: There are elements in the sql session. " | |
46 | "Please report this and help us track this down.") | |
47 | break | |
48 | ||
49 | def reset_after_request(self): | |
50 | Session.rollback() | |
51 | Session.remove() | |
52 | ||
53 | ||
54 | def load_models(app_config): | |
b0c8328e | 55 | import mediagoblin.db.models |
6eddc3b7 | 56 | |
6eddc3b7 SS |
57 | for plugin in mg_globals.global_config.get('plugins', {}).keys(): |
58 | _log.debug("Loading %s.models", plugin) | |
59 | try: | |
60 | __import__(plugin + ".models") | |
61 | except ImportError as exc: | |
62 | _log.debug("Could not load {0}.models: {1}".format( | |
63 | plugin, | |
64 | exc)) | |
65 | ||
66 | ||
afbfd405 E |
67 | def _sqlite_fk_pragma_on_connect(dbapi_con, con_record): |
68 | """Enable foreign key checking on each new sqlite connection""" | |
69 | dbapi_con.execute('pragma foreign_keys=on') | |
70 | ||
71 | ||
ea5fb2d9 CAW |
72 | def _sqlite_disable_fk_pragma_on_connect(dbapi_con, con_record): |
73 | """ | |
74 | Disable foreign key checking on each new sqlite connection | |
75 | (Good for migrations!) | |
76 | """ | |
77 | dbapi_con.execute('pragma foreign_keys=off') | |
78 | ||
79 | ||
80 | def setup_connection_and_db_from_config(app_config, migrations=False): | |
6eddc3b7 | 81 | engine = create_engine(app_config['sql_engine']) |
afbfd405 E |
82 | |
83 | # Enable foreign key checking for sqlite | |
ea5fb2d9 CAW |
84 | if app_config['sql_engine'].startswith('sqlite://'): |
85 | if migrations: | |
86 | event.listen(engine, 'connect', | |
87 | _sqlite_disable_fk_pragma_on_connect) | |
88 | else: | |
89 | event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect) | |
afbfd405 | 90 | |
6eddc3b7 | 91 | # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) |
afbfd405 | 92 | |
6eddc3b7 SS |
93 | Session.configure(bind=engine) |
94 | ||
95 | return DatabaseMaster(engine) | |
96 | ||
97 | ||
98 | def check_db_migrations_current(db): | |
99 | pass |