Skip to content

Commit ebac051

Browse files
daviianlunny
authored andcommitted
Rewrite migrations to not depend on future code changes (#2604)
* v38 migration used an outdated version of RepoUnit model (#2602) * change repoUnit model in migration * fix v16 migration repo_unit table * fix lint error * move type definition inside function Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com> * fix lint error Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com> * Fix time tracking migration * Refactor code * Fix migration from Gogs * v38 migration used an outdated version of RepoUnit model (#2602) * change repoUnit model in migration * fix v16 migration repo_unit table * fix lint error * move type definition inside function Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com> * fix lint error Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com> * Fix time tracking migration * Refactor code * Fix migration from Gogs * add error check Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com> * Additiomal fixes for migrations * Fix timetracking migration * Add back nil check
1 parent 92123fe commit ebac051

File tree

5 files changed

+83
-63
lines changed

5 files changed

+83
-63
lines changed

models/migrations/v15.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,15 @@ import (
1010
"github.com/go-xorm/xorm"
1111
)
1212

13-
// UserV15 describes the added field for User
14-
type UserV15 struct {
15-
KeepEmailPrivate bool
16-
AllowCreateOrganization bool
17-
}
18-
19-
// TableName will be invoked by XORM to customrize the table name
20-
func (*UserV15) TableName() string {
21-
return "user"
22-
}
23-
2413
func createAllowCreateOrganizationColumn(x *xorm.Engine) error {
25-
if err := x.Sync2(new(UserV15)); err != nil {
14+
type User struct {
15+
KeepEmailPrivate bool
16+
AllowCreateOrganization bool
17+
}
18+
19+
if err := x.Sync2(new(User)); err != nil {
2620
return fmt.Errorf("Sync2: %v", err)
27-
} else if _, err = x.Where("type=0").Cols("allow_create_organization").Update(&UserV15{AllowCreateOrganization: true}); err != nil {
21+
} else if _, err = x.Where("`type` = 0").Cols("allow_create_organization").Update(&User{AllowCreateOrganization: true}); err != nil {
2822
return fmt.Errorf("set allow_create_organization: %v", err)
2923
}
3024
return nil

models/migrations/v16.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@ import (
1313
"github.com/go-xorm/xorm"
1414
)
1515

16-
// RepoUnit describes all units of a repository
17-
type RepoUnit struct {
18-
ID int64
19-
RepoID int64 `xorm:"INDEX(s)"`
20-
Type int `xorm:"INDEX(s)"`
21-
Index int
22-
Config map[string]interface{} `xorm:"JSON"`
23-
CreatedUnix int64 `xorm:"INDEX CREATED"`
24-
Created time.Time `xorm:"-"`
25-
}
26-
2716
// Enumerate all the unit types
2817
const (
2918
V16UnitTypeCode = iota + 1 // 1 code
@@ -37,14 +26,25 @@ const (
3726
V16UnitTypeExternalTracker // 9 ExternalTracker
3827
)
3928

40-
// Repo describes a repository
41-
type Repo struct {
42-
ID int64
43-
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
44-
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
45-
}
46-
4729
func addUnitsToTables(x *xorm.Engine) error {
30+
// RepoUnit describes all units of a repository
31+
type RepoUnit struct {
32+
ID int64
33+
RepoID int64 `xorm:"INDEX(s)"`
34+
Type int `xorm:"INDEX(s)"`
35+
Index int
36+
Config map[string]interface{} `xorm:"JSON"`
37+
CreatedUnix int64 `xorm:"INDEX CREATED"`
38+
Created time.Time `xorm:"-"`
39+
}
40+
41+
// Repo describes a repository
42+
type Repo struct {
43+
ID int64
44+
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
45+
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
46+
}
47+
4848
var repos []Repo
4949
err := x.Table("repository").Select("*").Find(&repos)
5050
if err != nil {

models/migrations/v37.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ package migrations
77
import (
88
"html"
99

10-
"code.gitea.io/gitea/models"
11-
1210
"github.com/go-xorm/xorm"
1311
)
1412

1513
func unescapeUserFullNames(x *xorm.Engine) (err error) {
14+
type User struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
FullName string
17+
}
18+
1619
const batchSize = 100
1720
for start := 0; ; start += batchSize {
18-
users := make([]*models.User, 0, batchSize)
19-
if err := x.Limit(start, batchSize).Find(users); err != nil {
21+
users := make([]*User, 0, batchSize)
22+
if err := x.Limit(batchSize, start).Find(&users); err != nil {
2023
return err
2124
}
2225
if len(users) == 0 {
2326
return nil
2427
}
2528
for _, user := range users {
2629
user.FullName = html.UnescapeString(user.FullName)
27-
if _, err := x.Cols("full_name").Update(user); err != nil {
30+
if _, err := x.ID(user.ID).Cols("full_name").Update(user); err != nil {
2831
return err
2932
}
3033
}

models/migrations/v38.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55
package migrations
66

77
import (
8+
"time"
9+
810
"code.gitea.io/gitea/models"
911

12+
"github.com/go-xorm/core"
1013
"github.com/go-xorm/xorm"
1114
)
1215

1316
func removeCommitsUnitType(x *xorm.Engine) (err error) {
17+
// RepoUnit describes all units of a repository
18+
type RepoUnit struct {
19+
ID int64
20+
RepoID int64 `xorm:"INDEX(s)"`
21+
Type int `xorm:"INDEX(s)"`
22+
Index int
23+
Config core.Conversion `xorm:"TEXT"`
24+
CreatedUnix int64 `xorm:"INDEX CREATED"`
25+
Created time.Time `xorm:"-"`
26+
}
27+
1428
// Update team unit types
1529
const batchSize = 100
1630
for start := 0; ; start += batchSize {

models/migrations/v39.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,61 @@ import (
1313
"github.com/go-xorm/xorm"
1414
)
1515

16-
// Stopwatch see models/issue_stopwatch.go
17-
type Stopwatch struct {
18-
ID int64 `xorm:"pk autoincr"`
19-
IssueID int64 `xorm:"INDEX"`
20-
UserID int64 `xorm:"INDEX"`
21-
Created time.Time `xorm:"-"`
22-
CreatedUnix int64
23-
}
16+
func addTimetracking(x *xorm.Engine) error {
17+
// RepoUnit describes all units of a repository
18+
type RepoUnit struct {
19+
ID int64
20+
RepoID int64 `xorm:"INDEX(s)"`
21+
Type int `xorm:"INDEX(s)"`
22+
Index int
23+
Config map[string]interface{} `xorm:"JSON"`
24+
CreatedUnix int64 `xorm:"INDEX CREATED"`
25+
Created time.Time `xorm:"-"`
26+
}
2427

25-
// TrackedTime see models/issue_tracked_time.go
26-
type TrackedTime struct {
27-
ID int64 `xorm:"pk autoincr" json:"id"`
28-
IssueID int64 `xorm:"INDEX" json:"issue_id"`
29-
UserID int64 `xorm:"INDEX" json:"user_id"`
30-
Created time.Time `xorm:"-" json:"created"`
31-
CreatedUnix int64 `json:"-"`
32-
Time int64 `json:"time"`
33-
}
28+
// Stopwatch see models/issue_stopwatch.go
29+
type Stopwatch struct {
30+
ID int64 `xorm:"pk autoincr"`
31+
IssueID int64 `xorm:"INDEX"`
32+
UserID int64 `xorm:"INDEX"`
33+
Created time.Time `xorm:"-"`
34+
CreatedUnix int64
35+
}
36+
37+
// TrackedTime see models/issue_tracked_time.go
38+
type TrackedTime struct {
39+
ID int64 `xorm:"pk autoincr" json:"id"`
40+
IssueID int64 `xorm:"INDEX" json:"issue_id"`
41+
UserID int64 `xorm:"INDEX" json:"user_id"`
42+
Created time.Time `xorm:"-" json:"created"`
43+
CreatedUnix int64 `json:"-"`
44+
Time int64 `json:"time"`
45+
}
3446

35-
func addTimetracking(x *xorm.Engine) error {
3647
if err := x.Sync2(new(Stopwatch)); err != nil {
3748
return fmt.Errorf("Sync2: %v", err)
3849
}
3950
if err := x.Sync2(new(TrackedTime)); err != nil {
4051
return fmt.Errorf("Sync2: %v", err)
4152
}
4253
//Updating existing issue units
43-
var units []*RepoUnit
44-
x.Where("type = ?", V16UnitTypeIssues).Find(&units)
54+
units := make([]*RepoUnit, 0, 100)
55+
err := x.Where("`type` = ?", V16UnitTypeIssues).Find(&units)
56+
if err != nil {
57+
return fmt.Errorf("Query repo units: %v", err)
58+
}
4559
for _, unit := range units {
4660
if unit.Config == nil {
4761
unit.Config = make(map[string]interface{})
4862
}
49-
changes := false
5063
if _, ok := unit.Config["EnableTimetracker"]; !ok {
5164
unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
52-
changes = true
5365
}
5466
if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
5567
unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
56-
changes = true
5768
}
58-
if changes {
59-
if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
60-
return err
61-
}
69+
if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
70+
return err
6271
}
6372
}
6473
return nil

0 commit comments

Comments
 (0)