From: Elrond Date: Fri, 18 Jan 2013 17:42:52 +0000 (+0100) Subject: Enable foreign key checking on sqlite. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=afbfd40578ca93babe191bb0edef360822c91a27;p=mediagoblin.git Enable foreign key checking on sqlite. sqlite normally does not check foreign key referential integrity. But it can do so. So let's use it. Better safe than sorry. --- diff --git a/mediagoblin/db/open.py b/mediagoblin/db/open.py index d976acd8..5fd5ed03 100644 --- a/mediagoblin/db/open.py +++ b/mediagoblin/db/open.py @@ -15,7 +15,7 @@ # along with this program. If not, see . -from sqlalchemy import create_engine +from sqlalchemy import create_engine, event import logging from mediagoblin.db.base import Base, Session @@ -66,9 +66,20 @@ def load_models(app_config): exc)) +def _sqlite_fk_pragma_on_connect(dbapi_con, con_record): + """Enable foreign key checking on each new sqlite connection""" + dbapi_con.execute('pragma foreign_keys=on') + + def setup_connection_and_db_from_config(app_config): engine = create_engine(app_config['sql_engine']) + + # Enable foreign key checking for sqlite + if app_config['sql_engine'].startswith('sqlite://'): + event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect) + # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) + Session.configure(bind=engine) return DatabaseMaster(engine)