Browse Source

Remove repo unit index (#2621)

* Remove repo unit index

* Fix sqlite
Morlinest 2 years ago
parent
commit
a04718a2a6

+ 0 - 10
models/fixtures/repo_unit.yml

@@ -2,7 +2,6 @@
2 2
   id: 1
3 3
   repo_id: 1
4 4
   type: 4
5
-  index: 3
6 5
   config: "{}"
7 6
   created_unix: 946684810
8 7
 
@@ -10,7 +9,6 @@
10 9
   id: 2
11 10
   repo_id: 1
12 11
   type: 5
13
-  index: 4
14 12
   config: "{}"
15 13
   created_unix: 946684810
16 14
 
@@ -18,7 +16,6 @@
18 16
   id: 3
19 17
   repo_id: 1
20 18
   type: 1
21
-  index: 0
22 19
   config: "{}"
23 20
   created_unix: 946684810
24 21
 
@@ -26,7 +23,6 @@
26 23
   id: 4
27 24
   repo_id: 1
28 25
   type: 2
29
-  index: 1
30 26
   config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
31 27
   created_unix: 946684810
32 28
 
@@ -34,7 +30,6 @@
34 30
   id: 5
35 31
   repo_id: 1
36 32
   type: 3
37
-  index: 2
38 33
   config: "{}"
39 34
   created_unix: 946684810
40 35
 
@@ -42,7 +37,6 @@
42 37
   id: 6
43 38
   repo_id: 3
44 39
   type: 1
45
-  index: 0
46 40
   config: "{}"
47 41
   created_unix: 946684810
48 42
 
@@ -50,7 +44,6 @@
50 44
   id: 7
51 45
   repo_id: 3
52 46
   type: 2
53
-  index: 1
54 47
   config: "{\"EnableTimetracker\":false,\"AllowOnlyContributorsToTrackTime\":false}"
55 48
   created_unix: 946684810
56 49
 
@@ -58,7 +51,6 @@
58 51
   id: 8
59 52
   repo_id: 3
60 53
   type: 3
61
-  index: 2
62 54
   config: "{}"
63 55
   created_unix: 946684810
64 56
 
@@ -66,7 +58,6 @@
66 58
   id: 9
67 59
   repo_id: 3
68 60
   type: 4
69
-  index: 3
70 61
   config: "{}"
71 62
   created_unix: 946684810
72 63
 
@@ -74,6 +65,5 @@
74 65
   id: 10
75 66
   repo_id: 3
76 67
   type: 5
77
-  index: 4
78 68
   config: "{}"
79 69
   created_unix: 946684810

+ 2 - 0
models/migrations/migrations.go

@@ -138,6 +138,8 @@ var migrations = []Migration{
138 138
 	NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
139 139
 	// v44 -> v45
140 140
 	NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
141
+	// v45 -> v46
142
+	NewMigration("remove index column from repo_unit table", removeIndexColumnFromRepoUnitTable),
141 143
 }
142 144
 
143 145
 // Migrate database to current version

+ 28 - 0
models/migrations/v45.go

@@ -0,0 +1,28 @@
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
+	"code.gitea.io/gitea/modules/log"
11
+	"code.gitea.io/gitea/modules/setting"
12
+	"github.com/go-xorm/xorm"
13
+)
14
+
15
+func removeIndexColumnFromRepoUnitTable(x *xorm.Engine) (err error) {
16
+	switch {
17
+	case setting.UseSQLite3:
18
+		log.Warn("Unable to drop columns in SQLite")
19
+	case setting.UseMySQL, setting.UsePostgreSQL, setting.UseMSSQL, setting.UseTiDB:
20
+		if _, err := x.Exec("ALTER TABLE repo_unit DROP COLUMN index"); err != nil {
21
+			return fmt.Errorf("DROP COLUMN index: %v", err)
22
+		}
23
+	default:
24
+		log.Fatal(4, "Unrecognized DB")
25
+	}
26
+
27
+	return nil
28
+}

+ 1 - 3
models/repo.go

@@ -1245,19 +1245,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
1245 1245
 
