1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00

refactor(settings): SQLite's WAL mode is an init_command

The operations app set PRAGMA journal_mode=WAL from a connection_created
receiver because Django's SQLite OPTIONS had no init command; Django 5.1
added init_command, so the template sets it there and the receiver goes.
The mode is stored in the database file, so existing databases keep it.
This commit is contained in:
catborise 2026-11-06 11:01:51 +03:00
parent 2e287f3a7f
commit 2440c9eaf7
2 changed files with 3 additions and 14 deletions

View file

@ -1,21 +1,10 @@
from django.apps import AppConfig
from django.db.backends.signals import connection_created
def _sqlite_wal(sender, connection, **kwargs):
"""The worker and gunicorn write the same SQLite file: WAL lets readers
go on during a write. Django's SQLite OPTIONS have no init command."""
if connection.vendor == "sqlite":
with connection.cursor() as cursor:
cursor.execute("PRAGMA journal_mode=WAL")
class OperationsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "operations"
def ready(self):
connection_created.connect(_sqlite_wal)
# every installed app's jobs.py registers its kinds
from django.utils.module_loading import autodiscover_modules

View file

@ -161,9 +161,9 @@ else:
"NAME": Path.joinpath(BASE_DIR, "db.sqlite3"),
# IMMEDIATE takes the write lock when a transaction starts, so a
# check-then-write cannot interleave with another process; timeout
# is how long a writer waits for that lock (seconds). WAL is set
# per connection by the operations app.
"OPTIONS": {"transaction_mode": "IMMEDIATE", "timeout": 20},
# is how long a writer waits for that lock (seconds). WAL lets the
# worker and gunicorn read while one of them writes.
"OPTIONS": {"transaction_mode": "IMMEDIATE", "timeout": 20, "init_command": "PRAGMA journal_mode=WAL"},
# A file, not shared-cache memory: test threads and worker processes
# open the same database, and shared-cache locks ignore the timeout.
"TEST": {"NAME": os.getenv("WEBVIRTCLOUD_TEST_DB") or Path.joinpath(BASE_DIR, "test_db.sqlite3")},