2017-07-16 22:04:43 -04:00
|
|
|
// 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 (
|
2017-10-08 07:08:18 -04:00
|
|
|
"time"
|
|
|
|
|
2017-07-16 22:04:43 -04:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
"xorm.io/core"
|
2019-10-17 05:26:49 -04:00
|
|
|
"xorm.io/xorm"
|
2017-07-16 22:04:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func removeCommitsUnitType(x *xorm.Engine) (err error) {
|
2017-10-08 07:08:18 -04:00
|
|
|
// RepoUnit describes all units of a repository
|
|
|
|
type RepoUnit struct {
|
|
|
|
ID int64
|
|
|
|
RepoID int64 `xorm:"INDEX(s)"`
|
|
|
|
Type int `xorm:"INDEX(s)"`
|
|
|
|
Index int
|
|
|
|
Config core.Conversion `xorm:"TEXT"`
|
|
|
|
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
|
|
|
Created time.Time `xorm:"-"`
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:00:13 -04:00
|
|
|
type Team struct {
|
|
|
|
ID int64
|
|
|
|
UnitTypes []int `xorm:"json"`
|
|
|
|
}
|
|
|
|
|
2017-07-16 22:04:43 -04:00
|
|
|
// Update team unit types
|
|
|
|
const batchSize = 100
|
|
|
|
for start := 0; ; start += batchSize {
|
2018-06-21 12:00:13 -04:00
|
|
|
teams := make([]*Team, 0, batchSize)
|
2017-07-16 22:04:43 -04:00
|
|
|
if err := x.Limit(batchSize, start).Find(&teams); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(teams) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, team := range teams {
|
2018-06-21 12:00:13 -04:00
|
|
|
ut := make([]int, 0, len(team.UnitTypes))
|
2017-07-16 22:04:43 -04:00
|
|
|
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
|
2017-10-05 00:43:04 -04:00
|
|
|
if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
|
2017-07-16 22:04:43 -04:00
|
|
|
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
|
|
|
|
}
|