1246 1246
 	// insert units for repo
1247 1247
 	var units = make([]RepoUnit, 0, len(defaultRepoUnits))
1248
-	for i, tp := range defaultRepoUnits {
1248
+	for _, tp := range defaultRepoUnits {
1249 1249
 		if tp == UnitTypeIssues {
1250 1250
 			units = append(units, RepoUnit{
1251 1251
 				RepoID: repo.ID,
1252 1252
 				Type:   tp,
1253
-				Index:  i,
1254 1253
 				Config: &IssuesConfig{EnableTimetracker: setting.Service.DefaultEnableTimetracking, AllowOnlyContributorsToTrackTime: setting.Service.DefaultAllowOnlyContributorsToTrackTime},
1255 1254
 			})
1256 1255
 		} else {
1257 1256
 			units = append(units, RepoUnit{
1258 1257
 				RepoID: repo.ID,
1259 1258
 				Type:   tp,
1260
-				Index:  i,
1261 1259
 			})
1262 1260
 		}
1263 1261
 

+ 2 - 3
models/repo_unit.go

@@ -16,9 +16,8 @@ import (
16 16
 // RepoUnit describes all units of a repository
17 17
 type RepoUnit struct {
18 18
 	ID          int64
19
-	RepoID      int64    `xorm:"INDEX(s)"`
20
-	Type        UnitType `xorm:"INDEX(s)"`
21
-	Index       int
19
+	RepoID      int64           `xorm:"INDEX(s)"`
20
+	Type        UnitType        `xorm:"INDEX(s)"`
22 21
 	Config      core.Conversion `xorm:"TEXT"`
23 22
 	CreatedUnix int64           `xorm:"INDEX CREATED"`
24 23
 	Created     time.Time       `xorm:"-"`

+ 0 - 6
routers/repo/setting.go

@@ -149,7 +149,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
149 149
 			units = append(units, models.RepoUnit{
150 150
 				RepoID: repo.ID,
151 151
 				Type:   tp,
152
-				Index:  int(tp),
153 152
 				Config: new(models.UnitConfig),
154 153
 			})
155 154
 		}
@@ -165,7 +164,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
165 164
 				units = append(units, models.RepoUnit{
166 165
 					RepoID: repo.ID,
167 166
 					Type:   models.UnitTypeExternalWiki,
168
-					Index:  int(models.UnitTypeExternalWiki),
169 167
 					Config: &models.ExternalWikiConfig{
170 168
 						ExternalWikiURL: form.ExternalWikiURL,
171 169
 					},
@@ -174,7 +172,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
174 172
 				units = append(units, models.RepoUnit{
175 173
 					RepoID: repo.ID,
176 174
 					Type:   models.UnitTypeWiki,
177
-					Index:  int(models.UnitTypeWiki),
178 175
 					Config: new(models.UnitConfig),
179 176
 				})
180 177
 			}
@@ -190,7 +187,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
190 187
 				units = append(units, models.RepoUnit{
191 188
 					RepoID: repo.ID,
192 189
 					Type:   models.UnitTypeExternalTracker,
193
-					Index:  int(models.UnitTypeExternalTracker),
194 190
 					Config: &models.ExternalTrackerConfig{
195 191
 						ExternalTrackerURL:    form.ExternalTrackerURL,
196 192
 						ExternalTrackerFormat: form.TrackerURLFormat,
@@ -201,7 +197,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
201 197
 				units = append(units, models.RepoUnit{
202 198
 					RepoID: repo.ID,
203 199
 					Type:   models.UnitTypeIssues,
204
-					Index:  int(models.UnitTypeIssues),
205 200
 					Config: &models.IssuesConfig{
206 201
 						EnableTimetracker:                form.EnableTimetracker,
207 202
 						AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime,
@@ -214,7 +209,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
214 209
 			units = append(units, models.RepoUnit{
215 210
 				RepoID: repo.ID,
216 211
 				Type:   models.UnitTypePullRequests,
217
-				Index:  int(models.UnitTypePullRequests),
218 212
 				Config: new(models.UnitConfig),
219 213
 			})
220 214
 		}