Skip to content

Commit 9b05061

Browse files
authored
Check for WAL mode status (#2426)
1 parent bcabc43 commit 9b05061

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

chatterbot/storage/sql_storage.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,30 @@ def __init__(self, **kwargs):
4141

4242
@event.listens_for(Engine, 'connect')
4343
def set_sqlite_pragma(dbapi_connection, connection_record):
44-
dbapi_connection.execute('PRAGMA journal_mode=WAL')
45-
dbapi_connection.execute('PRAGMA synchronous=NORMAL')
44+
"""
45+
Set SQLite PRAGMA settings.
46+
47+
This function is called when a new connection is created.
48+
WAL mode must be set outside of a transaction because it cannot
49+
change into wal mode from within a transaction.
50+
"""
51+
cursor = dbapi_connection.cursor()
52+
53+
# Check current journal mode
54+
cursor.execute('PRAGMA journal_mode')
55+
current_mode = cursor.fetchone()[0]
56+
57+
# Only change if not already in WAL mode
58+
if current_mode.lower() != 'wal':
59+
# Set isolation_level to None to execute outside transaction
60+
old_isolation = dbapi_connection.isolation_level
61+
dbapi_connection.isolation_level = None
62+
cursor.execute('PRAGMA journal_mode=WAL')
63+
dbapi_connection.isolation_level = old_isolation
64+
65+
# Set synchronous mode (can be done normally)
66+
cursor.execute('PRAGMA synchronous=NORMAL')
67+
cursor.close()
4668

4769
if not inspect(self.engine).has_table('statement'):
4870
self.create_database()

0 commit comments

Comments
 (0)