summaryrefslogtreecommitdiffstats
path: root/models/migrations
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-07-17 05:04:43 +0300
committerLunny Xiao <xiaolunwen@gmail.com>2017-07-17 10:04:43 +0800
commitf33e6ae09ec27e898bb8f2cc44d587ea3b8e0dee (patch)
tree2c6a268356fcb5fd73ee468055869b129b14c6ed /models/migrations
parent047a67a90b455a077e8b6f6deaad6b7ec4b50810 (diff)
downloadgitea-f33e6ae09ec27e898bb8f2cc44d587ea3b8e0dee.tar.gz
gitea-f33e6ae09ec27e898bb8f2cc44d587ea3b8e0dee.zip
Remove unit types commits and settings (#2161)
* Remove unit types commits and settings * Can not limit units in administrator teams * Limit changing units only to teams with read and write access mode * Small code optimization
Diffstat (limited to 'models/migrations')
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v16.go32
-rw-r--r--models/migrations/v38.go56
3 files changed, 74 insertions, 16 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index e5b6473687..45c410c4d4 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -124,6 +124,8 @@ var migrations = []Migration{
NewMigration("regenerate git hooks", regenerateGitHooks36),
// v37 -> v38
NewMigration("unescape user full names", unescapeUserFullNames),
+ // v38 -> v39
+ NewMigration("remove commits and settings unit types", removeCommitsUnitType),
}
// Migrate database to current version
diff --git a/models/migrations/v16.go b/models/migrations/v16.go
index 12cad46d09..9cd4ef4da2 100644
--- a/models/migrations/v16.go
+++ b/models/migrations/v16.go
@@ -26,15 +26,15 @@ type RepoUnit struct {
// Enumerate all the unit types
const (
- UnitTypeCode = iota + 1 // 1 code
- UnitTypeIssues // 2 issues
- UnitTypePRs // 3 PRs
- UnitTypeCommits // 4 Commits
- UnitTypeReleases // 5 Releases
- UnitTypeWiki // 6 Wiki
- UnitTypeSettings // 7 Settings
- UnitTypeExternalWiki // 8 ExternalWiki
- UnitTypeExternalTracker // 9 ExternalTracker
+ V16UnitTypeCode = iota + 1 // 1 code
+ V16UnitTypeIssues // 2 issues
+ V16UnitTypePRs // 3 PRs
+ V16UnitTypeCommits // 4 Commits
+ V16UnitTypeReleases // 5 Releases
+ V16UnitTypeWiki // 6 Wiki
+ V16UnitTypeSettings // 7 Settings
+ V16UnitTypeExternalWiki // 8 ExternalWiki
+ V16UnitTypeExternalTracker // 9 ExternalTracker
)
// Repo describes a repository
@@ -79,32 +79,32 @@ func addUnitsToTables(x *xorm.Engine) error {
for _, repo := range repos {
for i := 1; i <= 9; i++ {
- if (i == UnitTypeWiki || i == UnitTypeExternalWiki) && !repo.EnableWiki {
+ if (i == V16UnitTypeWiki || i == V16UnitTypeExternalWiki) && !repo.EnableWiki {
continue
}
- if i == UnitTypeExternalWiki && !repo.EnableExternalWiki {
+ if i == V16UnitTypeExternalWiki && !repo.EnableExternalWiki {
continue
}
- if i == UnitTypePRs && !repo.EnablePulls {
+ if i == V16UnitTypePRs && !repo.EnablePulls {
continue
}
- if (i == UnitTypeIssues || i == UnitTypeExternalTracker) && !repo.EnableIssues {
+ if (i == V16UnitTypeIssues || i == V16UnitTypeExternalTracker) && !repo.EnableIssues {
continue
}
- if i == UnitTypeExternalTracker && !repo.EnableExternalTracker {
+ if i == V16UnitTypeExternalTracker && !repo.EnableExternalTracker {
continue
}
var config = make(map[string]string)
switch i {
- case UnitTypeExternalTracker:
+ case V16UnitTypeExternalTracker:
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
if len(repo.ExternalTrackerStyle) == 0 {
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
}
config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
- case UnitTypeExternalWiki:
+ case V16UnitTypeExternalWiki:
config["ExternalWikiURL"] = repo.ExternalWikiURL
}
diff --git a/models/migrations/v38.go b/models/migrations/v38.go
new file mode 100644
index 0000000000..69c346335e
--- /dev/null
+++ b/models/migrations/v38.go
@@ -0,0 +1,56 @@
+// Copyright 2017 The Gitea 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 (
+ "code.gitea.io/gitea/models"
+
+ "github.com/go-xorm/xorm"
+)
+
+func removeCommitsUnitType(x *xorm.Engine) (err error) {
+ // Update team unit types
+ const batchSize = 100
+ for start := 0; ; start += batchSize {
+ teams := make([]*models.Team, 0, batchSize)
+ if err := x.Limit(batchSize, start).Find(&teams); err != nil {
+ return err
+ }
+ if len(teams) == 0 {
+ break
+ }
+ for _, team := range teams {
+ ut := make([]models.UnitType, 0, len(team.UnitTypes))
+ for _, u := range team.UnitTypes {
+ if u < V16UnitTypeCommits {
+ ut = append(ut, u)
+ } else if u > V16UnitTypeSettings {
+ ut = append(ut, u-2)
+ } else if u > V16UnitTypeCommits && u != V16UnitTypeSettings {
+ ut = append(ut, u-1)
+ }
+ }
+ team.UnitTypes = ut
+ if _, err := x.Id(team.ID).Cols("unit_types").Update(team); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Delete commits and settings unit types
+ if _, err = x.In("`type`", []models.UnitType{V16UnitTypeCommits, V16UnitTypeSettings}).Delete(new(RepoUnit)); err != nil {
+ return err
+ }
+ // Fix renumber unit types that where in enumeration after settings unit type
+ if _, err = x.Where("`type` > ?", V16UnitTypeSettings).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
+ return err
+ }
+ // Fix renumber unit types that where in enumeration after commits unit type
+ if _, err = x.Where("`type` > ?", V16UnitTypeCommits).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
+ return err
+ }
+
+ return nil
+}