Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

v39.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 The Gitea Authors. 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/setting"
  9. "github.com/go-xorm/xorm"
  10. )
  11. func addTimetracking(x *xorm.Engine) error {
  12. // RepoUnit describes all units of a repository
  13. type RepoUnit struct {
  14. ID int64
  15. RepoID int64 `xorm:"INDEX(s)"`
  16. Type int `xorm:"INDEX(s)"`
  17. Index int
  18. Config map[string]interface{} `xorm:"JSON"`
  19. CreatedUnix int64 `xorm:"INDEX CREATED"`
  20. Created time.Time `xorm:"-"`
  21. }
  22. // Stopwatch see models/issue_stopwatch.go
  23. type Stopwatch struct {
  24. ID int64 `xorm:"pk autoincr"`
  25. IssueID int64 `xorm:"INDEX"`
  26. UserID int64 `xorm:"INDEX"`
  27. Created time.Time `xorm:"-"`
  28. CreatedUnix int64
  29. }
  30. // TrackedTime see models/issue_tracked_time.go
  31. type TrackedTime struct {
  32. ID int64 `xorm:"pk autoincr" json:"id"`
  33. IssueID int64 `xorm:"INDEX" json:"issue_id"`
  34. UserID int64 `xorm:"INDEX" json:"user_id"`
  35. Created time.Time `xorm:"-" json:"created"`
  36. CreatedUnix int64 `json:"-"`
  37. Time int64 `json:"time"`
  38. }
  39. if err := x.Sync2(new(Stopwatch)); err != nil {
  40. return fmt.Errorf("Sync2: %v", err)
  41. }
  42. if err := x.Sync2(new(TrackedTime)); err != nil {
  43. return fmt.Errorf("Sync2: %v", err)
  44. }
  45. //Updating existing issue units
  46. units := make([]*RepoUnit, 0, 100)
  47. err := x.Where("`type` = ?", V16UnitTypeIssues).Find(&units)
  48. if err != nil {
  49. return fmt.Errorf("Query repo units: %v", err)
  50. }
  51. for _, unit := range units {
  52. if unit.Config == nil {
  53. unit.Config = make(map[string]interface{})
  54. }
  55. if _, ok := unit.Config["EnableTimetracker"]; !ok {
  56. unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
  57. }
  58. if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
  59. unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
  60. }
  61. if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }