mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
388d36b839
Fixes issues where a db-accessing service wants to call db-accessing methods with locks.
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
import sqlite3
|
|
import threading
|
|
from logging import Logger
|
|
|
|
from invokeai.app.services.config import InvokeAIAppConfig
|
|
|
|
sqlite_memory = ":memory:"
|
|
|
|
|
|
class SqliteDatabase:
|
|
conn: sqlite3.Connection
|
|
lock: threading.RLock
|
|
_logger: Logger
|
|
_config: InvokeAIAppConfig
|
|
|
|
def __init__(self, config: InvokeAIAppConfig, logger: Logger):
|
|
self._logger = logger
|
|
self._config = config
|
|
|
|
if self._config.use_memory_db:
|
|
location = sqlite_memory
|
|
logger.info("Using in-memory database")
|
|
else:
|
|
db_path = self._config.db_path
|
|
db_path.parent.mkdir(parents=True, exist_ok=True)
|
|
location = str(db_path)
|
|
self._logger.info(f"Using database at {location}")
|
|
|
|
self.conn = sqlite3.connect(location, check_same_thread=False)
|
|
self.lock = threading.RLock()
|
|
self.conn.row_factory = sqlite3.Row
|
|
|
|
if self._config.log_sql:
|
|
self.conn.set_trace_callback(self._logger.debug)
|
|
|
|
self.conn.execute("PRAGMA foreign_keys = ON;")
|
|
|
|
def clean(self) -> None:
|
|
try:
|
|
self.lock.acquire()
|
|
self.conn.execute("VACUUM;")
|
|
self.conn.commit()
|
|
self._logger.info("Cleaned database")
|
|
except Exception as e:
|
|
self._logger.error(f"Error cleaning database: {e}")
|
|
raise e
|
|
finally:
|
|
self.lock.release()
|