mirror of
https://github.com/Casvt/MIND.git
synced 2026-04-03 03:00:22 -04:00
Implemented WAL journal mode for database
Some of the waitress code has been overwritten to make this work (at `backend.db.ThreadedTaskDispatcher`). The `handler_thread` function has been changed so that when the thread shuts down, the database connection for the thread is also closed (this _has_ to happen _in the thread that the connection is for/from_). The `shutdown` function opens and closes a database connection at the end, which triggers the checkpoint of the journal, writing everything to the db file and removing the `-wal` and `-shm` temporary db files.
This commit is contained in:
@@ -7,8 +7,11 @@ from time import time
|
||||
from typing import Union
|
||||
|
||||
from flask import g
|
||||
from waitress.task import ThreadedTaskDispatcher as OldThreadedTaskDispatcher
|
||||
|
||||
__DATABASE_VERSION__ = 5
|
||||
from backend.custom_exceptions import AccessUnauthorized, UserNotFound
|
||||
|
||||
__DATABASE_VERSION__ = 6
|
||||
|
||||
class Singleton(type):
|
||||
_instances = {}
|
||||
@@ -20,6 +23,18 @@ class Singleton(type):
|
||||
|
||||
return cls._instances[i]
|
||||
|
||||
class ThreadedTaskDispatcher(OldThreadedTaskDispatcher):
|
||||
def handler_thread(self, thread_no: int) -> None:
|
||||
super().handler_thread(thread_no)
|
||||
i = f'{DBConnection}{current_thread()}'
|
||||
if i in Singleton._instances and not Singleton._instances[i].closed:
|
||||
Singleton._instances[i].close()
|
||||
|
||||
def shutdown(self, cancel_pending: bool = True, timeout: int = 5) -> bool:
|
||||
print('Shutting down MIND...')
|
||||
super().shutdown(cancel_pending, timeout)
|
||||
DBConnection(20.0).close()
|
||||
|
||||
class DBConnection(Connection, metaclass=Singleton):
|
||||
file = ''
|
||||
|
||||
@@ -177,13 +192,22 @@ def migrate_db(current_db_version: int) -> None:
|
||||
COMMIT;
|
||||
""")
|
||||
current_db_version = 5
|
||||
|
||||
|
||||
if current_db_version == 5:
|
||||
# V5 -> V6
|
||||
from backend.users import User
|
||||
try:
|
||||
User('User1', 'Password1').delete()
|
||||
except (UserNotFound, AccessUnauthorized):
|
||||
pass
|
||||
|
||||
return
|
||||
|
||||
def setup_db() -> None:
|
||||
"""Setup the database
|
||||
"""
|
||||
cursor = get_db()
|
||||
cursor.execute("PRAGMA journal_mode = wal;")
|
||||
|
||||
cursor.executescript("""
|
||||
CREATE TABLE IF NOT EXISTS users(
|
||||
|
||||
Reference in New Issue
Block a user