Fix migration v111 (#12868)

This commit is contained in:
Lunny Xiao 2020-09-16 15:37:04 +08:00 committed by GitHub
parent 3a02f0896e
commit 62a3c847cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 17 deletions

View file

@ -11,7 +11,6 @@ import (
) )
func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error { func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
type ProtectedBranch struct { type ProtectedBranch struct {
CanPush bool `xorm:"NOT NULL DEFAULT false"` CanPush bool `xorm:"NOT NULL DEFAULT false"`
EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"` EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"`
@ -23,29 +22,26 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
Official bool `xorm:"NOT NULL DEFAULT false"` Official bool `xorm:"NOT NULL DEFAULT false"`
} }
sess := x.NewSession() if err := x.Sync2(new(ProtectedBranch)); err != nil {
defer sess.Close()
if err := sess.Sync2(new(ProtectedBranch)); err != nil {
return err return err
} }
if err := sess.Sync2(new(Review)); err != nil { if err := x.Sync2(new(Review)); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `protected_branch` SET `enable_whitelist` = ? WHERE enable_whitelist IS NULL", false); err != nil { if _, err := x.Exec("UPDATE `protected_branch` SET `enable_whitelist` = ? WHERE enable_whitelist IS NULL", false); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `protected_branch` SET `can_push` = `enable_whitelist`"); err != nil { if _, err := x.Exec("UPDATE `protected_branch` SET `can_push` = `enable_whitelist`"); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `protected_branch` SET `enable_approvals_whitelist` = ? WHERE `required_approvals` > ?", true, 0); err != nil { if _, err := x.Exec("UPDATE `protected_branch` SET `enable_approvals_whitelist` = ? WHERE `required_approvals` > ?", true, 0); err != nil {
return err return err
} }
var pageSize int64 = 20 var pageSize int64 = 20
qresult, err := sess.QueryInterface("SELECT max(id) as max_id FROM issue") qresult, err := x.QueryInterface("SELECT max(id) as max_id FROM issue")
if err != nil { if err != nil {
return err return err
} }
@ -57,10 +53,19 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
} }
totalPages := totalIssues / pageSize totalPages := totalIssues / pageSize
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
// Find latest review of each user in each pull request, and set official field if appropriate // Find latest review of each user in each pull request, and set official field if appropriate
reviews := []*models.Review{}
var page int64 var page int64
var count int
for page = 0; page <= totalPages; page++ { for page = 0; page <= totalPages; page++ {
reviews := []*models.Review{}
if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id > ? AND issue_id <= ? AND type in (?, ?) GROUP BY issue_id, reviewer_id)", if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id > ? AND issue_id <= ? AND type in (?, ?) GROUP BY issue_id, reviewer_id)",
page*pageSize, (page+1)*pageSize, models.ReviewTypeApprove, models.ReviewTypeReject). page*pageSize, (page+1)*pageSize, models.ReviewTypeApprove, models.ReviewTypeReject).
Find(&reviews); err != nil { Find(&reviews); err != nil {
@ -68,23 +73,37 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
} }
for _, review := range reviews { for _, review := range reviews {
if err := review.LoadAttributes(); err != nil { if err := review.LoadAttributesX(sess); err != nil {
// Error might occur if user or issue doesn't exist, ignore it. // Error might occur if user or issue doesn't exist, ignore it.
continue continue
} }
official, err := models.IsOfficialReviewer(review.Issue, review.Reviewer) official, err := models.IsOfficialReviewerX(sess, review.Issue, review.Reviewer)
if err != nil { if err != nil {
// Branch might not be proteced or other error, ignore it. // Branch might not be proteced or other error, ignore it.
continue continue
} }
review.Official = official review.Official = official
count++
if _, err := sess.ID(review.ID).Cols("official").Update(review); err != nil { if _, err := sess.ID(review.ID).Cols("official").Update(review); err != nil {
return err return err
} }
}
if count == 100 {
if err := sess.Commit(); err != nil {
return err
}
count = 0
if err := sess.Begin(); err != nil {
return err
}
}
}
} }
return sess.Commit() if count > 0 {
return sess.Commit()
}
return nil
} }

View file

@ -110,7 +110,8 @@ func (r *Review) LoadReviewer() error {
return r.loadReviewer(x) return r.loadReviewer(x)
} }
func (r *Review) loadAttributes(e Engine) (err error) { // LoadAttributesX loads all attributes except CodeComments with an Engine parameter
func (r *Review) LoadAttributesX(e Engine) (err error) {
if err = r.loadIssue(e); err != nil { if err = r.loadIssue(e); err != nil {
return return
} }
@ -125,7 +126,7 @@ func (r *Review) loadAttributes(e Engine) (err error) {
// LoadAttributes loads all attributes except CodeComments // LoadAttributes loads all attributes except CodeComments
func (r *Review) LoadAttributes() error { func (r *Review) LoadAttributes() error {
return r.loadAttributes(x) return r.LoadAttributesX(x)
} }
func getReviewByID(e Engine, id int64) (*Review, error) { func getReviewByID(e Engine, id int64) (*Review, error) {
@ -203,6 +204,12 @@ func IsOfficialReviewer(issue *Issue, reviewer *User) (bool, error) {
return isOfficialReviewer(x, issue, reviewer) return isOfficialReviewer(x, issue, reviewer)
} }
// IsOfficialReviewerX check if reviewer can make official reviews in issue (counts towards required approvals)
// with an Engine parameter
func IsOfficialReviewerX(e Engine, issue *Issue, reviewer *User) (bool, error) {
return isOfficialReviewer(x, issue, reviewer)
}
func isOfficialReviewer(e Engine, issue *Issue, reviewer *User) (bool, error) { func isOfficialReviewer(e Engine, issue *Issue, reviewer *User) (bool, error) {
pr, err := getPullRequestByIssueID(e, issue.ID) pr, err := getPullRequestByIssueID(e, issue.ID)
if err != nil { if err != nil {