You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v38.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "github.com/go-xorm/core"
  9. "github.com/go-xorm/xorm"
  10. )
  11. func removeCommitsUnitType(x *xorm.Engine) (err error) {
  12. // RepoUnit describes all units of a repository
  13. type RepoUnit struct {
  14. ID int64
  15. RepoID int64 `xorm:"INDEX(s)"`
  16. Type int `xorm:"INDEX(s)"`
  17. Index int
  18. Config core.Conversion `xorm:"TEXT"`
  19. CreatedUnix int64 `xorm:"INDEX CREATED"`
  20. Created time.Time `xorm:"-"`
  21. }
  22. // Update team unit types
  23. const batchSize = 100
  24. for start := 0; ; start += batchSize {
  25. teams := make([]*models.Team, 0, batchSize)
  26. if err := x.Limit(batchSize, start).Find(&teams); err != nil {
  27. return err
  28. }
  29. if len(teams) == 0 {
  30. break
  31. }
  32. for _, team := range teams {
  33. ut := make([]models.UnitType, 0, len(team.UnitTypes))
  34. for _, u := range team.UnitTypes {
  35. if u < V16UnitTypeCommits {
  36. ut = append(ut, u)
  37. } else if u > V16UnitTypeSettings {
  38. ut = append(ut, u-2)
  39. } else if u > V16UnitTypeCommits && u != V16UnitTypeSettings {
  40. ut = append(ut, u-1)
  41. }
  42. }
  43. team.UnitTypes = ut
  44. if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
  45. return err
  46. }
  47. }
  48. }
  49. // Delete commits and settings unit types
  50. if _, err = x.In("`type`", []models.UnitType{V16UnitTypeCommits, V16UnitTypeSettings}).Delete(new(RepoUnit)); err != nil {
  51. return err
  52. }
  53. // Fix renumber unit types that where in enumeration after settings unit type
  54. if _, err = x.Where("`type` > ?", V16UnitTypeSettings).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
  55. return err
  56. }
  57. // Fix renumber unit types that where in enumeration after commits unit type
  58. if _, err = x.Where("`type` > ?", V16UnitTypeCommits).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
  59. return err
  60. }
  61. return nil
  62. }