diff --git a/piccolo/engine/sqlite.py b/piccolo/engine/sqlite.py index 2b1840c58..cf5a72d4a 100644 --- a/piccolo/engine/sqlite.py +++ b/piccolo/engine/sqlite.py @@ -590,6 +590,7 @@ def __init__( path: str = "piccolo.sqlite", log_queries: bool = False, log_responses: bool = False, + enable_wal_mode: bool = False, **connection_kwargs, ) -> None: """ @@ -602,6 +603,9 @@ def __init__( :param log_responses: If ``True``, the raw response from each query is printed out. Useful for debugging. + :param enable_wal_mode: + If ``True``, set the journal mode to write ahead logging (WAL) + i.e. execute `PRAGMA journal_mode = WAL` when returning the db connection. :param connection_kwargs: These are passed directly to the database adapter. We recommend setting ``timeout`` if you expect your application to process a @@ -618,6 +622,7 @@ def __init__( self.log_queries = log_queries self.log_responses = log_responses + self.enable_wal_mode = enable_wal_mode self.connection_kwargs = { **default_connection_kwargs, **connection_kwargs, @@ -696,6 +701,10 @@ async def get_connection(self) -> Connection: connection = await aiosqlite.connect(**self.connection_kwargs) connection.row_factory = dict_factory # type: ignore await connection.execute("PRAGMA foreign_keys = 1") + + if self.enable_wal_mode: + await connection.execute("PRAGMA journal_mode = WAL") + return connection ########################################################################### @@ -724,6 +733,9 @@ async def _run_in_new_connection( args = [] async with aiosqlite.connect(**self.connection_kwargs) as connection: await connection.execute("PRAGMA foreign_keys = 1") + + if self.enable_wal_mode: + await connection.execute("PRAGMA journal_mode = WAL") connection.row_factory = dict_factory # type: ignore async with connection.execute(query, args) as cursor: @@ -753,6 +765,9 @@ async def _run_in_existing_connection( args = [] await connection.execute("PRAGMA foreign_keys = 1") + if self.enable_wal_mode: + await connection.execute("PRAGMA journal_mode = WAL") + connection.row_factory = dict_factory async with connection.execute(query, args) as cursor: response = await cursor.fetchall()