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.

69 lines
1.7 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. "fmt"
  7. "xorm.io/xorm"
  8. )
  9. func removeDuplicateUnitTypes(x *xorm.Engine) error {
  10. // RepoUnit describes all units of a repository
  11. type RepoUnit struct {
  12. RepoID int64
  13. Type int
  14. }
  15. // Enumerate all the unit types
  16. const (
  17. UnitTypeCode = iota + 1 // 1 code
  18. UnitTypeIssues // 2 issues
  19. UnitTypePullRequests // 3 PRs
  20. UnitTypeReleases // 4 Releases
  21. UnitTypeWiki // 5 Wiki
  22. UnitTypeExternalWiki // 6 ExternalWiki
  23. UnitTypeExternalTracker // 7 ExternalTracker
  24. )
  25. var externalIssueRepoUnits []RepoUnit
  26. err := x.Where("type = ?", UnitTypeExternalTracker).Find(&externalIssueRepoUnits)
  27. if err != nil {
  28. return fmt.Errorf("Query repositories: %v", err)
  29. }
  30. var externalWikiRepoUnits []RepoUnit
  31. err = x.Where("type = ?", UnitTypeExternalWiki).Find(&externalWikiRepoUnits)
  32. if err != nil {
  33. return fmt.Errorf("Query repositories: %v", err)
  34. }
  35. sess := x.NewSession()
  36. defer sess.Close()
  37. if err := sess.Begin(); err != nil {
  38. return err
  39. }
  40. for _, repoUnit := range externalIssueRepoUnits {
  41. if _, err = sess.Delete(&RepoUnit{
  42. RepoID: repoUnit.RepoID,
  43. Type: UnitTypeIssues,
  44. }); err != nil {
  45. return fmt.Errorf("Delete repo unit: %v", err)
  46. }
  47. }
  48. for _, repoUnit := range externalWikiRepoUnits {
  49. if _, err = sess.Delete(&RepoUnit{
  50. RepoID: repoUnit.RepoID,
  51. Type: UnitTypeWiki,
  52. }); err != nil {
  53. return fmt.Errorf("Delete repo unit: %v", err)
  54. }
  55. }
  56. return sess.Commit()
  57. }