fix: stabilize backend services and frontend pointer
This commit is contained in:
@@ -57,6 +57,8 @@ class RagDatasetServiceImpl(IRagDatasetService):
|
||||
ORDER BY dataset_id, is_default DESC, sort_order ASC, id ASC
|
||||
) a ON a.dataset_id = d.id
|
||||
"""
|
||||
_tenant_schema_checked = False
|
||||
_tenant_schema_lock = asyncio.Lock()
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.TenantResolver = TenantResolver()
|
||||
@@ -1038,10 +1040,39 @@ class RagDatasetServiceImpl(IRagDatasetService):
|
||||
raise LeauditException(StatusCodeEnum.HTTP_403_FORBIDDEN, "当前用户只能管理本地区知识库")
|
||||
|
||||
async def _ensure_rag_tenant_schema(self, session) -> None:
|
||||
await session.execute(text("ALTER TABLE rag_dataset ADD COLUMN IF NOT EXISTS tenant_code VARCHAR(64) NULL"))
|
||||
await session.execute(text("ALTER TABLE rag_chat_app ADD COLUMN IF NOT EXISTS tenant_code VARCHAR(64) NULL"))
|
||||
await session.execute(text("CREATE INDEX IF NOT EXISTS idx_rag_dataset_tenant_code ON rag_dataset(tenant_code) WHERE deleted_at IS NULL"))
|
||||
await session.execute(text("CREATE INDEX IF NOT EXISTS idx_rag_chat_app_tenant_code ON rag_chat_app(tenant_code) WHERE deleted_at IS NULL"))
|
||||
if self.__class__._tenant_schema_checked:
|
||||
return
|
||||
|
||||
async with self.__class__._tenant_schema_lock:
|
||||
if self.__class__._tenant_schema_checked:
|
||||
return
|
||||
|
||||
columns = (
|
||||
await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT table_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = current_schema()
|
||||
AND table_name IN ('rag_dataset', 'rag_chat_app')
|
||||
AND column_name = 'tenant_code'
|
||||
"""
|
||||
)
|
||||
)
|
||||
).scalars().all()
|
||||
existing = set(columns)
|
||||
if existing == {"rag_dataset", "rag_chat_app"}:
|
||||
self.__class__._tenant_schema_checked = True
|
||||
return
|
||||
|
||||
await session.execute(text("SET LOCAL lock_timeout = '1000ms'"))
|
||||
if "rag_dataset" not in existing:
|
||||
await session.execute(text("ALTER TABLE rag_dataset ADD COLUMN tenant_code VARCHAR(64) NULL"))
|
||||
if "rag_chat_app" not in existing:
|
||||
await session.execute(text("ALTER TABLE rag_chat_app ADD COLUMN tenant_code VARCHAR(64) NULL"))
|
||||
await session.execute(text("CREATE INDEX IF NOT EXISTS idx_rag_dataset_tenant_code ON rag_dataset(tenant_code) WHERE deleted_at IS NULL"))
|
||||
await session.execute(text("CREATE INDEX IF NOT EXISTS idx_rag_chat_app_tenant_code ON rag_chat_app(tenant_code) WHERE deleted_at IS NULL"))
|
||||
self.__class__._tenant_schema_checked = True
|
||||
|
||||
def _dataset_tenant_filter_sql(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user