Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ def __init__(
path: str = "piccolo.sqlite",
log_queries: bool = False,
log_responses: bool = False,
enable_wal_mode: bool = False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of changing this to:

journal_mode: Literal['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'] | None = None

It provides a bit more flexibility if people want alternative journal modes.

**connection_kwargs,
) -> None:
"""
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

###########################################################################
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down