feat: 项目重命名 SQL-Platform + 数据源表单重写(连接模式/流程/排除DB/脱敏)
- 项目名称 Web-Lite Pro → SQL-Platform 全局替换(13处) - Instance 模型新增: access_mode, workflow, excluded_databases, mask_fields - 创建数据源表单: 分区卡片(数据源信息/连接模式/流程/高级配置) - 后端 JSON 字段序列化/反序列化 - 新增 5 个 Instance 字段测试 (133 total)
This commit is contained in:
1 parent
1877522239
commit
d5d19d6be4
16 files changed
+366
-73
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from typing import Optional, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
@@ -16,6 +17,29 @@ from app.services.audit_log_service import AuditLogService
|
||||
router = APIRouter(prefix="/instances", tags=["数据源管理"])
|
||||
|
||||
|
||||
def _serialize_json_fields(instance: Instance) -> dict:
|
||||
"""将 Instance 的 JSON text 字段反序列化为 dict/list,返回 Response dict"""
|
||||
d = {
|
||||
"id": instance.id,
|
||||
"name": instance.name,
|
||||
"db_type": instance.db_type,
|
||||
"host": instance.host,
|
||||
"port": instance.port,
|
||||
"username": instance.username,
|
||||
"database": instance.database,
|
||||
"env": instance.env,
|
||||
"description": instance.description,
|
||||
"status": instance.status,
|
||||
"version": instance.version,
|
||||
"access_mode": instance.access_mode or "readwrite",
|
||||
"workflow": json.loads(instance.workflow) if instance.workflow else None,
|
||||
"excluded_databases": json.loads(instance.excluded_databases) if instance.excluded_databases else None,
|
||||
"mask_fields": json.loads(instance.mask_fields) if instance.mask_fields else None,
|
||||
"created_at": instance.created_at,
|
||||
}
|
||||
return d
|
||||
|
||||
|
||||
@router.get("", response_model=List[InstanceResponse])
|
||||
async def list_instances(
|
||||
db_type: Optional[str] = None,
|
||||
@@ -29,7 +53,8 @@ async def list_instances(
|
||||
if env:
|
||||
stmt = stmt.where(Instance.env == env)
|
||||
result = await db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
instances = result.scalars().all()
|
||||
return [_serialize_json_fields(i) for i in instances]
|
||||
|
||||
|
||||
@router.post("", response_model=InstanceResponse, status_code=201)
|
||||
@@ -48,6 +73,10 @@ async def create_instance(
|
||||
database=data.database,
|
||||
env=data.env,
|
||||
description=data.description,
|
||||
access_mode=data.access_mode,
|
||||
workflow=json.dumps([s.model_dump() for s in data.workflow], ensure_ascii=False) if data.workflow else None,
|
||||
excluded_databases=json.dumps(data.excluded_databases, ensure_ascii=False) if data.excluded_databases else None,
|
||||
mask_fields=json.dumps([m.model_dump() for m in data.mask_fields], ensure_ascii=False) if data.mask_fields else None,
|
||||
)
|
||||
db.add(instance)
|
||||
await db.flush()
|
||||
@@ -58,7 +87,7 @@ async def create_instance(
|
||||
)
|
||||
await db.commit()
|
||||
await db.refresh(instance)
|
||||
return instance
|
||||
return _serialize_json_fields(instance)
|
||||
|
||||
|
||||
@router.get("/{instance_id}", response_model=InstanceResponse)
|
||||
@@ -71,7 +100,7 @@ async def get_instance(
|
||||
instance = result.scalar_one_or_none()
|
||||
if not instance:
|
||||
raise HTTPException(status_code=404, detail="数据源不存在")
|
||||
return instance
|
||||
return _serialize_json_fields(instance)
|
||||
|
||||
|
||||
@router.put("/{instance_id}", response_model=InstanceResponse)
|
||||
@@ -89,11 +118,19 @@ async def update_instance(
|
||||
update_data = data.model_dump(exclude_unset=True)
|
||||
if "password" in update_data:
|
||||
update_data["password"] = encrypt_password(update_data["password"])
|
||||
# JSON fields
|
||||
if "workflow" in update_data:
|
||||
update_data["workflow"] = json.dumps([s if isinstance(s, dict) else s for s in update_data["workflow"]], ensure_ascii=False) if update_data["workflow"] else None
|
||||
if "excluded_databases" in update_data:
|
||||
update_data["excluded_databases"] = json.dumps(update_data["excluded_databases"], ensure_ascii=False) if update_data["excluded_databases"] else None
|
||||
if "mask_fields" in update_data:
|
||||
update_data["mask_fields"] = json.dumps([m if isinstance(m, dict) else m for m in update_data["mask_fields"]], ensure_ascii=False) if update_data["mask_fields"] else None
|
||||
|
||||
for key, value in update_data.items():
|
||||
setattr(instance, key, value)
|
||||
await db.commit()
|
||||
await db.refresh(instance)
|
||||
return instance
|
||||
return _serialize_json_fields(instance)
|
||||
|
||||
|
||||
@router.delete("/{instance_id}")
|
||||
@@ -128,7 +165,6 @@ async def test_connection(
|
||||
if not instance:
|
||||
raise HTTPException(status_code=404, detail="数据源不存在")
|
||||
|
||||
# Decrypt password for connection test
|
||||
instance.password = decrypt_password(instance.password)
|
||||
return await InstanceService.test_connection(instance)
|
||||
|
||||
|
||||
Reference in new issue
Block a user