fix: restore sync get_db_context after v0.9.5 merge

Upstream v0.9.5 dropped the sync context-manager helper from
internal/db.py, but our local UserUsages and KnowledgeDrive paths still
import and use it (and chats.py imports it for backward compat). Without
this helper, importing open_webui.models.chats fails at startup with
ImportError: cannot import name 'get_db_context'.

Re-adds the original implementation: yields the passed session if
DATABASE_ENABLE_SESSION_SHARING is on, otherwise opens a new one via
get_db().

Made-with: Cursor
This commit is contained in:
PVBLIC Foundation
2026-05-11 10:26:17 -07:00
parent 972b0d8f34
commit 14321d1b33
+15
View File
@@ -324,6 +324,21 @@ def get_session():
get_db = contextmanager(get_session)
@contextmanager
def get_db_context(db: Optional[Session] = None):
"""Sync context manager that reuses an existing session if provided.
Used by local sync paths (UserUsages, KnowledgeDrive) where rewriting to
async would require touching call sites in middleware/routers. Mirrors
the async ``get_async_db_context`` pattern.
"""
if isinstance(db, Session) and DATABASE_ENABLE_SESSION_SHARING:
yield db
else:
with get_db() as session:
yield session
# ============================================================
# ASYNC ENGINE (used for ALL runtime database operations)
# ============================================================