Fix broken migration v27 (#1504)

Mirror.interval column type needed to be changed to bigint.
Correct interval where the interval set is < MinInterval.
This commit is contained in:
Jonas Östanbäck 2017-04-30 04:08:16 +02:00 committed by Lunny Xiao
parent 1562e9ab70
commit 9a8ca3e877
1 changed files with 30 additions and 12 deletions

View File

@ -8,6 +8,9 @@ import (
"fmt" "fmt"
"time" "time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
@ -18,18 +21,10 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
Name string Name string
} }
type Mirror struct { type Mirror struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX"` RepoID int64 `xorm:"INDEX"`
Repo *Repository `xorm:"-"` Repo *Repository `xorm:"-"`
Interval time.Duration Interval time.Duration
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
NextUpdate time.Time `xorm:"-"`
NextUpdateUnix int64 `xorm:"INDEX"`
address string `xorm:"-"`
} }
sess := x.NewSession() sess := x.NewSession()
@ -39,6 +34,24 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
return err return err
} }
dialect := x.Dialect().DriverName()
switch dialect {
case "mysql":
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
case "postgres":
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" SET DATA TYPE bigint")
case "tidb":
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
case "mssql":
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" BIGINT")
case "sqlite3":
}
if err != nil {
return fmt.Errorf("Error changing mirror interval column type: %v", err)
}
var mirrors []Mirror var mirrors []Mirror
err = sess.Table("mirror").Select("*").Find(&mirrors) err = sess.Table("mirror").Select("*").Find(&mirrors)
if err != nil { if err != nil {
@ -46,6 +59,11 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
} }
for _, mirror := range mirrors { for _, mirror := range mirrors {
mirror.Interval = mirror.Interval * time.Hour mirror.Interval = mirror.Interval * time.Hour
if mirror.Interval < setting.Mirror.MinInterval {
log.Info("Mirror interval less than Mirror.MinInterval, setting default interval: repo id %v", mirror.RepoID)
mirror.Interval = setting.Mirror.DefaultInterval
}
log.Debug("Mirror interval set to %v for repo id %v", mirror.Interval, mirror.RepoID)
_, err := sess.Id(mirror.ID).Cols("interval").Update(mirror) _, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
if err != nil { if err != nil {
return fmt.Errorf("update mirror interval failed: %v", err) return fmt.Errorf("update mirror interval failed: %v", err)