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.

v49.go 2.2KB

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