From 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2 Mon Sep 17 00:00:00 2001 From: fluzz Date: Thu, 3 Aug 2023 09:44:21 +0200 Subject: [PATCH] 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. --- models/issues/comment.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index a4b5dec5b7..e730308704 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -1106,24 +1106,19 @@ func UpdateComment(c *Comment, doer *user_model.User) error { return err } defer committer.Close() + sess := db.GetEngine(ctx).ID(c.ID).AllCols() + if c.Issue.NoAutoTime { + // update the DataBase + sess = sess.NoAutoTime().SetExpr("updated_unix", c.Issue.UpdatedUnix) + // the UpdatedUnix value of the Comment also has to be set, + // to return the adequate valuè + // see https://codeberg.org/forgejo/forgejo/pulls/764#issuecomment-1023801 + c.UpdatedUnix = c.Issue.UpdatedUnix + } if _, err := sess.Update(c); err != nil { return err } - if c.Issue.NoAutoTime { - // AllCols().Update() does not change the "update_unix" field - // even if NoAutoTime is set. - // So, we need to commit the former pending Update() and - // then call an other Update() specifically to set "updated_unix". - if err := committer.Commit(); err != nil { - return fmt.Errorf("Commit: %w", err) - } - c.UpdatedUnix = c.Issue.UpdatedUnix - sess := db.GetEngine(ctx).ID(c.ID).Cols("updated_unix").NoAutoTime() - if _, err := sess.Update(c); err != nil { - return err - } - } if err := c.LoadIssue(ctx); err != nil { return err }