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 <dschneiderbauer@gmail.com>

* change string map to interface map

Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
This commit is contained in:
David Schneiderbauer 2017-10-09 15:08:22 +02:00 committed by Lauris BH
parent d1cec5ecfa
commit 74399f333f
4 changed files with 20 additions and 23 deletions

View File

@ -10,21 +10,15 @@ import (
"github.com/go-xorm/xorm" "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 { 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) 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 fmt.Errorf("set allow_create_organization: %v", err)
} }
return nil return nil

View File

@ -33,9 +33,9 @@ func addUnitsToTables(x *xorm.Engine) error {
RepoID int64 `xorm:"INDEX(s)"` RepoID int64 `xorm:"INDEX(s)"`
Type int `xorm:"INDEX(s)"` Type int `xorm:"INDEX(s)"`
Index int Index int
Config map[string]string `xorm:"JSON"` Config map[string]interface{} `xorm:"JSON"`
CreatedUnix int64 `xorm:"INDEX CREATED"` CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"` Created time.Time `xorm:"-"`
} }
// Repo describes a repository // Repo describes a repository
@ -95,7 +95,7 @@ func addUnitsToTables(x *xorm.Engine) error {
continue continue
} }
var config = make(map[string]string) var config = make(map[string]interface{})
switch i { switch i {
case V16UnitTypeExternalTracker: case V16UnitTypeExternalTracker:
config["ExternalTrackerURL"] = repo.ExternalTrackerURL config["ExternalTrackerURL"] = repo.ExternalTrackerURL

View File

@ -7,16 +7,19 @@ package migrations
import ( import (
"html" "html"
"code.gitea.io/gitea/models"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
func unescapeUserFullNames(x *xorm.Engine) (err error) { func unescapeUserFullNames(x *xorm.Engine) (err error) {
type User struct {
ID int64 `xorm:"pk autoincr"`
FullName string
}
const batchSize = 100 const batchSize = 100
for start := 0; ; start += batchSize { for start := 0; ; start += batchSize {
users := make([]*models.User, 0, batchSize) users := make([]*User, 0, batchSize)
if err := x.Limit(start, batchSize).Find(users); err != nil { if err := x.Limit(batchSize, start).Find(&users); err != nil {
return err return err
} }
if len(users) == 0 { if len(users) == 0 {
@ -24,7 +27,7 @@ func unescapeUserFullNames(x *xorm.Engine) (err error) {
} }
for _, user := range users { for _, user := range users {
user.FullName = html.UnescapeString(user.FullName) 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 return err
} }
} }

View File

@ -47,7 +47,7 @@ func removeCommitsUnitType(x *xorm.Engine) (err error) {
} }
} }
team.UnitTypes = ut 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 return err
} }
} }