Bradley Kirton's Blog

Published on Dec. 4, 2023

Go home

Enable sqlite pragmas in django

The snippet below enables the journalmode, synchronous and busytimeout pragmas.

import typing as t
from django.apps import AppConfig
from django.db.backends.signals import connection_created

class CoreConfig(AppConfig):
    """Core application config."""

    name = "core"

    def ready(self) -> None:
        def enable_sqlite_pragmas(sender: t.Any, connection: t.Any, **kwargs: t.Any) -> None:
            """Enable the necessary sqlite pragmas."""
            cursor = connection.cursor()
            cursor.execute("PRAGMA journal_mode = WAL;")
            cursor.execute("PRAGMA synchronous = NORMAL;")
            cursor.execute("PRAGMA busy_timeout = 5000;")