summaryrefslogtreecommitdiffstats
path: root/models/migrations/v111.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-09-06 16:54:29 +0800
committerGitHub <noreply@github.com>2020-09-06 16:54:29 +0800
commit6590cac89bef910651ad2c27654e5e83766cf3f2 (patch)
treec3038bc5e6e7632925b8cf05a442d12774ebf3cb /models/migrations/v111.go
parent1c35353c53977bbb50e5089e20bcb0591305286f (diff)
downloadgitea-6590cac89bef910651ad2c27654e5e83766cf3f2.tar.gz
gitea-6590cac89bef910651ad2c27654e5e83766cf3f2.zip
Fix bug on migration 111 (#12736)
* Fix bug on migration 111 * Check reviews when migrating v111 * Fix bug * Fix commit
Diffstat (limited to 'models/migrations/v111.go')
-rw-r--r--models/migrations/v111.go44
1 files changed, 32 insertions, 12 deletions
diff --git a/models/migrations/v111.go b/models/migrations/v111.go
index 6a94298ddc..12eb2d2444 100644
--- a/models/migrations/v111.go
+++ b/models/migrations/v111.go
@@ -357,21 +357,18 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
return sess.Where("uid=?", reviewer.ID).In("team_id", protectedBranch.ApprovalsWhitelistTeamIDs).Exist(new(TeamUser))
}
- sess := x.NewSession()
- defer sess.Close()
-
- 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
}
- 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
}
- 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
}
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 {
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
reviews := []*Review{}
- var page int64
- for page = 0; page <= totalPages; page++ {
- 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)",
+
+ var executeBody = func(page, pageSize int64) error {
+ 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).
Find(&reviews); err != nil {
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 {
reviewer := new(User)
has, err := sess.ID(review.ReviewerID).Get(reviewer)
@@ -407,13 +416,24 @@ func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
continue
}
review.Official = official
-
+ updated++
if _, err := sess.ID(review.ID).Cols("official").Update(review); err != nil {
return err
}
}
+ if updated > 0 {
+ return sess.Commit()
+ }
+ return nil
+ }
+
+ var page int64
+ for page = 0; page <= totalPages; page++ {
+ if err := executeBody(page, pageSize); err != nil {
+ return err
+ }
}
- return sess.Commit()
+ return nil
}