fix: set busy_timeout and WAL pragmas on every async SQLite connection

This commit is contained in:
2026-04-13 19:17:53 -04:00
parent 62db686b42
commit c9be447a38

View File

@@ -1,5 +1,5 @@
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy import create_engine, Engine from sqlalchemy import create_engine, Engine, event
from sqlmodel import SQLModel from sqlmodel import SQLModel
from typing import AsyncGenerator from typing import AsyncGenerator
@@ -11,7 +11,21 @@ def get_async_engine(db_path: str) -> AsyncEngine:
prefix = "sqlite+aiosqlite:///" prefix = "sqlite+aiosqlite:///"
if db_path.startswith(":memory:"): if db_path.startswith(":memory:"):
prefix = "sqlite+aiosqlite://" prefix = "sqlite+aiosqlite://"
return create_async_engine(f"{prefix}{db_path}", echo=False, connect_args={"uri": True}) engine = create_async_engine(
f"{prefix}{db_path}",
echo=False,
connect_args={"uri": True, "timeout": 30},
)
@event.listens_for(engine.sync_engine, "connect")
def _set_sqlite_pragmas(dbapi_conn, _conn_record):
cursor = dbapi_conn.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.execute("PRAGMA busy_timeout=30000")
cursor.close()
return engine
def get_sync_engine(db_path: str) -> Engine: def get_sync_engine(db_path: str) -> Engine:
prefix = "sqlite:///" prefix = "sqlite:///"