Skip to content

Commit d867475

Browse files
committed
Merge the update logic
1 parent 2ea18d6 commit d867475

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

models/issues/comment.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,16 +1315,18 @@ func (c *Comment) HasOriginalAuthor() bool {
13151315
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
13161316
}
13171317

1318+
func UpdateIssueNumCommentsBuilder(issueID int64) *builder.Builder {
1319+
subQuery := builder.Select("COUNT(*)").From("`comment`").Where(
1320+
builder.Eq{"issue_id": issueID}.And(
1321+
builder.In("`type`", ConversationCountedCommentType()),
1322+
))
1323+
1324+
return builder.Update(builder.Eq{"num_comments": subQuery}).
1325+
From("`issue`").Where(builder.Eq{"id": issueID})
1326+
}
1327+
13181328
func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
1319-
countCommentsBuilder := builder.Select("count(*)").From("comment").Where(builder.Eq{
1320-
"issue_id": issueID,
1321-
}.And(builder.In("type", ConversationCountedCommentType())))
1322-
1323-
_, err := db.GetEngine(ctx).
1324-
SetExpr("num_comments", countCommentsBuilder).
1325-
ID(issueID).
1326-
NoAutoTime().
1327-
Update(new(Issue))
1329+
_, err := db.GetEngine(ctx).Exec(UpdateIssueNumCommentsBuilder(issueID))
13281330
return err
13291331
}
13301332

models/issues/review.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ func InsertReviews(ctx context.Context, reviews []*Review) error {
639639
return err
640640
}
641641
}
642+
643+
if err := UpdateIssueNumComments(ctx, review.IssueID); err != nil {
644+
return err
645+
}
642646
}
643647

644648
return committer.Commit()

models/repo.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,7 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
109109
}
110110

111111
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
112-
return StatsCorrectSQL(ctx,
113-
builder.Update(builder.Eq{
114-
"num_comments": builder.Select("COUNT(*)").From("`comment`").Where(
115-
builder.Eq{"issue_id": id}.And(
116-
builder.In("type", issues_model.ConversationCountedCommentType()),
117-
)),
118-
}).From("`issue`").Where(builder.Eq{"id": id}),
119-
)
112+
return StatsCorrectSQL(ctx, issues_model.UpdateIssueNumCommentsBuilder(id))
120113
}
121114

122115
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {

0 commit comments

Comments
 (0)