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