Skip to content

Commit 7416776

Browse files
author
Ayush Shukla
committed
feat(database): Engine-level Write-Ahead Logging & concurrency buffers for SQLite (#477)
1 parent 8a00969 commit 7416776

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

montage/app.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ def set_mysql_session_charset_and_collation(connection, branch):
5353
return
5454

5555

56+
def set_sqlite_pragma(dbapi_connection, connection_record):
57+
# @ayushshukla1807: Intercepting the raw dialect connection natively here to enforce WAL mode.
58+
# This physically overrides the default DELETE journal locks and completely eliminates
59+
# the 'Database is Locked' queue drops during our concurrent testing loops (#477).
60+
cursor = dbapi_connection.cursor()
61+
cursor.execute("PRAGMA journal_mode=WAL")
62+
cursor.execute("PRAGMA synchronous=NORMAL")
63+
cursor.execute("PRAGMA busy_timeout=5000")
64+
cursor.close()
65+
66+
5667
def create_app(env_name='prod', config=None):
5768
# rendering is handled by MessageMiddleware
5869
ui_routes = (PUBLIC_UI_ROUTES + JUROR_UI_ROUTES
@@ -68,7 +79,13 @@ def create_app(env_name='prod', config=None):
6879
config = load_env_config(env_name=env_name)
6980
print('== loaded config file: %s' % (config['__file__'],))
7081

71-
engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60)
82+
db_url = config.get('db_url', DEFAULT_DB_URL)
83+
engine = create_engine(db_url, pool_recycle=60)
84+
85+
if 'sqlite' in db_url:
86+
event.listen(engine, 'connect', set_sqlite_pragma)
87+
elif 'mysql' in db_url:
88+
event.listen(engine, 'engine_connect', set_mysql_session_charset_and_collation)
7289
session_type = sessionmaker()
7390
session_type.configure(bind=engine)
7491
tmp_rdb_session = session_type()
@@ -122,6 +139,8 @@ def get_engine():
122139

123140
if 'mysql' in db_url:
124141
event.listen(engine, 'engine_connect', set_mysql_session_charset_and_collation)
142+
elif 'sqlite' in db_url:
143+
event.listen(engine, 'connect', set_sqlite_pragma)
125144

126145
return engine
127146

0 commit comments

Comments
 (0)