@@ -893,7 +893,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
893893 }
894894 fallthrough
895895 case CommentTypeComment :
896- if _ , err = db . Exec (ctx , "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?" , opts .Issue .ID ); err != nil {
896+ if err := UpdateIssueNumComments (ctx , opts .Issue .ID ); err != nil {
897897 return err
898898 }
899899 fallthrough
@@ -1182,8 +1182,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11821182 return err
11831183 }
11841184
1185- if comment .Type == CommentTypeComment {
1186- if _ , err := e . ID ( comment .IssueID ). Decr ( "num_comments" ). Update ( new ( Issue ) ); err != nil {
1185+ if comment .Type == CommentTypeComment || comment . Type == CommentTypeReview {
1186+ if err := UpdateIssueNumComments ( ctx , comment .IssueID ); err != nil {
11871187 return err
11881188 }
11891189 }
@@ -1300,6 +1300,26 @@ func (c *Comment) HasOriginalAuthor() bool {
13001300 return c .OriginalAuthor != "" && c .OriginalAuthorID != 0
13011301}
13021302
1303+ func CountCommentsBuilder (issueID int64 ) * builder.Builder {
1304+ return builder .Select ("count(*)" ).From ("comment" ).Where (builder.Eq {
1305+ "issue_id" : issueID ,
1306+ }.And (builder .Or (
1307+ builder.Eq {"type" : CommentTypeComment },
1308+ builder.Eq {"type" : CommentTypeReview },
1309+ builder.Eq {"type" : CommentTypeCode },
1310+ )).And (builder.Neq {
1311+ "invalidated" : true ,
1312+ }))
1313+ }
1314+
1315+ func UpdateIssueNumComments (ctx context.Context , issueID int64 ) error {
1316+ _ , err := db .GetEngine (ctx ).
1317+ SetExpr ("num_comments" , CountCommentsBuilder (issueID )).
1318+ ID (issueID ).
1319+ Update (new (Issue ))
1320+ return err
1321+ }
1322+
13031323// InsertIssueComments inserts many comments of issues.
13041324func InsertIssueComments (ctx context.Context , comments []* Comment ) error {
13051325 if len (comments ) == 0 {
@@ -1332,8 +1352,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
13321352 }
13331353
13341354 for _ , issueID := range issueIDs {
1335- if _ , err := db .Exec (ctx , "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?" ,
1336- issueID , CommentTypeComment , issueID ); err != nil {
1355+ if err := UpdateIssueNumComments (ctx , issueID ); err != nil {
13371356 return err
13381357 }
13391358 }
0 commit comments