feat: 更新数据库时支持同步保存子表配置和DDL

- UpdateDatabaseRequest 添加 SubTables 字段
- 数据库更新时同步创建或更新子表记录
- 支持子表 DDL 的保存

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 10:33:30 +08:00
parent c917d6b04c
commit 2eddc53249
4 changed files with 75 additions and 15 deletions

View File

@@ -54,21 +54,22 @@ type CreateDatabaseRequest struct {
// UpdateRequest 更新数据库信息请求
type UpdateDatabaseRequest struct {
Name string `json:"name"`
Description string `json:"description"`
DBType string `json:"db_type"`
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
TableCount int `json:"table_count"`
Charset string `json:"charset"`
SSLMode string `json:"ssl_mode"`
URI string `json:"uri"` // Neo4j 连接地址
GraphLabels string `json:"graph_labels"` // Neo4j 标签列表 (JSON)
GraphRelationship string `json:"graph_relationship"` // Neo4j 关系类型列表 (JSON)
SelectedLabel string `json:"selected_label"` // 当前选中的标签
Name string `json:"name"`
Description string `json:"description"`
DBType string `json:"db_type"`
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
TableCount int `json:"table_count"`
Charset string `json:"charset"`
SSLMode string `json:"ssl_mode"`
URI string `json:"uri"` // Neo4j 连接地址
GraphLabels string `json:"graph_labels"` // Neo4j 标签列表 (JSON)
GraphRelationship string `json:"graph_relationship"` // Neo4j 关系类型列表 (JSON)
SelectedLabel string `json:"selected_label"` // 当前选中的标签
SubTables []CreateSubTableRequest `json:"sub_tables"` // 子表配置
}
// SaveGraphRequest 保存图谱信息请求

View File

@@ -95,6 +95,7 @@ type CreateSubTableRequest struct {
RelationField string `json:"relation_field"`
RelationType string `json:"relation_type"`
Fields []FieldMapping `json:"fields"` // 字段映射列表
DDL string `json:"ddl"` // 建表DDL
}
// UpdateSubTableRequest 更新子表请求
@@ -105,6 +106,7 @@ type UpdateSubTableRequest struct {
MappingType string `json:"mapping_type"`
RelationField string `json:"relation_field"`
RelationType string `json:"relation_type"`
DDL string `json:"ddl"`
}
// SubTableMapping 完整的子表映射配置(存储到文件的格式)

View File

@@ -696,12 +696,66 @@ func (s *DatabaseService) Update(id string, req model.UpdateDatabaseRequest) (*m
updates["ssl_mode"] = req.SSLMode
}
// 更新数据库基本信息
info := &model.DatabaseInfo{}
if err := s.repo.Update(id, info); err != nil {
log.Printf("[Update] 更新失败: %v", err)
return nil, err
}
// 处理 SubTables - 创建或更新子表记录(包括 DDL
if len(req.SubTables) > 0 {
log.Printf("[Update] 处理 %d 个子表配置", len(req.SubTables))
for _, subTableReq := range req.SubTables {
subTableReq.DatabaseID = id
// 检查是否已存在(根据 parent_table 查找)
existingTables, err := s.subTableRepo.FindByDatabaseID(id)
if err != nil {
log.Printf("[Update] 查询子表失败: %v", err)
continue
}
found := false
for _, existing := range existingTables {
if existing.ParentTable == subTableReq.ParentTable {
// 存在则更新
log.Printf("[Update] 更新子表: %s", existing.ID)
err := s.subTableRepo.Update(existing.ID, &model.SubTableInfo{
ParentTable: subTableReq.ParentTable,
SubTableName: subTableReq.SubTableName,
SubTableComment: subTableReq.SubTableComment,
MappingType: subTableReq.MappingType,
RelationField: subTableReq.RelationField,
RelationType: subTableReq.RelationType,
DDL: subTableReq.DDL,
})
if err != nil {
log.Printf("[Update] 更新子表失败: %v", err)
}
found = true
break
}
}
if !found {
// 不存在则创建
log.Printf("[Update] 创建子表: %s", subTableReq.ParentTable)
err := s.subTableRepo.Create(&model.SubTableInfo{
ID: uuid.New().String(),
DatabaseID: id,
ParentTable: subTableReq.ParentTable,
SubTableName: subTableReq.SubTableName,
SubTableComment: subTableReq.SubTableComment,
MappingType: subTableReq.MappingType,
RelationField: subTableReq.RelationField,
RelationType: subTableReq.RelationType,
DDL: subTableReq.DDL,
})
if err != nil {
log.Printf("[Update] 创建子表失败: %v", err)
}
}
}
}
return s.repo.FindByID(id)
}

View File

@@ -119,6 +119,7 @@ func (s *SubTableService) Create(req model.CreateSubTableRequest) (*model.SubTab
MappingType: req.MappingType,
RelationField: req.RelationField,
RelationType: req.RelationType,
DDL: req.DDL,
}
if err := s.repo.Create(info); err != nil {
@@ -196,6 +197,8 @@ func (s *SubTableService) Update(id string, req model.UpdateSubTableRequest) (*m
if req.RelationType != "" {
info.RelationType = req.RelationType
}
// 更新 DDL
info.DDL = req.DDL
if err := s.repo.Update(id, info); err != nil {
log.Printf("[SubTable Update] 更新失败: %v", err)