From 8458182990a2ed040daa7b7321779c14a0bb9559 Mon Sep 17 00:00:00 2001 From: krek0 Date: Fri, 24 Apr 2026 23:26:09 +0200 Subject: [PATCH] Enable Write-Ahead Logging (WAL) for SQLite to improve performance when using it. --- photo21/settings.py | 3 +++ photologue/apps.py | 17 +++++++++++++++++ photologue/models.py | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/photo21/settings.py b/photo21/settings.py index 88390ae..fb36c54 100644 --- a/photo21/settings.py +++ b/photo21/settings.py @@ -129,6 +129,9 @@ DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + "OPTIONS": { + "timeout": 10, + }, } } diff --git a/photologue/apps.py b/photologue/apps.py index a446591..10f0a9b 100644 --- a/photologue/apps.py +++ b/photologue/apps.py @@ -3,8 +3,25 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.apps import AppConfig +from django.db.backends.signals import connection_created class PhotologueConfig(AppConfig): default_auto_field = "django.db.models.AutoField" name = "photologue" + + def ready(self): + from django.db import connection + + def enable_sqlite_wal(sender, connection, **kwargs): + if connection.vendor == "sqlite": + cursor = connection.cursor() + cursor.execute("PRAGMA journal_mode=WAL;") + cursor.execute("PRAGMA synchronous=OFF;") + cursor.execute("PRAGMA journal_size_limit=67108864;") + cursor.execute("PRAGMA wal_autocheckpoint=1000;") + cursor.execute("PRAGMA cache_size=-65536;") + cursor.execute("PRAGMA temp_store=MEMORY;") + cursor.execute("PRAGMA mmap_size=268435456;") + + connection_created.connect(enable_sqlite_wal) diff --git a/photologue/models.py b/photologue/models.py index 3585e8c..594ac2c 100644 --- a/photologue/models.py +++ b/photologue/models.py @@ -486,7 +486,7 @@ class ImageModel(models.Model): self._old_image.storage.delete( self._old_image.name ) # Delete (old) base image. - if self.date_taken is None or image_has_changed: + if (self.date_taken is None or image_has_changed) and self.image: # Attempt to get the date the photo was taken from the EXIF data. try: exif_date = self.exif(self.image.file).get(