| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 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 | |
| 17 | |
| 18 | from sqlalchemy import create_engine, event |
| 19 | import logging |
| 20 | |
| 21 | import six |
| 22 | |
| 23 | from mediagoblin.db.base import Base, Session |
| 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 | |
| 33 | for k, v in six.iteritems(Base._decl_class_registry): |
| 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): |
| 55 | import mediagoblin.db.models |
| 56 | |
| 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 | |
| 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 | |
| 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): |
| 81 | engine = create_engine(app_config['sql_engine']) |
| 82 | |
| 83 | # Enable foreign key checking for sqlite |
| 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) |
| 90 | |
| 91 | # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) |
| 92 | |
| 93 | Session.configure(bind=engine) |
| 94 | |
| 95 | return DatabaseMaster(engine) |
| 96 | |
| 97 | |
| 98 | def check_db_migrations_current(db): |
| 99 | pass |