Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code review on mssql #5502

Merged
merged 4 commits into from
Dec 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"

"github.com/go-xorm/builder"
Expand Down Expand Up @@ -266,13 +267,24 @@ type PullReviewersWithType struct {
// GetReviewersByPullID gets all reviewers for a pull request with the statuses
func GetReviewersByPullID(pullID int64) (issueReviewers []*PullReviewersWithType, err error) {
irs := []*PullReviewersWithType{}
err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
Table("review").
Join("INNER", "`user`", "review.reviewer_id = `user`.id").
Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)", pullID, ReviewTypeApprove, ReviewTypeReject).
GroupBy("`user`.id, review.type").
OrderBy("review_updated_unix DESC").
Find(&irs)
if x.Dialect().DBType() == core.MSSQL {
err = x.SQL(`SELECT [user].*, review.type, review.review_updated_unix FROM
(SELECT review.id, review.type, review.reviewer_id, max(review.updated_unix) as review_updated_unix
FROM review WHERE review.issue_id=? AND (review.type = ? OR review.type = ?)
GROUP BY review.id, review.type, review.reviewer_id) as review
INNER JOIN [user] ON review.reviewer_id = [user].id ORDER BY review_updated_unix DESC`,
pullID, ReviewTypeApprove, ReviewTypeReject).
Find(&irs)
} else {
err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
Table("review").
Join("INNER", "`user`", "review.reviewer_id = `user`.id").
Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)",
pullID, ReviewTypeApprove, ReviewTypeReject).
GroupBy("`user`.id, review.type").
OrderBy("review_updated_unix DESC").
Find(&irs)
}

// We need to group our results by user id _and_ review type, otherwise the query fails when using postgresql.
// But becaus we're doing this, we need to manually filter out multiple reviews of different types by the
Expand Down