From 19241803f2a7aea28d405786a47c54d8f75951ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Fri, 15 Mar 2024 14:16:06 +0100 Subject: [PATCH] Use WAL --- feedvault/apps.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/feedvault/apps.py b/feedvault/apps.py index 249f139..0ceeec7 100644 --- a/feedvault/apps.py +++ b/feedvault/apps.py @@ -1,4 +1,12 @@ +from typing import TYPE_CHECKING + from django.apps import AppConfig +from django.db.backends.signals import connection_created +from django.db.backends.sqlite3.base import DatabaseWrapper +from django.dispatch import receiver + +if TYPE_CHECKING: + from django.db.backends.utils import CursorWrapper class FeedVaultConfig(AppConfig): @@ -6,3 +14,23 @@ class FeedVaultConfig(AppConfig): default_auto_field: str = "django.db.models.BigAutoField" name: str = "feedvault" + + +@receiver(signal=connection_created) +def activate_wal(sender: DatabaseWrapper, connection: DatabaseWrapper, **kwargs: dict) -> None: # noqa: ARG001 + """Activate Write-Ahead Logging (WAL) for SQLite databases. + + WAL mode allows for concurrent reads and writes to the database. + + We do this in apps.py because it is the earliest point in the + application startup process where we can be sure that the database + connection has been created. + + Args: + sender: The sender of the signal. + connection: The connection to the database. + kwargs: Additional keyword arguments. + """ + if connection.vendor == "sqlite": + cursor: CursorWrapper = connection.cursor() + cursor.execute(sql="PRAGMA journal_mode=WAL;")