fix(platform): 模型路由 - 合并PR并修复归档重试兼容
This commit is contained in:
1 parent
51c79c5a65
commit
a5bac9ec78
14 files changed
+365
-70
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type LogicalModelGraph struct {
|
||||
@@ -17,10 +18,11 @@ type LogicalModelGraph struct {
|
||||
}
|
||||
|
||||
var ErrLogicalModelInUse = errors.New("logical model is in use")
|
||||
var ErrLogicalModelUnavailable = errors.New("logical model is unavailable")
|
||||
|
||||
func (r *Repository) LogicalModels(includeDisabled bool) ([]model.LogicalModel, error) {
|
||||
var items []model.LogicalModel
|
||||
query := r.db.Order("sort_order asc, created_at asc")
|
||||
query := r.db.Where("archived_at IS NULL").Order("sort_order asc, created_at asc")
|
||||
if !includeDisabled {
|
||||
query = query.Where("enabled = ?", true)
|
||||
}
|
||||
@@ -403,19 +405,17 @@ func (r *Repository) SaveLogicalModelBundle(item *model.LogicalModel, revision *
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteLogicalModel(id string, now time.Time) error {
|
||||
func (r *Repository) ArchiveLogicalModel(id string, audit *model.AdminAuditEvent, now time.Time) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 先停用并锁定目录主体,避免删除期间仍被新的数据库查询选中。
|
||||
result := tx.Model(&model.LogicalModel{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{"enabled": false, "updated_at": now})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
// PostgreSQL 下锁定主体,使任务创建与归档在同一行上串行,避免检查后仍写入新任务。
|
||||
var item model.LogicalModel
|
||||
query := tx.Where("id = ? AND archived_at IS NULL", id)
|
||||
if r.Dialect() == "postgres" {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
if result.RowsAffected != 1 {
|
||||
return gorm.ErrRecordNotFound
|
||||
if err := query.First(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var activeTasks int64
|
||||
if err := tx.Model(&model.Task{}).
|
||||
Where("logical_model_id = ? AND status IN ?", id, []model.TaskStatus{model.TaskStatusQueued, model.TaskStatusRunning}).
|
||||
@@ -425,19 +425,48 @@ func (r *Repository) DeleteLogicalModel(id string, now time.Time) error {
|
||||
if activeTasks > 0 {
|
||||
return ErrLogicalModelInUse
|
||||
}
|
||||
|
||||
// revision 与 route 是不可变的任务快照;删除目录主体时保留它们供历史记录追溯。
|
||||
deleted := tx.Where("id = ?", id).Delete(&model.LogicalModel{})
|
||||
if deleted.Error != nil {
|
||||
return deleted.Error
|
||||
archived := tx.Model(&model.LogicalModel{}).
|
||||
Where("id = ? AND archived_at IS NULL", id).
|
||||
Updates(map[string]any{"enabled": false, "archived_at": now, "updated_at": now})
|
||||
if archived.Error != nil {
|
||||
return archived.Error
|
||||
}
|
||||
if deleted.RowsAffected != 1 {
|
||||
if archived.RowsAffected != 1 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
if audit != nil {
|
||||
if err := tx.Create(audit).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// requireActiveLogicalModelForTask 与归档共用主体行锁,保证新任务只引用仍在目录中的当前 revision。
|
||||
func (r *Repository) requireActiveLogicalModelForTask(tx *gorm.DB, task *model.Task) error {
|
||||
if task == nil || task.LogicalModelID == "" {
|
||||
return nil
|
||||
}
|
||||
var item model.LogicalModel
|
||||
query := tx.Select("id").Where(
|
||||
"id = ? AND enabled = ? AND archived_at IS NULL AND active_revision_id = ?",
|
||||
task.LogicalModelID,
|
||||
true,
|
||||
task.LogicalModelRevisionID,
|
||||
)
|
||||
if r.Dialect() == "postgres" {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
if err := query.First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrLogicalModelUnavailable
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteLogicalModelRoute(id string) error {
|
||||
return r.db.Model(&model.LogicalModelRoute{}).Where("id = ?", id).Updates(map[string]any{"enabled": false, "weight": 0, "updated_at": time.Now()}).Error
|
||||
}
|
||||
Reference in new issue
Block a user