Skip to content

Commit 9a8ca3e

Browse files
cez81lunny
authored andcommitted
Fix broken migration v27 (#1504)
Mirror.interval column type needed to be changed to bigint. Correct interval where the interval set is < MinInterval.
1 parent 1562e9a commit 9a8ca3e

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

models/migrations/v27.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"fmt"
99
"time"
1010

11+
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/setting"
13+
1114
"github.com/go-xorm/xorm"
1215
)
1316

@@ -18,18 +21,10 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
1821
Name string
1922
}
2023
type Mirror struct {
21-
ID int64 `xorm:"pk autoincr"`
22-
RepoID int64 `xorm:"INDEX"`
23-
Repo *Repository `xorm:"-"`
24-
Interval time.Duration
25-
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
26-
27-
Updated time.Time `xorm:"-"`
28-
UpdatedUnix int64 `xorm:"INDEX"`
29-
NextUpdate time.Time `xorm:"-"`
30-
NextUpdateUnix int64 `xorm:"INDEX"`
31-
32-
address string `xorm:"-"`
24+
ID int64 `xorm:"pk autoincr"`
25+
RepoID int64 `xorm:"INDEX"`
26+
Repo *Repository `xorm:"-"`
27+
Interval time.Duration
3328
}
3429

3530
sess := x.NewSession()
@@ -39,13 +34,36 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
3934
return err
4035
}
4136

37+
dialect := x.Dialect().DriverName()
38+
39+
switch dialect {
40+
case "mysql":
41+
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
42+
case "postgres":
43+
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" SET DATA TYPE bigint")
44+
case "tidb":
45+
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
46+
case "mssql":
47+
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" BIGINT")
48+
case "sqlite3":
49+
}
50+
51+
if err != nil {
52+
return fmt.Errorf("Error changing mirror interval column type: %v", err)
53+
}
54+
4255
var mirrors []Mirror
4356
err = sess.Table("mirror").Select("*").Find(&mirrors)
4457
if err != nil {
4558
return fmt.Errorf("Query repositories: %v", err)
4659
}
4760
for _, mirror := range mirrors {
4861
mirror.Interval = mirror.Interval * time.Hour
62+
if mirror.Interval < setting.Mirror.MinInterval {
63+
log.Info("Mirror interval less than Mirror.MinInterval, setting default interval: repo id %v", mirror.RepoID)
64+
mirror.Interval = setting.Mirror.DefaultInterval
65+
}
66+
log.Debug("Mirror interval set to %v for repo id %v", mirror.Interval, mirror.RepoID)
4967
_, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
5068
if err != nil {
5169
return fmt.Errorf("update mirror interval failed: %v", err)

0 commit comments

Comments
 (0)