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.

v27.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "xorm.io/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 "mssql":
  36. _, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" BIGINT")
  37. case "sqlite3":
  38. }
  39. if err != nil {
  40. return fmt.Errorf("Error changing mirror interval column type: %v", err)
  41. }
  42. var mirrors []Mirror
  43. err = sess.Table("mirror").Select("*").Find(&mirrors)
  44. if err != nil {
  45. return fmt.Errorf("Query repositories: %v", err)
  46. }
  47. for _, mirror := range mirrors {
  48. mirror.Interval *= time.Hour
  49. if mirror.Interval < setting.Mirror.MinInterval {
  50. log.Info("Mirror interval less than Mirror.MinInterval, setting default interval: repo id %v", mirror.RepoID)
  51. mirror.Interval = setting.Mirror.DefaultInterval
  52. }
  53. log.Debug("Mirror interval set to %v for repo id %v", mirror.Interval, mirror.RepoID)
  54. _, err := sess.ID(mirror.ID).Cols("interval").Update(mirror)
  55. if err != nil {
  56. return fmt.Errorf("update mirror interval failed: %v", err)
  57. }
  58. }
  59. return sess.Commit()
  60. }