Browse Source

Fix broken migration v27 (#1504)

Mirror.interval column type needed to be changed to bigint.
Correct interval where the interval set is < MinInterval.
Jonas Östanbäck 3 years ago
parent
commit
9a8ca3e877
1 changed files with 30 additions and 12 deletions
  1. 30 12
      models/migrations/v27.go

+ 30 - 12
models/migrations/v27.go

@@ -8,6 +8,9 @@ import (
8 8
 	"fmt"
9 9
 	"time"
10 10
 
11
+	"code.gitea.io/gitea/modules/log"
12
+	"code.gitea.io/gitea/modules/setting"
13
+
11 14
 	"github.com/go-xorm/xorm"
12 15
 )
13 16
 
@@ -18,18 +21,10 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
18 21
 		Name    string
19 22
 	}
20 23
 	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
33 28
 	}
34 29
 
35 30
 	sess := x.NewSession()
@@ -39,6 +34,24 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
39 34
 		return err
40 35
 	}
41 36
 
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
+
42 55
 	var mirrors []Mirror
43 56
 	err = sess.Table("mirror").Select("*").Find(&mirrors)
44 57
 	if err != nil {
@@ -46,6 +59,11 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
46 59
 	}
47 60
 	for _, mirror := range mirrors {
48 61
 		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)
49 67
 		_, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
50 68
 		if err != nil {
51 69
 			return fmt.Errorf("update mirror interval failed: %v", err)