Pettier code to set the update time of comments

Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)

XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
This commit is contained in:
fluzz 2023-08-03 09:44:21 +02:00
parent da932152f1
commit 1f6a42808d

View file

@ -1106,24 +1106,19 @@ func UpdateComment(c *Comment, doer *user_model.User) error {
return err return err
} }
defer committer.Close() defer committer.Close()
sess := db.GetEngine(ctx).ID(c.ID).AllCols() sess := db.GetEngine(ctx).ID(c.ID).AllCols()
if _, err := sess.Update(c); err != nil {
return err
}
if c.Issue.NoAutoTime { if c.Issue.NoAutoTime {
// AllCols().Update() does not change the "update_unix" field // update the DataBase
// even if NoAutoTime is set. sess = sess.NoAutoTime().SetExpr("updated_unix", c.Issue.UpdatedUnix)
// So, we need to commit the former pending Update() and // the UpdatedUnix value of the Comment also has to be set,
// then call an other Update() specifically to set "updated_unix". // to return the adequate valuè
if err := committer.Commit(); err != nil { // see https://codeberg.org/forgejo/forgejo/pulls/764#issuecomment-1023801
return fmt.Errorf("Commit: %w", err)
}
c.UpdatedUnix = c.Issue.UpdatedUnix c.UpdatedUnix = c.Issue.UpdatedUnix
sess := db.GetEngine(ctx).ID(c.ID).Cols("updated_unix").NoAutoTime() }
if _, err := sess.Update(c); err != nil { if _, err := sess.Update(c); err != nil {
return err return err
} }
}
if err := c.LoadIssue(ctx); err != nil { if err := c.LoadIssue(ctx); err != nil {
return err return err
} }