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.

75 lines
2.0 KiB

  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/xorm"
  9. "xorm.io/core"
  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. type Team struct {
  23. ID int64
  24. UnitTypes []int `xorm:"json"`
  25. }
  26. // Update team unit types
  27. const batchSize = 100
  28. for start := 0; ; start += batchSize {
  29. teams := make([]*Team, 0, batchSize)
  30. if err := x.Limit(batchSize, start).Find(&teams); err != nil {
  31. return err
  32. }
  33. if len(teams) == 0 {
  34. break
  35. }
  36. for _, team := range teams {
  37. ut := make([]int, 0, len(team.UnitTypes))
  38. for _, u := range team.UnitTypes {
  39. if u < V16UnitTypeCommits {
  40. ut = append(ut, u)
  41. } else if u > V16UnitTypeSettings {
  42. ut = append(ut, u-2)
  43. } else if u > V16UnitTypeCommits && u != V16UnitTypeSettings {
  44. ut = append(ut, u-1)
  45. }
  46. }
  47. team.UnitTypes = ut
  48. if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. // Delete commits and settings unit types
  54. if _, err = x.In("`type`", []models.UnitType{V16UnitTypeCommits, V16UnitTypeSettings}).Delete(new(RepoUnit)); err != nil {
  55. return err
  56. }
  57. // Fix renumber unit types that where in enumeration after settings unit type
  58. if _, err = x.Where("`type` > ?", V16UnitTypeSettings).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
  59. return err
  60. }
  61. // Fix renumber unit types that where in enumeration after commits unit type
  62. if _, err = x.Where("`type` > ?", V16UnitTypeCommits).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
  63. return err
  64. }
  65. return nil
  66. }