Skip to content

Commit

Permalink
configure sqlite w/ best settings (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiromatron authored Jul 23, 2024
1 parent 478d6cd commit 7bbdd49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
3 changes: 0 additions & 3 deletions bmds_ui/analysis/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

class Config(AppConfig):
name = "bmds_ui.analysis"

def ready(self):
from .signals import init_command # noqa: F401
11 changes: 0 additions & 11 deletions bmds_ui/analysis/signals.py

This file was deleted.

Empty file.
31 changes: 31 additions & 0 deletions bmds_ui/desktop/sqlite3/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from sqlite3 import dbapi2 as Database

from django.db.backends.sqlite3 import base
from django.db.backends.sqlite3._functions import register as register_functions
from django.utils.asyncio import async_unsafe


class DatabaseWrapper(base.DatabaseWrapper):
# TODO - remove w/ django 5.1 release
# adapted from https://blog.pecar.me/sqlite-django-config
# https://gcollazo.com/optimal-sqlite-settings-for-django/
def _start_transaction_under_autocommit(self):
# Acquire a write lock immediately for transactions
self.cursor().execute("BEGIN IMMEDIATE")

@async_unsafe
def get_new_connection(self, conn_params):
conn = Database.connect(**conn_params)
register_functions(conn)

conn.execute("PRAGMA foreign_keys=ON")
conn.execute("PRAGMA legacy_alter_table = OFF")
conn.execute("PRAGMA journal_mode = WAL")
conn.execute("PRAGMA synchronous = NORMAL")
conn.execute("PRAGMA busy_timeout = 5000")
conn.execute("PRAGMA temp_store = MEMORY")
conn.execute("PRAGMA mmap_size = 134217728")
conn.execute("PRAGMA journal_size_limit = 67108864")
conn.execute("PRAGMA cache_size = 2000")

return conn
5 changes: 2 additions & 3 deletions bmds_ui/main/settings/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.getenv("BMDS_DB", APP_HOME / "pybmds.sqlite3"),
"STARTUP_OPTIONS": {"init_command": "PRAGMA journal_mode=wal;"},
"ENGINE": "bmds_ui.desktop.sqlite3",
"NAME": os.getenv("BMDS_DB", APP_HOME / "bmds-desktop.db"),
}
}

Expand Down

0 comments on commit 7bbdd49

Please sign in to comment.