Browse Source

models/protect_branch: fix whitelist with invalid 'protect_branch_id' (#4333)

If user creates a protect branch for the first time (which has ID=0),
it generates invalid whitelist records with 'protect_branch_id=0'.
This prevents future updates of protect branch whitelist.

Migration: remove existing invalid protect branch whitelist records.
pull/4353/head
Unknwon 8 years ago
parent
commit
1038916460
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 2
      models/migrations/migrations.go
  3. 14
      models/migrations/v17.go
  4. 13
      models/repo_branch.go
  5. 2
      templates/.VERSION

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.10.30.0325" const APP_VER = "0.10.31.0327"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

2
models/migrations/migrations.go

@ -62,6 +62,8 @@ var migrations = []Migration{
NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks), NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks),
// v15 -> v16:v0.10.16 // v15 -> v16:v0.10.16
NewMigration("update repository sizes", updateRepositorySizes), NewMigration("update repository sizes", updateRepositorySizes),
// v16 -> v17:v0.10.31
NewMigration("remove invalid protect branch whitelist", removeInvalidProtectBranchWhitelist),
} }
// Migrate database to current version // Migrate database to current version

14
models/migrations/v17.go

@ -0,0 +1,14 @@
// Copyright 2017 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"github.com/go-xorm/xorm"
)
func removeInvalidProtectBranchWhitelist(x *xorm.Engine) error {
_, err := x.Exec("DELETE FROM protect_branch_whitelist WHERE protect_branch_id = 0")
return err
}

13
models/repo_branch.go

@ -187,6 +187,13 @@ func UpdateOrgProtectBranch(repo *Repository, protectBranch *ProtectBranch, whit
protectBranch.WhitelistTeamIDs = strings.Join(base.Int64sToStrings(validTeamIDs), ",") protectBranch.WhitelistTeamIDs = strings.Join(base.Int64sToStrings(validTeamIDs), ",")
} }
// Make sure protectBranch.ID is not 0 for whitelists
if protectBranch.ID == 0 {
if _, err = x.Insert(protectBranch); err != nil {
return fmt.Errorf("Insert: %v", err)
}
}
// Merge users and members of teams // Merge users and members of teams
var whitelists []*ProtectBranchWhitelist var whitelists []*ProtectBranchWhitelist
if hasUsersChanged || hasTeamsChanged { if hasUsersChanged || hasTeamsChanged {
@ -226,12 +233,6 @@ func UpdateOrgProtectBranch(repo *Repository, protectBranch *ProtectBranch, whit
return err return err
} }
if protectBranch.ID == 0 {
if _, err = sess.Insert(protectBranch); err != nil {
return fmt.Errorf("Insert: %v", err)
}
}
if _, err = sess.Id(protectBranch.ID).AllCols().Update(protectBranch); err != nil { if _, err = sess.Id(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
return fmt.Errorf("Update: %v", err) return fmt.Errorf("Update: %v", err)
} }

2
templates/.VERSION

@ -1 +1 @@
0.10.30.0325 0.10.31.0327
Loading…
Cancel
Save