Skip to content

Commit 74399f3

Browse files
daviianlafriks
authored andcommitted
Backport of migration fixes (#2604) (#2677)
* 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 * Fix migration from Gogs * Refactor code * add error check * Additiomal fixes for migrations * Add back nil check * replace deprecated .Id with .ID Signed-off-by: David Schneiderbauer <[email protected]> * change string map to interface map Signed-off-by: David Schneiderbauer <[email protected]>
1 parent d1cec5e commit 74399f3

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func addUnitsToTables(x *xorm.Engine) error {
3333
RepoID int64 `xorm:"INDEX(s)"`
3434
Type int `xorm:"INDEX(s)"`
3535
Index int
36-
Config map[string]string `xorm:"JSON"`
37-
CreatedUnix int64 `xorm:"INDEX CREATED"`
38-
Created time.Time `xorm:"-"`
36+
Config map[string]interface{} `xorm:"JSON"`
37+
CreatedUnix int64 `xorm:"INDEX CREATED"`
38+
Created time.Time `xorm:"-"`
3939
}
4040

4141
// Repo describes a repository
@@ -95,7 +95,7 @@ func addUnitsToTables(x *xorm.Engine) error {
9595
continue
9696
}
9797

98-
var config = make(map[string]string)
98+
var config = make(map[string]interface{})
9999
switch i {
100100
case V16UnitTypeExternalTracker:
101101
config["ExternalTrackerURL"] = repo.ExternalTrackerURL

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func removeCommitsUnitType(x *xorm.Engine) (err error) {
4747
}
4848
}
4949
team.UnitTypes = ut
50-
if _, err := x.Id(team.ID).Cols("unit_types").Update(team); err != nil {
50+
if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
5151
return err
5252
}
5353
}

0 commit comments

Comments
 (0)