@@ -868,7 +868,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
868
868
}
869
869
fallthrough
870
870
case CommentTypeComment :
871
- if _ , err = db . Exec (ctx , "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?" , opts .Issue .ID ); err != nil {
871
+ if err := UpdateIssueNumComments (ctx , opts .Issue .ID ); err != nil {
872
872
return err
873
873
}
874
874
fallthrough
@@ -1148,8 +1148,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
1148
1148
return err
1149
1149
}
1150
1150
1151
- if comment .Type == CommentTypeComment {
1152
- if _ , err := e . ID ( comment .IssueID ). Decr ( "num_comments" ). Update ( new ( Issue ) ); err != nil {
1151
+ if comment .Type == CommentTypeComment || comment . Type == CommentTypeReview {
1152
+ if err := UpdateIssueNumComments ( ctx , comment .IssueID ); err != nil {
1153
1153
return err
1154
1154
}
1155
1155
}
@@ -1266,6 +1266,26 @@ func (c *Comment) HasOriginalAuthor() bool {
1266
1266
return c .OriginalAuthor != "" && c .OriginalAuthorID != 0
1267
1267
}
1268
1268
1269
+ func CountCommentsBuilder (issueID int64 ) * builder.Builder {
1270
+ return builder .Select ("count(*)" ).From ("comment" ).Where (builder.Eq {
1271
+ "issue_id" : issueID ,
1272
+ }.And (builder .Or (
1273
+ builder.Eq {"type" : CommentTypeComment },
1274
+ builder.Eq {"type" : CommentTypeReview },
1275
+ builder.Eq {"type" : CommentTypeCode },
1276
+ )).And (builder.Neq {
1277
+ "invalidated" : true ,
1278
+ }))
1279
+ }
1280
+
1281
+ func UpdateIssueNumComments (ctx context.Context , issueID int64 ) error {
1282
+ _ , err := db .GetEngine (ctx ).
1283
+ SetExpr ("num_comments" , CountCommentsBuilder (issueID )).
1284
+ ID (issueID ).
1285
+ Update (new (Issue ))
1286
+ return err
1287
+ }
1288
+
1269
1289
// InsertIssueComments inserts many comments of issues.
1270
1290
func InsertIssueComments (ctx context.Context , comments []* Comment ) error {
1271
1291
if len (comments ) == 0 {
@@ -1298,8 +1318,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
1298
1318
}
1299
1319
1300
1320
for _ , issueID := range issueIDs {
1301
- if _ , err := db .Exec (ctx , "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?" ,
1302
- issueID , CommentTypeComment , issueID ); err != nil {
1321
+ if err := UpdateIssueNumComments (ctx , issueID ); err != nil {
1303
1322
return err
1304
1323
}
1305
1324
}
0 commit comments