From 877ffc07412c7c9512114c3cd5a9ec781c1aefde Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Wed, 27 Mar 2024 10:15:32 -0400 Subject: [PATCH] Tune SQLite settings for using SQLite in production for small sites See: https://fractaledmind.github.io/2023/09/07/enhancing-rails-sqlite-fine-tuning/ --- .gitignore | 2 ++ brutaldon/__init__.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/.gitignore b/.gitignore index 73ef8ca..a8cf95d 100644 --- a/.gitignore +++ b/.gitignore @@ -114,3 +114,5 @@ node_modules .vscode package-lock.json yarn.lock +/db.sqlite3-* + diff --git a/brutaldon/__init__.py b/brutaldon/__init__.py index e69de29..b637806 100644 --- a/brutaldon/__init__.py +++ b/brutaldon/__init__.py @@ -0,0 +1,13 @@ +from django.db.backends.signals import connection_created +def configure_sqlite_web_settings(sender, connection, **kwargs): + """Configure sqlite for web application use.""" + if connection.vendor == 'sqlite': + cursor = connection.cursor() + cursor.execute('PRAGMA journal_mode = WAL;') + cursor.execute('PRAGMA synchronous = NORMAL;') + cursor.execute('PRAGMA journal_size_limit = 67108864; -- 64 megabytes') + cursor.execute('PRAGMA mmap_size = 134217728; -- 128 megabytes') + cursor.execute('PRAGMA cache_size = 2000; -- 2000 pages') + cursor.execute('PRAGMA busy_timeout = 5000;') + +connection_created.connect(configure_sqlite_web_settings)