File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments