* 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 <dschneiderbauer@gmail.com> * change string map to interface map Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
This commit is contained in:
parent
d1cec5ecfa
commit
74399f333f
|
@ -10,21 +10,15 @@ import (
|
|||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
// UserV15 describes the added field for User
|
||||
type UserV15 struct {
|
||||
KeepEmailPrivate bool
|
||||
AllowCreateOrganization bool
|
||||
}
|
||||
|
||||
// TableName will be invoked by XORM to customrize the table name
|
||||
func (*UserV15) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
|
||||
func createAllowCreateOrganizationColumn(x *xorm.Engine) error {
|
||||
if err := x.Sync2(new(UserV15)); err != nil {
|
||||
type User struct {
|
||||
KeepEmailPrivate bool
|
||||
AllowCreateOrganization bool
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(User)); err != nil {
|
||||
return fmt.Errorf("Sync2: %v", err)
|
||||
} else if _, err = x.Where("type=0").Cols("allow_create_organization").Update(&UserV15{AllowCreateOrganization: true}); err != nil {
|
||||
} else if _, err = x.Where("`type` = 0").Cols("allow_create_organization").Update(&User{AllowCreateOrganization: true}); err != nil {
|
||||
return fmt.Errorf("set allow_create_organization: %v", err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -33,9 +33,9 @@ func addUnitsToTables(x *xorm.Engine) error {
|
|||
RepoID int64 `xorm:"INDEX(s)"`
|
||||
Type int `xorm:"INDEX(s)"`
|
||||
Index int
|
||||
Config map[string]string `xorm:"JSON"`
|
||||
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
||||
Created time.Time `xorm:"-"`
|
||||
Config map[string]interface{} `xorm:"JSON"`
|
||||
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
||||
Created time.Time `xorm:"-"`
|
||||
}
|
||||
|
||||
// Repo describes a repository
|
||||
|
@ -95,7 +95,7 @@ func addUnitsToTables(x *xorm.Engine) error {
|
|||
continue
|
||||
}
|
||||
|
||||
var config = make(map[string]string)
|
||||
var config = make(map[string]interface{})
|
||||
switch i {
|
||||
case V16UnitTypeExternalTracker:
|
||||
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
|
||||
|
|
|
@ -7,16 +7,19 @@ package migrations
|
|||
import (
|
||||
"html"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
func unescapeUserFullNames(x *xorm.Engine) (err error) {
|
||||
type User struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
FullName string
|
||||
}
|
||||
|
||||
const batchSize = 100
|
||||
for start := 0; ; start += batchSize {
|
||||
users := make([]*models.User, 0, batchSize)
|
||||
if err := x.Limit(start, batchSize).Find(users); err != nil {
|
||||
users := make([]*User, 0, batchSize)
|
||||
if err := x.Limit(batchSize, start).Find(&users); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(users) == 0 {
|
||||
|
@ -24,7 +27,7 @@ func unescapeUserFullNames(x *xorm.Engine) (err error) {
|
|||
}
|
||||
for _, user := range users {
|
||||
user.FullName = html.UnescapeString(user.FullName)
|
||||
if _, err := x.Cols("full_name").Update(user); err != nil {
|
||||
if _, err := x.ID(user.ID).Cols("full_name").Update(user); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ func removeCommitsUnitType(x *xorm.Engine) (err error) {
|
|||
}
|
||||
}
|
||||
team.UnitTypes = ut
|
||||
if _, err := x.Id(team.ID).Cols("unit_types").Update(team); err != nil {
|
||||
if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user