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.

v39.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }