Skip to content

Commit 26e49b8

Browse files
daviianlafriks
authored andcommitted
Fix doubled issue tab introduced in migration v16 (#2611)
* fix duplicate issue tab as UnitTypeIssue and UnitTypeExternalTracker are existing at the same time Signed-off-by: David Schneiderbauer <[email protected]>
1 parent cc84ca4 commit 26e49b8

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ var migrations = []Migration{
136136
NewMigration("add tags to releases and sync existing repositories", releaseAddColumnIsTagAndSyncTags),
137137
// v43 -> v44
138138
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
139+
// v44 -> v45
140+
NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
139141
}
140142

141143
// Migrate database to current version

models/migrations/v44.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)