Switch "sqlite_refcheck" keyword arg to "migrations" which Elrond thinks is cleaner

Also, if migrations is true, *explicitly* say that foreign key checking is off
This commit is contained in:
Christopher Allan Webber 2013-04-26 15:27:44 -05:00
parent 313b38f895
commit ea5fb2d9d4
2 changed files with 16 additions and 4 deletions

View 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)

View 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)