OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
"""Async CRUD over the ``workspace_config`` table.
Each row is one logical section ('providers' | 'knowledge' | 'agent' |
'onboarding' | 'meta'). The repo speaks plain dicts — serialization
to/from ``OpenRAGConfig`` is the service's job.
"""
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import col
from db.models import WorkspaceConfig
# Section names recognized by the migration / service.
SECTIONS = ("providers", "knowledge", "agent", "onboarding", "meta")
class WorkspaceConfigRepo:
def __init__(self, session: AsyncSession):
self.session = session
async def get_section(self, section: str) -> dict[str, Any] | None:
row = await self.session.get(WorkspaceConfig, section)
return None if row is None else (row.value or {})
async def list_all(self) -> dict[str, dict[str, Any]]:
result = await self.session.execute(select(WorkspaceConfig))
return {row.section: (row.value or {}) for row in result.scalars().all()}
async def upsert(
self,
section: str,
value: dict[str, Any],
actor_user_id: str | None = None,
) -> WorkspaceConfig:
existing = await self.session.get(WorkspaceConfig, section)
if existing is None:
row = WorkspaceConfig(
section=section,
value=value,
updated_at=datetime.now(UTC),
updated_by=actor_user_id,
)
self.session.add(row)
await self.session.flush()
return row
existing.value = value
existing.updated_at = datetime.now(UTC)
if actor_user_id is not None:
existing.updated_by = actor_user_id
self.session.add(existing)
await self.session.flush()
return existing
async def merge_section_keys(
self,
section: str,
updates: dict[str, Any],
deletions: set[str] | None = None,
actor_user_id: str | None = None,
) -> WorkspaceConfig:
"""Atomically merge *updates* into a section, removing *deletions*.
Acquires a ``SELECT FOR UPDATE`` row lock (PostgreSQL) before reading
so that concurrent callers cannot interleave their read-modify-write
cycles and silently overwrite each other's keys. On SQLite the lock
clause is a no-op, which is safe because the deployment constraint
enforces a single uvicorn worker (see AGENTS.md).
"""
stmt = (
select(WorkspaceConfig).where(col(WorkspaceConfig.section) == section).with_for_update()
)
result = await self.session.execute(stmt)
existing = result.scalar_one_or_none()
merged = dict(existing.value or {}) if existing is not None else {}
merged.update(updates)
for key in deletions or set():
merged.pop(key, None)
if existing is None:
row = WorkspaceConfig(
section=section,
value=merged,
updated_at=datetime.now(UTC),
updated_by=actor_user_id,
)
self.session.add(row)
await self.session.flush()
return row
existing.value = merged
existing.updated_at = datetime.now(UTC)
if actor_user_id is not None:
existing.updated_by = actor_user_id
self.session.add(existing)
await self.session.flush()
return existing
async def has_any(self) -> bool:
"""Quick check for the migration: do we have any rows?"""
result = await self.session.execute(select(WorkspaceConfig).limit(1))
return result.scalar_one_or_none() is not None