summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Ripton <dripton@redhat.com>2012-10-09 16:51:24 -0400
committerDavid Ripton <dripton@redhat.com>2012-10-09 17:08:54 -0400
commit5b4b7e4083b85bd9c10792cf0adb11dc07e92333 (patch)
treeca528f33fe22319141e12aac348af2d0a5ae7cbd
parent7b618f2bf5e39c22dd60d3c3977f9b7137890493 (diff)
Tell SQLite to enforce foreign keys.
Fixes bug 1021023. This requires SQLite version 3.6.19 or greater. Older versions of SQLite have no support for enforcing foreign keys. However, SQLite ignores unknown PRAGMA commands rather than treating them as errors, so this change should be harmless with old versions of SQLite. SQLAlchemy events require SQLAlchemy 0.7 or greater. But other code in this module already uses events, so this change should impose no new SQLAlchemy version requirements. Change-Id: Ic3eeeddbe012af65fe18c884674699cf16725d8d
-rw-r--r--nova/db/sqlalchemy/session.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/nova/db/sqlalchemy/session.py b/nova/db/sqlalchemy/session.py
index 6c349e0c2..a1257e151 100644
--- a/nova/db/sqlalchemy/session.py
+++ b/nova/db/sqlalchemy/session.py
@@ -67,6 +67,14 @@ def add_regexp_listener(dbapi_con, con_record):
dbapi_con.create_function('regexp', 2, regexp)
+def enforce_foreign_keys_listener(dbapi_conn, connection_rec):
+ """Tell SQLite to enforce foreign keys.
+
+ Requires SQLite version >= 3.6.19.
+ """
+ dbapi_conn.execute("PRAGMA foreign_keys = ON")
+
+
def greenthread_yield(dbapi_con, con_record):
"""
Ensure other greenthreads get a chance to execute by forcing a context
@@ -142,6 +150,8 @@ def get_engine():
sqlalchemy.event.listen(_ENGINE, 'connect',
synchronous_switch_listener)
sqlalchemy.event.listen(_ENGINE, 'connect', add_regexp_listener)
+ sqlalchemy.event.listen(_ENGINE, 'connect',
+ enforce_foreign_keys_listener)
if (FLAGS.sql_connection_trace and
_ENGINE.dialect.dbapi.__name__ == 'MySQLdb'):