diff --git a/bmds_ui/analysis/apps.py b/bmds_ui/analysis/apps.py index 9aaebd51..fa9c834c 100644 --- a/bmds_ui/analysis/apps.py +++ b/bmds_ui/analysis/apps.py @@ -3,6 +3,3 @@ class Config(AppConfig): name = "bmds_ui.analysis" - - def ready(self): - from .signals import init_command # noqa: F401 diff --git a/bmds_ui/analysis/signals.py b/bmds_ui/analysis/signals.py deleted file mode 100644 index f4334262..00000000 --- a/bmds_ui/analysis/signals.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.db.backends.signals import connection_created -from django.dispatch import receiver - - -@receiver(connection_created) -def init_command(sender, connection, **kwargs) -> None: - # https://code.djangoproject.com/ticket/24018 - command = connection.settings_dict.get("STARTUP_OPTIONS", {}).get("init_command") - if connection.vendor == "sqlite" and command: - cursor = connection.cursor() - cursor.execute(command) diff --git a/bmds_ui/desktop/sqlite3/__init__.py b/bmds_ui/desktop/sqlite3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bmds_ui/desktop/sqlite3/base.py b/bmds_ui/desktop/sqlite3/base.py new file mode 100644 index 00000000..6de3d7a9 --- /dev/null +++ b/bmds_ui/desktop/sqlite3/base.py @@ -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 diff --git a/bmds_ui/main/settings/desktop.py b/bmds_ui/main/settings/desktop.py index e1bc9101..f0a0cf28 100644 --- a/bmds_ui/main/settings/desktop.py +++ b/bmds_ui/main/settings/desktop.py @@ -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"), } }