Skip to content

Commit a04718a

Browse files
Morlinestlafriks
authored andcommitted
Remove repo unit index (#2621)
* Remove repo unit index * Fix sqlite
1 parent 46cc45f commit a04718a

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

models/fixtures/repo_unit.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,68 @@
22
id: 1
33
repo_id: 1
44
type: 4
5-
index: 3
65
config: "{}"
76
created_unix: 946684810
87

98
-
109
id: 2
1110
repo_id: 1
1211
type: 5
13-
index: 4
1412
config: "{}"
1513
created_unix: 946684810
1614

1715
-
1816
id: 3
1917
repo_id: 1
2018
type: 1
21-
index: 0
2219
config: "{}"
2320
created_unix: 946684810
2421

2522
-
2623
id: 4
2724
repo_id: 1
2825
type: 2
29-
index: 1
3026
config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
3127
created_unix: 946684810
3228

3329
-
3430
id: 5
3531
repo_id: 1
3632
type: 3
37-
index: 2
3833
config: "{}"
3934
created_unix: 946684810
4035

4136
-
4237
id: 6
4338
repo_id: 3
4439
type: 1
45-
index: 0
4640
config: "{}"
4741
created_unix: 946684810
4842

4943
-
5044
id: 7
5145
repo_id: 3
5246
type: 2
53-
index: 1
5447
config: "{\"EnableTimetracker\":false,\"AllowOnlyContributorsToTrackTime\":false}"
5548
created_unix: 946684810
5649

5750
-
5851
id: 8
5952
repo_id: 3
6053
type: 3
61-
index: 2
6254
config: "{}"
6355
created_unix: 946684810
6456

6557
-
6658
id: 9
6759
repo_id: 3
6860
type: 4
69-
index: 3
7061
config: "{}"
7162
created_unix: 946684810
7263

7364
-
7465
id: 10
7566
repo_id: 3
7667
type: 5
77-
index: 4
7868
config: "{}"
7969
created_unix: 946684810

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ var migrations = []Migration{
138138
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
139139
// v44 -> v45
140140
NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
141+
// v45 -> v46
142+
NewMigration("remove index column from repo_unit table", removeIndexColumnFromRepoUnitTable),
141143
}
142144

143145
// Migrate database to current version

models/migrations/v45.go

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

models/repo.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,19 +1245,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
12451245

12461246
// insert units for repo
12471247
var units = make([]RepoUnit, 0, len(defaultRepoUnits))
1248-
for i, tp := range defaultRepoUnits {
1248+
for _, tp := range defaultRepoUnits {
12491249
if tp == UnitTypeIssues {
12501250
units = append(units, RepoUnit{
12511251
RepoID: repo.ID,
12521252
Type: tp,
1253-
Index: i,
12541253
Config: &IssuesConfig{EnableTimetracker: setting.Service.DefaultEnableTimetracking, AllowOnlyContributorsToTrackTime: setting.Service.DefaultAllowOnlyContributorsToTrackTime},
12551254
})
12561255
} else {
12571256
units = append(units, RepoUnit{
12581257
RepoID: repo.ID,
12591258
Type: tp,
1260-
Index: i,
12611259
})
12621260
}
12631261

models/repo_unit.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import (
1616
// RepoUnit describes all units of a repository
1717
type RepoUnit struct {
1818
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)"`
2221
Config core.Conversion `xorm:"TEXT"`
2322
CreatedUnix int64 `xorm:"INDEX CREATED"`
2423
Created time.Time `xorm:"-"`

routers/repo/setting.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
149149
units = append(units, models.RepoUnit{
150150
RepoID: repo.ID,
151151
Type: tp,
152-
Index: int(tp),
153152
Config: new(models.UnitConfig),
154153
})
155154
}
@@ -165,7 +164,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
165164
units = append(units, models.RepoUnit{
166165
RepoID: repo.ID,
167166
Type: models.UnitTypeExternalUncyclo,
168-
Index: int(models.UnitTypeExternalUncyclo),
169167
Config: &models.ExternalUncycloConfig{
170168
ExternalUncycloURL: form.ExternalUncycloURL,
171169
},
@@ -174,7 +172,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
174172
units = append(units, models.RepoUnit{
175173
RepoID: repo.ID,
176174
Type: models.UnitTypeUncyclo,
177-
Index: int(models.UnitTypeUncyclo),
178175
Config: new(models.UnitConfig),
179176
})
180177
}
@@ -190,7 +187,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
190187
units = append(units, models.RepoUnit{
191188
RepoID: repo.ID,
192189
Type: models.UnitTypeExternalTracker,
193-
Index: int(models.UnitTypeExternalTracker),
194190
Config: &models.ExternalTrackerConfig{
195191
ExternalTrackerURL: form.ExternalTrackerURL,
196192
ExternalTrackerFormat: form.TrackerURLFormat,
@@ -201,7 +197,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
201197
units = append(units, models.RepoUnit{
202198
RepoID: repo.ID,
203199
Type: models.UnitTypeIssues,
204-
Index: int(models.UnitTypeIssues),
205200
Config: &models.IssuesConfig{
206201
EnableTimetracker: form.EnableTimetracker,
207202
AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime,
@@ -214,7 +209,6 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
214209
units = append(units, models.RepoUnit{
215210
RepoID: repo.ID,
216211
Type: models.UnitTypePullRequests,
217-
Index: int(models.UnitTypePullRequests),
218212
Config: new(models.UnitConfig),
219213
})
220214
}

0 commit comments

Comments
 (0)