From 74399f333fcb85113ea8490f59880d4d5f7e22ba Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Mon, 9 Oct 2017 15:08:22 +0200 Subject: [PATCH] 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 * change string map to interface map Signed-off-by: David Schneiderbauer --- models/migrations/v15.go | 20 +++++++------------- models/migrations/v16.go | 8 ++++---- models/migrations/v37.go | 13 ++++++++----- models/migrations/v38.go | 2 +- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/models/migrations/v15.go b/models/migrations/v15.go index e29067c98..3492a7190 100644 --- a/models/migrations/v15.go +++ b/models/migrations/v15.go @@ -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 diff --git a/models/migrations/v16.go b/models/migrations/v16.go index f14b196f2..f6fb76da5 100644 --- a/models/migrations/v16.go +++ b/models/migrations/v16.go @@ -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 diff --git a/models/migrations/v37.go b/models/migrations/v37.go index aac00e84c..00653a780 100644 --- a/models/migrations/v37.go +++ b/models/migrations/v37.go @@ -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 } } diff --git a/models/migrations/v38.go b/models/migrations/v38.go index 216d49935..6f66484b0 100644 --- a/models/migrations/v38.go +++ b/models/migrations/v38.go @@ -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 } }