diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index e561036408..391da6a53b 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -336,6 +336,10 @@ else: except Exception: DATABASE_POOL_RECYCLE = 3600 +DATABASE_ENABLE_SQLITE_WAL = ( + os.environ.get("DATABASE_ENABLE_SQLITE_WAL", "False").lower() == "true" +) + RESET_CONFIG_ON_START = ( os.environ.get("RESET_CONFIG_ON_START", "False").lower() == "true" ) diff --git a/backend/open_webui/internal/db.py b/backend/open_webui/internal/db.py index d7a200ff20..ccc3995ded 100644 --- a/backend/open_webui/internal/db.py +++ b/backend/open_webui/internal/db.py @@ -14,9 +14,10 @@ from open_webui.env import ( DATABASE_POOL_RECYCLE, DATABASE_POOL_SIZE, DATABASE_POOL_TIMEOUT, + DATABASE_ENABLE_SQLITE_WAL, ) from peewee_migrate import Router -from sqlalchemy import Dialect, create_engine, MetaData, types +from sqlalchemy import Dialect, create_engine, MetaData, event, types from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.pool import QueuePool, NullPool @@ -114,6 +115,14 @@ elif "sqlite" in SQLALCHEMY_DATABASE_URL: engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} ) + def on_connect(dbapi_connection, connection_record): + cursor = dbapi_connection.cursor() + if DATABASE_ENABLE_SQLITE_WAL: + cursor.execute("PRAGMA journal_mode=WAL") + else: + cursor.execute("PRAGMA journal_mode=DELETE") + cursor.close() + event.listen(engine, "connect", on_connect) else: if isinstance(DATABASE_POOL_SIZE, int): if DATABASE_POOL_SIZE > 0: