Skip to content

Commit 16263af

Browse files
GiteaBot6543
andauthored
Fix inperformant query on retrifing review from database. (go-gitea#28552) (go-gitea#28562)
Backport go-gitea#28552 by @6543 can we please PLEAS PLEASE only use raw SQL statements if it is relay needed!!! source is go-gitea#28544 (before refactoring) Co-authored-by: 6543 <m.huber@kithara.com>
1 parent f096635 commit 16263af

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

models/issues/review.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,10 @@ func SubmitReview(ctx context.Context, doer *user_model.User, issue *Issue, revi
461461
func GetReviewByIssueIDAndUserID(ctx context.Context, issueID, userID int64) (*Review, error) {
462462
review := new(Review)
463463

464-
has, err := db.GetEngine(ctx).SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_id = ? AND original_author_id = 0 AND type in (?, ?, ?))",
465-
issueID, userID, ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest).
464+
has, err := db.GetEngine(ctx).Where(
465+
builder.In("type", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest).
466+
And(builder.Eq{"issue_id": issueID, "reviewer_id": userID, "original_author_id": 0})).
467+
Desc("id").
466468
Get(review)
467469
if err != nil {
468470
return nil, err
@@ -476,13 +478,13 @@ func GetReviewByIssueIDAndUserID(ctx context.Context, issueID, userID int64) (*R
476478
}
477479

478480
// GetTeamReviewerByIssueIDAndTeamID get the latest review request of reviewer team for a pull request
479-
func GetTeamReviewerByIssueIDAndTeamID(ctx context.Context, issueID, teamID int64) (review *Review, err error) {
480-
review = new(Review)
481+
func GetTeamReviewerByIssueIDAndTeamID(ctx context.Context, issueID, teamID int64) (*Review, error) {
482+
review := new(Review)
481483

482-
var has bool
483-
if has, err = db.GetEngine(ctx).SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = ?)",
484-
issueID, teamID).
485-
Get(review); err != nil {
484+
has, err := db.GetEngine(ctx).Where(builder.Eq{"issue_id": issueID, "reviewer_team_id": teamID}).
485+
Desc("id").
486+
Get(review)
487+
if err != nil {
486488
return nil, err
487489
}
488490

0 commit comments

Comments
 (0)