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:
parent
313b38f895
commit
ea5fb2d9d4
@ -71,12 +71,24 @@ def _sqlite_fk_pragma_on_connect(dbapi_con, con_record):
|
|||||||
dbapi_con.execute('pragma foreign_keys=on')
|
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'])
|
engine = create_engine(app_config['sql_engine'])
|
||||||
|
|
||||||
# Enable foreign key checking for sqlite
|
# Enable foreign key checking for sqlite
|
||||||
if app_config['sql_engine'].startswith('sqlite://') and sqlite_refcheck:
|
if app_config['sql_engine'].startswith('sqlite://'):
|
||||||
event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect)
|
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)
|
# logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ def run_dbupdate(app_config, global_config):
|
|||||||
global_config.get('plugins', {}).keys())
|
global_config.get('plugins', {}).keys())
|
||||||
|
|
||||||
# Set up the database
|
# 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)
|
Session = sessionmaker(bind=db.engine)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user