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