From 14321d1b33332190272f1d3b88ac2be0523f22a7 Mon Sep 17 00:00:00 2001 From: PVBLIC Foundation Date: Mon, 11 May 2026 10:26:17 -0700 Subject: [PATCH] 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 --- backend/open_webui/internal/db.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/open_webui/internal/db.py b/backend/open_webui/internal/db.py index b544605687..a2f3293ea5 100644 --- a/backend/open_webui/internal/db.py +++ b/backend/open_webui/internal/db.py @@ -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) # ============================================================