Skip to content

Commit 9c4d250

Browse files
committed
Merge branch 'lunny/fix_issue_comment_num' into lunny/fix_issue_comment_num2
2 parents e69da2c + 3e52622 commit 9c4d250

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

models/issues/comment.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
13041324
func 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
}

models/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
106106
}
107107

108108
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
109-
return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?", id)
109+
return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND (type=0 or type=22)) WHERE id=?", id)
110110
}
111111

112112
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
@@ -198,7 +198,7 @@ func CheckRepoStats(ctx context.Context) error {
198198
},
199199
// Issue.NumComments
200200
{
201-
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)"),
201+
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND (type=0 OR type=22))"),
202202
repoStatsCorrectIssueNumComments,
203203
"issue count 'num_comments'",
204204
},

0 commit comments

Comments
 (0)