Switch "sqlite_refcheck" keyword arg to "migrations" which Elrond thinks is cleaner
authorChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 26 Apr 2013 20:27:44 +0000 (15:27 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 26 Apr 2013 20:27:44 +0000 (15:27 -0500)
Also, if migrations is true, *explicitly* say that foreign key checking is off

mediagoblin/db/open.py
mediagoblin/gmg_commands/dbupdate.py

index 4c0694cc27a1bfcbaa60faf69093506f1719588c..0b1679fb180b05903a9f4040b6b3226b58b7c850 100644 (file)
@@ -71,12 +71,24 @@ def _sqlite_fk_pragma_on_connect(dbapi_con, con_record):
     dbapi_con.execute('pragma foreign_keys=on')
 
 
-def setup_connection_and_db_from_config(app_config, sqlite_refcheck=True):
+def _sqlite_disable_fk_pragma_on_connect(dbapi_con, con_record):
+    """
+    Disable foreign key checking on each new sqlite connection
+    (Good for migrations!)
+    """
+    dbapi_con.execute('pragma foreign_keys=off')
+
+
+def setup_connection_and_db_from_config(app_config, migrations=False):
     engine = create_engine(app_config['sql_engine'])
 
     # Enable foreign key checking for sqlite
-    if app_config['sql_engine'].startswith('sqlite://') and sqlite_refcheck:
-        event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect)
+    if app_config['sql_engine'].startswith('sqlite://'):
+        if migrations:
+            event.listen(engine, 'connect',
+                         _sqlite_disable_fk_pragma_on_connect)
+        else:
+            event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect)
 
     # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
 
index f33d83d5002a19dd8151c8e4e4d307982dcf51dd..32700c4041bab4f385e398e587718831eb535a14 100644 (file)
@@ -114,7 +114,7 @@ def run_dbupdate(app_config, global_config):
             global_config.get('plugins', {}).keys())
 
     # Set up the database
-    db = setup_connection_and_db_from_config(app_config, sqlite_refcheck=False)
+    db = setup_connection_and_db_from_config(app_config, migrations=True)
 
     Session = sessionmaker(bind=db.engine)