Skip to content

Commit

Permalink
Constraint FindReviews with condition
Browse files Browse the repository at this point in the history
On the LatestOnly subquery, re-use the outter condition to avoid always
grouping by all reviews in the database.

Signed-off-by: Cory Todd <cory.todd@canonical.com>
  • Loading branch information
Cory Todd committed Jun 8, 2023
1 parent 970d85e commit bdba85b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion models/issues/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,4 @@ func TestGetApprovers(t *testing.T) {
approvers := pr.GetApprovers()
expected := "Reviewed-by: User Five <user5@example.com>\nReviewed-by: User Six <user6@example.com>\n"
assert.EqualValues(t, expected, approvers)
}
}
4 changes: 3 additions & 1 deletion models/issues/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,16 @@ func (opts *FindReviewOptions) toCond() builder.Cond {
// FindReviews returns reviews passing FindReviewOptions
func FindReviews(ctx context.Context, opts FindReviewOptions) ([]*Review, error) {
reviews := make([]*Review, 0, 10)
sess := db.GetEngine(ctx).Where(opts.toCond())
cond := opts.toCond()
sess := db.GetEngine(ctx).Where(cond)
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, &opts)
}
if opts.LatestOnly {
sess.In("id", builder.
Select("max ( id ) ").
From("review").
Where(cond).
GroupBy("reviewer_id"))
}
return reviews, sess.
Expand Down

0 comments on commit bdba85b

Please sign in to comment.