You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

  1. // Copyright 2017 Gitea. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "fmt"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/go-xorm/xorm"
  11. )
  12. func convertIntervalToDuration(x *xorm.Engine) (err error) {
  13. type Repository struct {
  14. ID int64
  15. OwnerID int64
  16. Name string
  17. }
  18. type Mirror struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. RepoID int64 `xorm:"INDEX"`
  21. Repo *Repository `xorm:"-"`
  22. Interval time.Duration
  23. }
  24. sess := x.NewSession()
  25. defer sess.Close()
  26. if err := sess.Begin(); err != nil {
  27. return err
  28. }
  29. dialect := x.Dialect().DriverName()
  30. switch dialect {
  31. case "mysql":
  32. _, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
  33. case "postgres":
  34. _, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" SET DATA TYPE bigint")
  35. case "tidb":
  36. _, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
  37. case "mssql":
  38. _, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" BIGINT")
  39. case "sqlite3":
  40. }
  41. if err != nil {
  42. return fmt.Errorf("Error changing mirror interval column type: %v", err)
  43. }
  44. var mirrors []Mirror
  45. err = sess.Table("mirror").Select("*").Find(&mirrors)
  46. if err != nil {
  47. return fmt.Errorf("Query repositories: %v", err)
  48. }
  49. for _, mirror := range mirrors {
  50. mirror.Interval = mirror.Interval * time.Hour
  51. if mirror.Interval < setting.Mirror.MinInterval {
  52. log.Info("Mirror interval less than Mirror.MinInterval, setting default interval: repo id %v", mirror.RepoID)
  53. mirror.Interval = setting.Mirror.DefaultInterval
  54. }
  55. log.Debug("Mirror interval set to %v for repo id %v", mirror.Interval, mirror.RepoID)
  56. _, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
  57. if err != nil {
  58. return fmt.Errorf("update mirror interval failed: %v", err)
  59. }
  60. }
  61. return sess.Commit()
  62. }