Skip to content

Commit

Permalink
Use db.Save instead of Updates
Browse files Browse the repository at this point in the history
`db.Updates` does not respect the serializer
field tag, so attempts to update the model
with it will fail.

Signed-off-by: Sam Lucidi <slucidi@redhat.com>
  • Loading branch information
mansam committed Jul 1, 2024
1 parent e5ec915 commit d7c906e
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 56 deletions.
2 changes: 1 addition & 1 deletion api/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) {
db = db.Omit(clause.Associations)
m.Archived = true
m.Summary = summary
err = db.Updates(h.fields(&m)).Error
err = db.Save(&m).Error
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (h ApplicationHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations, "BucketID")
result = db.Updates(h.fields(m))
result = db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (h ArchetypeHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/assessment.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations, "Thresholds", "RiskMessages")
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
6 changes: 0 additions & 6 deletions api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ func (h *BaseHandler) preLoad(db *gorm.DB, fields ...string) (tx *gorm.DB) {
return
}

// fields builds a map of fields.
func (h *BaseHandler) fields(m any) (mp map[string]any) {
mp = reflect.Fields(m)
return
}

// pk returns the PK (ID) parameter.
func (h *BaseHandler) pk(ctx *gin.Context) (id uint) {
s := ctx.Param(ID)
Expand Down
2 changes: 1 addition & 1 deletion api/businessservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h BusinessServiceHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h StakeholderGroupHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (h IdentityHandler) Update(ctx *gin.Context) {
m.ID = id
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
err = db.Updates(h.fields(m)).Error
err = db.Save(m).Error
if err != nil {
_ = ctx.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion api/jobfunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h JobFunctionHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/migrationwave.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h MigrationWaveHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (h ProxyHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
11 changes: 4 additions & 7 deletions api/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,16 @@ func (h QuestionnaireHandler) Update(ctx *gin.Context) {
updated := r.Model()
updated.ID = id
updated.UpdateUser = h.CurrentUser(ctx)
var fields map[string]any
if m.Builtin() {
fields = map[string]any{
"updateUser": updated.UpdateUser,
"required": updated.Required,
}
m.UpdateUser = updated.UpdateUser
m.Required = updated.Required
} else {
fields = h.fields(updated)
m = updated
}

db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result = db.Updates(fields)
result = db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
25 changes: 7 additions & 18 deletions api/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h ReviewHandler) AddRoutes(e *gin.Engine) {
routeGroup.GET(ReviewRoot, h.Get)
routeGroup.PUT(ReviewRoot, h.Update)
routeGroup.DELETE(ReviewRoot, h.Delete)
routeGroup.POST(CopyRoot, h.CopyReview)
routeGroup.POST(CopyRoot, h.CopyReview, Transaction)
}

// Get godoc
Expand Down Expand Up @@ -155,7 +155,7 @@ func (h ReviewHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down Expand Up @@ -194,26 +194,15 @@ func (h ReviewHandler) CopyReview(ctx *gin.Context) {
Comments: m.Comments,
ApplicationID: &id,
}
existing := []model.Review{}
result = h.DB(ctx).Find(&existing, "applicationid = ?", id)
result = h.DB(ctx).Delete(&model.Review{}, "applicationid = ?", id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
// if the application doesn't already have a review, create one.
if len(existing) == 0 {
result = h.DB(ctx).Create(copied)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
// if the application already has a review, replace it with the copied review.
} else {
result = h.DB(ctx).Model(&existing[0]).Updates(h.fields(copied))
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
result = h.DB(ctx).Create(copied)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
}
h.Status(ctx, http.StatusNoContent)
Expand Down
4 changes: 2 additions & 2 deletions api/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (h *RuleSetHandler) update(ctx *gin.Context, r *RuleSet) (err error) {
m.UpdateUser = h.CurrentUser(ctx)
db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
err = db.Updates(h.fields(m)).Error
err = db.Save(m).Error
if err != nil {
return
}
Expand All @@ -267,7 +267,7 @@ func (h *RuleSetHandler) update(ctx *gin.Context, r *RuleSet) (err error) {
for i := range m.Rules {
m := &m.Rules[i]
db = h.DB(ctx).Model(m)
err = db.Updates(h.fields(m)).Error
err = db.Updates(m).Error
if err != nil {
return
}
Expand Down
16 changes: 8 additions & 8 deletions api/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ func (h SettingHandler) Update(ctx *gin.Context) {
return
}

updates := Setting{}
updates.Key = key
err := h.Bind(ctx, &updates.Value)
m := &model.Setting{}
result := h.DB(ctx).First(m, "key = ?", key)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err := h.Bind(ctx, &m.Value)
if err != nil {
_ = ctx.Error(err)
return
}

m := updates.Model()
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Where("key", key)
result := db.Updates(h.fields(m))
result = h.DB(ctx).Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
}
Expand Down
2 changes: 1 addition & 1 deletion api/stakeholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h StakeholderHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h TagHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/tagcategory.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (h TagCategoryHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (h TargetHandler) Update(ctx *gin.Context) {
}
db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result = db.Updates(h.fields(m))
result = db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (h TrackerHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down

0 comments on commit d7c906e

Please sign in to comment.