feat: 数据源编辑加排除数据库+脱敏字段+审批流关联; 权限组Transfer改为选择实例
- EditInstanceDialog: 新增排除数据库(逗号分隔)、脱敏字段(动态表单+表/字段/规则)、审批流程关联 - Instance: workflow JSON字段改为workflow_id FK关联workflow_definitions表 - InstanceGroupPage: Transfer穿梭框从选数据库改为选实例(数据源) - 后端instance API返回workflow_name, _get_workflow_names批量查询 - DB迁移: instances表添加workflow_id列 - 37个测试全部通过
This commit is contained in:
1 parent
4111e1eee8
commit
128de64592
6 files changed
+268
-211
No files matched your search
@@ -19,7 +19,7 @@ from app.services.audit_log_service import AuditLogService
|
||||
router = APIRouter(prefix="/instances", tags=["数据源管理"])
|
||||
|
||||
|
||||
def _serialize_json_fields(instance: Instance) -> dict:
|
||||
def _serialize_json_fields(instance: Instance, workflow_name: str = None) -> dict:
|
||||
"""将 Instance 的 JSON text 字段反序列化为 dict/list,返回 Response dict"""
|
||||
d = {
|
||||
"id": instance.id,
|
||||
@@ -34,7 +34,8 @@ def _serialize_json_fields(instance: Instance) -> dict:
|
||||
"status": instance.status,
|
||||
"version": instance.version,
|
||||
"access_mode": instance.access_mode or "readwrite",
|
||||
"workflow": json.loads(instance.workflow) if instance.workflow else None,
|
||||
"workflow_id": instance.workflow_id,
|
||||
"workflow_name": workflow_name,
|
||||
"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,
|
||||
@@ -42,6 +43,16 @@ def _serialize_json_fields(instance: Instance) -> dict:
|
||||
return d
|
||||
|
||||
|
||||
async def _get_workflow_names(db: AsyncSession, instances: list) -> dict:
|
||||
"""批量获取 workflow_id -> name 映射"""
|
||||
from app.models.workflow import WorkflowDefinition
|
||||
wids = list({i.workflow_id for i in instances if i.workflow_id})
|
||||
if not wids:
|
||||
return {}
|
||||
result = await db.execute(select(WorkflowDefinition).where(WorkflowDefinition.id.in_(wids)))
|
||||
return {w.id: w.name for w in result.scalars().all()}
|
||||
|
||||
|
||||
@router.get("", response_model=List[InstanceResponse])
|
||||
async def list_instances(
|
||||
db_type: Optional[str] = None,
|
||||
@@ -71,7 +82,8 @@ async def list_instances(
|
||||
|
||||
result = await db.execute(stmt)
|
||||
instances = result.scalars().all()
|
||||
return [_serialize_json_fields(i) for i in instances]
|
||||
wnames = await _get_workflow_names(db, instances)
|
||||
return [_serialize_json_fields(i, wnames.get(i.workflow_id)) for i in instances]
|
||||
|
||||
|
||||
@router.post("", response_model=InstanceResponse, status_code=201)
|
||||
@@ -91,7 +103,7 @@ async def create_instance(
|
||||
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,
|
||||
workflow_id=data.workflow_id,
|
||||
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,
|
||||
)
|
||||
@@ -104,7 +116,8 @@ async def create_instance(
|
||||
)
|
||||
await db.commit()
|
||||
await db.refresh(instance)
|
||||
return _serialize_json_fields(instance)
|
||||
wnames = await _get_workflow_names(db, [instance])
|
||||
return _serialize_json_fields(instance, wnames.get(instance.workflow_id))
|
||||
|
||||
|
||||
@router.post("/test-connection", response_model=ConnectionTestResult)
|
||||
@@ -133,7 +146,8 @@ async def get_instance(
|
||||
raise HTTPException(status_code=404, detail="数据源不存在")
|
||||
# P0: 检查访问权限
|
||||
await check_instance_access(current_user, instance, db)
|
||||
return _serialize_json_fields(instance)
|
||||
wnames = await _get_workflow_names(db, [instance])
|
||||
return _serialize_json_fields(instance, wnames.get(instance.workflow_id))
|
||||
|
||||
|
||||
@router.put("/{instance_id}", response_model=InstanceResponse)
|
||||
@@ -151,9 +165,9 @@ 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
|
||||
# workflow_id: 0 means clear
|
||||
if "workflow_id" in update_data and update_data["workflow_id"] == 0:
|
||||
update_data["workflow_id"] = 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:
|
||||
@@ -163,7 +177,8 @@ async def update_instance(
|
||||
setattr(instance, key, value)
|
||||
await db.commit()
|
||||
await db.refresh(instance)
|
||||
return _serialize_json_fields(instance)
|
||||
wnames = await _get_workflow_names(db, [instance])
|
||||
return _serialize_json_fields(instance, wnames.get(instance.workflow_id))
|
||||
|
||||
|
||||
@router.delete("/{instance_id}")
|
||||
|
||||
@@ -22,7 +22,7 @@ class Instance(Base):
|
||||
|
||||
# 新增字段
|
||||
access_mode = Column(String(20), default="readwrite") # readwrite / read / write
|
||||
workflow = Column(Text, nullable=True) # JSON: [{stage, role, assignee}]
|
||||
workflow_id = Column(Integer, ForeignKey("workflow_definitions.id", ondelete="SET NULL"), nullable=True)
|
||||
excluded_databases = Column(Text, nullable=True) # JSON: ["sys", "information_schema", ...]
|
||||
mask_fields = Column(Text, nullable=True) # JSON: [{table, field, rule}]
|
||||
|
||||
@@ -34,3 +34,4 @@ class Instance(Base):
|
||||
|
||||
# 关系
|
||||
group = relationship("InstanceGroup", back_populates="instances")
|
||||
workflow_def = relationship("WorkflowDefinition")
|
||||
@@ -26,7 +26,7 @@ class InstanceCreate(BaseModel):
|
||||
env: Literal["production", "staging", "development"] = "development"
|
||||
description: Optional[str] = None
|
||||
access_mode: Literal["readwrite", "read", "write"] = "readwrite"
|
||||
workflow: Optional[List[WorkflowStage]] = None
|
||||
workflow_id: Optional[int] = Field(None, description="关联的审批流程ID")
|
||||
excluded_databases: Optional[List[str]] = None
|
||||
mask_fields: Optional[List[MaskFieldRule]] = None
|
||||
|
||||
@@ -41,7 +41,7 @@ class InstanceUpdate(BaseModel):
|
||||
env: Optional[Literal["production", "staging", "development"]] = None
|
||||
description: Optional[str] = None
|
||||
access_mode: Optional[Literal["readwrite", "read", "write"]] = None
|
||||
workflow: Optional[List[WorkflowStage]] = None
|
||||
workflow_id: Optional[int] = Field(default=None, description="关联的审批流程ID,设为0清除关联")
|
||||
excluded_databases: Optional[List[str]] = None
|
||||
mask_fields: Optional[List[MaskFieldRule]] = None
|
||||
|
||||
@@ -59,7 +59,8 @@ class InstanceResponse(BaseModel):
|
||||
status: str
|
||||
version: Optional[str] = None
|
||||
access_mode: str = "readwrite"
|
||||
workflow: Optional[List[dict]] = None
|
||||
workflow_id: Optional[int] = None
|
||||
workflow_name: Optional[str] = None
|
||||
excluded_databases: Optional[List[str]] = None
|
||||
mask_fields: Optional[List[dict]] = None
|
||||
created_at: datetime
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""测试 Instance 新增字段:access_mode, workflow, excluded_databases, mask_fields"""
|
||||
"""测试 Instance 新增字段:access_mode, workflow_id, excluded_databases, mask_fields"""
|
||||
import pytest
|
||||
from tests.conftest import auth_header
|
||||
|
||||
@@ -12,14 +12,9 @@ async def test_create_instance_with_full_fields(client, admin_token):
|
||||
"host": "mysql.example.com",
|
||||
"port": 3306,
|
||||
"username": "admin",
|
||||
"password": "secret123",
|
||||
"password": "***",
|
||||
"env": "production",
|
||||
"access_mode": "read",
|
||||
"workflow": [
|
||||
{"stage": "提交阶段", "role": "提交人", "assignee": ""},
|
||||
{"stage": "开发负责人", "role": "开发负责人", "assignee": "lairdlandonloose"},
|
||||
{"stage": "执行人", "role": "执行人", "assignee": "zachlogan"},
|
||||
],
|
||||
"excluded_databases": ["sys", "information_schema", "mysql"],
|
||||
"mask_fields": [
|
||||
{"table": "users", "field": "phone", "rule": "mask"},
|
||||
@@ -29,9 +24,7 @@ async def test_create_instance_with_full_fields(client, admin_token):
|
||||
assert res.status_code == 201
|
||||
data = res.json()
|
||||
assert data["access_mode"] == "read"
|
||||
assert data["workflow"] is not None
|
||||
assert len(data["workflow"]) == 3
|
||||
assert data["workflow"][1]["assignee"] == "lairdlandonloose"
|
||||
assert data["workflow_id"] is None
|
||||
assert data["excluded_databases"] == ["sys", "information_schema", "mysql"]
|
||||
assert len(data["mask_fields"]) == 2
|
||||
assert data["mask_fields"][0]["table"] == "users"
|
||||
@@ -46,7 +39,7 @@ async def test_create_instance_default_access_mode(client, admin_token):
|
||||
"host": "127.0.0.1",
|
||||
"port": 3306,
|
||||
"username": "root",
|
||||
"password": "test123",
|
||||
"password": "***",
|
||||
}, headers=auth_header(admin_token))
|
||||
assert res.status_code == 201
|
||||
assert res.json()["access_mode"] == "readwrite"
|
||||
@@ -55,16 +48,14 @@ async def test_create_instance_default_access_mode(client, admin_token):
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_instance_includes_new_fields(client, admin_token):
|
||||
"""列表接口返回新字段"""
|
||||
# 创建
|
||||
await client.post("/api/v1/instances", json={
|
||||
"name": "list-test",
|
||||
"db_type": "postgres",
|
||||
"host": "10.0.0.1",
|
||||
"port": 5432,
|
||||
"username": "postgres",
|
||||
"password": "pwd",
|
||||
"password": "***",
|
||||
"access_mode": "write",
|
||||
"workflow": [{"stage": "审批", "role": "DBA", "assignee": "alice"}],
|
||||
}, headers=auth_header(admin_token))
|
||||
|
||||
res = await client.get("/api/v1/instances", headers=auth_header(admin_token))
|
||||
@@ -73,35 +64,28 @@ async def test_list_instance_includes_new_fields(client, admin_token):
|
||||
found = [i for i in items if i["name"] == "list-test"]
|
||||
assert len(found) == 1
|
||||
assert found[0]["access_mode"] == "write"
|
||||
assert found[0]["workflow"] is not None
|
||||
assert found[0]["workflow"][0]["assignee"] == "alice"
|
||||
assert "workflow_id" in found[0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_instance_workflow(client, admin_token):
|
||||
"""更新流程配置"""
|
||||
async def test_update_instance_fields(client, admin_token):
|
||||
"""更新字段"""
|
||||
create = await client.post("/api/v1/instances", json={
|
||||
"name": "update-wf",
|
||||
"name": "update-fields",
|
||||
"db_type": "mysql",
|
||||
"host": "127.0.0.1",
|
||||
"port": 3306,
|
||||
"username": "root",
|
||||
"password": "test",
|
||||
"password": "***",
|
||||
}, headers=auth_header(admin_token))
|
||||
iid = create.json()["id"]
|
||||
|
||||
res = await client.put(f"/api/v1/instances/{iid}", json={
|
||||
"workflow": [
|
||||
{"stage": "提交", "role": "提交人", "assignee": "bob"},
|
||||
{"stage": "审核", "role": "DBA", "assignee": "alice"},
|
||||
{"stage": "执行", "role": "DBA", "assignee": "charlie"},
|
||||
],
|
||||
"access_mode": "read",
|
||||
"excluded_databases": ["test_db"],
|
||||
}, headers=auth_header(admin_token))
|
||||
assert res.status_code == 200
|
||||
data = res.json()
|
||||
assert len(data["workflow"]) == 3
|
||||
assert data["access_mode"] == "read"
|
||||
assert data["excluded_databases"] == ["test_db"]
|
||||
|
||||
@@ -115,10 +99,46 @@ async def test_instance_null_json_fields(client, admin_token):
|
||||
"host": "127.0.0.1",
|
||||
"port": 3306,
|
||||
"username": "root",
|
||||
"password": "test",
|
||||
"password": "***",
|
||||
}, headers=auth_header(admin_token))
|
||||
assert res.status_code == 201
|
||||
data = res.json()
|
||||
assert data["workflow"] is None
|
||||
assert data["workflow_id"] is None
|
||||
assert data["excluded_databases"] is None
|
||||
assert data["mask_fields"] is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_instance_workflow_id_association(client, admin_token):
|
||||
"""数据源关联审批流程"""
|
||||
# 创建 workflow
|
||||
wf_res = await client.post("/api/v1/workflows", json={
|
||||
"name": "通用生产审批流程",
|
||||
"description": "测试用",
|
||||
"category": "ticket",
|
||||
}, headers=auth_header(admin_token))
|
||||
if wf_res.status_code == 201:
|
||||
wf_id = wf_res.json()["id"]
|
||||
else:
|
||||
# workflow may already exist, try to get one
|
||||
wf_list = await client.get("/api/v1/workflows", headers=auth_header(admin_token))
|
||||
workflows = wf_list.json()
|
||||
wf_id = workflows[0]["id"] if workflows else None
|
||||
|
||||
if not wf_id:
|
||||
pytest.skip("No workflow available")
|
||||
|
||||
# 创建带 workflow_id 的实例
|
||||
res = await client.post("/api/v1/instances", json={
|
||||
"name": "wf-instance",
|
||||
"db_type": "mysql",
|
||||
"host": "127.0.0.1",
|
||||
"port": 3306,
|
||||
"username": "root",
|
||||
"password": "***",
|
||||
"workflow_id": wf_id,
|
||||
}, headers=auth_header(admin_token))
|
||||
assert res.status_code == 201
|
||||
data = res.json()
|
||||
assert data["workflow_id"] == wf_id
|
||||
assert data["workflow_name"] is not None
|
||||
Reference in new issue
Block a user