Browse Source

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 <dschneiderbauer@gmail.com>
David Schneiderbauer 2 years ago
parent
commit
26e49b8e3d
2 changed files with 71 additions and 0 deletions
  1. 2 0
      models/migrations/migrations.go
  2. 69 0
      models/migrations/v44.go

+ 2 - 0
models/migrations/migrations.go

@@ -136,6 +136,8 @@ var migrations = []Migration{
136 136
 	NewMigration("add tags to releases and sync existing repositories", releaseAddColumnIsTagAndSyncTags),
137 137
 	// v43 -> v44
138 138
 	NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
139
+	// v44 -> v45
140
+	NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
139 141
 }
140 142
 
141 143
 // Migrate database to current version

+ 69 - 0
models/migrations/v44.go

@@ -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
+}