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
pull/2675/head^2
David Schneiderbauer 7 years ago committed by Lunny Xiao
parent 92123fe82a
commit ebac051e72
  1. 20
      models/migrations/v15.go
  2. 36
      models/migrations/v16.go
  3. 13
      models/migrations/v37.go
  4. 14
      models/migrations/v38.go
  5. 63
      models/migrations/v39.go

@ -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

@ -13,17 +13,6 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
RepoID int64 `xorm:"INDEX(s)"`
Type int `xorm:"INDEX(s)"`
Index int
Config map[string]interface{} `xorm:"JSON"`
CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"`
}
// Enumerate all the unit types // Enumerate all the unit types
const ( const (
V16UnitTypeCode = iota + 1 // 1 code V16UnitTypeCode = iota + 1 // 1 code
@ -37,14 +26,25 @@ const (
V16UnitTypeExternalTracker // 9 ExternalTracker V16UnitTypeExternalTracker // 9 ExternalTracker
) )
// Repo describes a repository
type Repo struct {
ID int64
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
}
func addUnitsToTables(x *xorm.Engine) error { func addUnitsToTables(x *xorm.Engine) error {
// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
RepoID int64 `xorm:"INDEX(s)"`
Type int `xorm:"INDEX(s)"`
Index int
Config map[string]interface{} `xorm:"JSON"`
CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"`
}
// Repo describes a repository
type Repo struct {
ID int64
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
}
var repos []Repo var repos []Repo
err := x.Table("repository").Select("*").Find(&repos) err := x.Table("repository").Select("*").Find(&repos)
if err != nil { if err != nil {

@ -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
} }
} }

@ -5,12 +5,26 @@
package migrations package migrations
import ( import (
"time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
func removeCommitsUnitType(x *xorm.Engine) (err error) { func removeCommitsUnitType(x *xorm.Engine) (err error) {
// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
RepoID int64 `xorm:"INDEX(s)"`
Type int `xorm:"INDEX(s)"`
Index int
Config core.Conversion `xorm:"TEXT"`
CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"`
}
// Update team unit types // Update team unit types
const batchSize = 100 const batchSize = 100
for start := 0; ; start += batchSize { for start := 0; ; start += batchSize {

@ -13,26 +13,37 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
// Stopwatch see models/issue_stopwatch.go func addTimetracking(x *xorm.Engine) error {
type Stopwatch struct { // RepoUnit describes all units of a repository
ID int64 `xorm:"pk autoincr"` type RepoUnit struct {
IssueID int64 `xorm:"INDEX"` ID int64
UserID int64 `xorm:"INDEX"` RepoID int64 `xorm:"INDEX(s)"`
Created time.Time `xorm:"-"` Type int `xorm:"INDEX(s)"`
CreatedUnix int64 Index int
} Config map[string]interface{} `xorm:"JSON"`
CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"`
}
// TrackedTime see models/issue_tracked_time.go // Stopwatch see models/issue_stopwatch.go
type TrackedTime struct { type Stopwatch struct {
ID int64 `xorm:"pk autoincr" json:"id"` ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX" json:"issue_id"` IssueID int64 `xorm:"INDEX"`
UserID int64 `xorm:"INDEX" json:"user_id"` UserID int64 `xorm:"INDEX"`
Created time.Time `xorm:"-" json:"created"` Created time.Time `xorm:"-"`
CreatedUnix int64 `json:"-"` CreatedUnix int64
Time int64 `json:"time"` }
}
// TrackedTime see models/issue_tracked_time.go
type TrackedTime struct {
ID int64 `xorm:"pk autoincr" json:"id"`
IssueID int64 `xorm:"INDEX" json:"issue_id"`
UserID int64 `xorm:"INDEX" json:"user_id"`
Created time.Time `xorm:"-" json:"created"`
CreatedUnix int64 `json:"-"`
Time int64 `json:"time"`
}
func addTimetracking(x *xorm.Engine) error {
if err := x.Sync2(new(Stopwatch)); err != nil { if err := x.Sync2(new(Stopwatch)); err != nil {
return fmt.Errorf("Sync2: %v", err) return fmt.Errorf("Sync2: %v", err)
} }
@ -40,25 +51,23 @@ func addTimetracking(x *xorm.Engine) error {
return fmt.Errorf("Sync2: %v", err) return fmt.Errorf("Sync2: %v", err)
} }
//Updating existing issue units //Updating existing issue units
var units []*RepoUnit units := make([]*RepoUnit, 0, 100)
x.Where("type = ?", V16UnitTypeIssues).Find(&units) err := x.Where("`type` = ?", V16UnitTypeIssues).Find(&units)
if err != nil {
return fmt.Errorf("Query repo units: %v", err)
}
for _, unit := range units { for _, unit := range units {
if unit.Config == nil { if unit.Config == nil {
unit.Config = make(map[string]interface{}) unit.Config = make(map[string]interface{})
} }
changes := false
if _, ok := unit.Config["EnableTimetracker"]; !ok { if _, ok := unit.Config["EnableTimetracker"]; !ok {
unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
changes = true
} }
if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok { if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
changes = true
} }
if changes { if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil { return err
return err
}
} }
} }
return nil return nil

Loading…
Cancel
Save