fix(backend): harden final MVP invariants
This commit is contained in:
@@ -236,14 +236,15 @@ func findOrCreateProjectTag(tx *gorm.DB, projectID uint, value string) (*uint, *
|
||||
if name == "" {
|
||||
return nil, nil, "", nil
|
||||
}
|
||||
candidate := models.SenlinAgentTag{ProjectID: projectID, Name: name}
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "project_id"}, {Name: "name"}},
|
||||
DoNothing: true,
|
||||
}).Create(&candidate).Error; err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
var tag models.SenlinAgentTag
|
||||
err := tx.Where("project_id = ? AND name = ?", projectID, name).First(&tag).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
tag = models.SenlinAgentTag{ProjectID: projectID, Name: name}
|
||||
if err := tx.Create(&tag).Error; err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
} else if err != nil {
|
||||
if err := tx.Where("project_id = ? AND name = ?", projectID, name).First(&tag).Error; err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
return &tag.ID, &tag.Identity, tag.Name, nil
|
||||
|
||||
Reference in New Issue
Block a user