Update engine.py

This commit is contained in:
2026-01-20 13:11:48 +01:00
parent 5345e61d58
commit 527fce8a1d

View File

@@ -18,11 +18,15 @@ class SorterEngine:
@contextmanager @contextmanager
def get_connection(): def get_connection():
"""Context manager for efficient DB connections with WAL mode.""" """Context manager for efficient DB connections with WAL mode."""
# Ensure directory exists
db_dir = os.path.dirname(SorterEngine.DB_PATH)
if db_dir and not os.path.exists(db_dir):
os.makedirs(db_dir, exist_ok=True)
conn = sqlite3.connect(SorterEngine.DB_PATH, check_same_thread=False) conn = sqlite3.connect(SorterEngine.DB_PATH, check_same_thread=False)
conn.execute("PRAGMA journal_mode=WAL") # Write-Ahead Logging for speed conn.execute("PRAGMA journal_mode=WAL") # Write-Ahead Logging for speed
conn.execute("PRAGMA synchronous=NORMAL") # Faster writes conn.execute("PRAGMA synchronous=NORMAL") # Faster writes
conn.execute("PRAGMA cache_size=2000") # ~2MB cache conn.execute("PRAGMA cache_size=2000") # ~2MB cache
conn.row_factory = sqlite3.Row # Efficient row access
try: try:
yield conn yield conn
finally: finally: