Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite migrations to not depend on future code changes #2604

Merged
merged 17 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions models/migrations/v15.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions models/migrations/v16.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ import (
"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
const (
V16UnitTypeCode = iota + 1 // 1 code
Expand All @@ -37,14 +26,25 @@ const (
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 {
// 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
err := x.Table("repository").Select("*").Find(&repos)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions models/migrations/v37.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ 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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Nearly impossible to see

return err
}
if len(users) == 0 {
return nil
}
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
}
}
Expand Down
16 changes: 15 additions & 1 deletion models/migrations/v38.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@
package migrations

import (
"time"

"code.gitea.io/gitea/models"

"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
)

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
const batchSize = 100
for start := 0; ; start += batchSize {
Expand All @@ -33,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
}
}
Expand Down
63 changes: 36 additions & 27 deletions models/migrations/v39.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,61 @@ import (
"github.com/go-xorm/xorm"
)

// Stopwatch see models/issue_stopwatch.go
type Stopwatch struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX"`
UserID int64 `xorm:"INDEX"`
Created time.Time `xorm:"-"`
CreatedUnix int64
}
func addTimetracking(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:"-"`
}

// 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"`
}
// Stopwatch see models/issue_stopwatch.go
type Stopwatch struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX"`
UserID int64 `xorm:"INDEX"`
Created time.Time `xorm:"-"`
CreatedUnix int64
}

// 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 {
return fmt.Errorf("Sync2: %v", err)
}
if err := x.Sync2(new(TrackedTime)); err != nil {
return fmt.Errorf("Sync2: %v", err)
}
//Updating existing issue units
var units []*RepoUnit
x.Where("type = ?", V16UnitTypeIssues).Find(&units)
units := make([]*RepoUnit, 0, 100)
err := x.Where("`type` = ?", V16UnitTypeIssues).Find(&units)
if err != nil {
return fmt.Errorf("Query repo units: %v", err)
}
for _, unit := range units {
if unit.Config == nil {
unit.Config = make(map[string]interface{})
}
changes := false
if _, ok := unit.Config["EnableTimetracker"]; !ok {
unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
changes = true
}
if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
changes = true
}
if changes {
if _, err := x.Id(unit.ID).Cols("config").Update(unit); err != nil {
return err
}
if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
return err
}
}
return nil
Expand Down