Fix bug on migration 111 (#12736)

* Fix bug on migration 111

* Check reviews when migrating v111

* Fix bug

* Fix commit
This commit is contained in:
Lunny Xiao 2020-09-06 16:54:29 +08:00 committed by GitHub
parent 1c35353c53
commit 6590cac89b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -357,21 +357,18 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
return sess.Where("uid=?", reviewer.ID).In("team_id", protectedBranch.ApprovalsWhitelistTeamIDs).Exist(new(TeamUser)) return sess.Where("uid=?", reviewer.ID).In("team_id", protectedBranch.ApprovalsWhitelistTeamIDs).Exist(new(TeamUser))
} }
sess := x.NewSession() if _, err := x.Exec("UPDATE `protected_branch` SET `enable_whitelist` = ? WHERE enable_whitelist IS NULL", false); err != nil {
defer sess.Close()
if _, err := sess.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
} }
@ -385,14 +382,26 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
// 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 := []*Review{} reviews := []*Review{}
var page int64
for page = 0; page <= totalPages; page++ { var executeBody = func(page, pageSize int64) error {
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 := x.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, ReviewTypeApprove, ReviewTypeReject). page*pageSize, (page+1)*pageSize, ReviewTypeApprove, ReviewTypeReject).
Find(&reviews); err != nil { Find(&reviews); err != nil {
return err return err
} }
if len(reviews) == 0 {
return nil
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
var updated int
for _, review := range reviews { for _, review := range reviews {
reviewer := new(User) reviewer := new(User)
has, err := sess.ID(review.ReviewerID).Get(reviewer) has, err := sess.ID(review.ReviewerID).Get(reviewer)
@ -407,13 +416,24 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
continue continue
} }
review.Official = official review.Official = official
updated++
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 updated > 0 {
return sess.Commit() return sess.Commit()
} }
return nil
}
var page int64
for page = 0; page <= totalPages; page++ {
if err := executeBody(page, pageSize); err != nil {
return err
}
}
return nil
}