aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations/v39.go
blob: 41279484e1cfaf3fbc3fa300a48134ec3a5169ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
	"fmt"
	"time"

	"code.gitea.io/gitea/modules/setting"

	"github.com/go-xorm/xorm"
)

// Stopwatch see models/issue_stopwatch.go
type Stopwatch struct {
	ID          int64     `xorm:"pk autoincr"`
	IssueID     int64     `xorm:"INDEX"`
	UserID      int64     `xorm:"INDEX"`
	Created     time.Time `xorm:"-"`
	CreatedUnix int64
}

// TrackedTime see models/issue_tracked_time.go
type TrackedTime struct {
	ID          int64     `xorm:"pk autoincr" json:"id"`
	IssueID     int64     `xorm:"INDEX" json:"issue_id"`
	UserID      int64     `xorm:"INDEX" json:"user_id"`
	Created     time.Time `xorm:"-" json:"created"`
	CreatedUnix int64     `json:"-"`
	Time        int64     `json:"time"`
}

func addTimetracking(x *xorm.Engine) error {
	if err := x.Sync2(new(Stopwatch)); err != nil {
		return fmt.Errorf("Sync2: %v", err)
	}
	if err := x.Sync2(new(TrackedTime)); err != nil {
		return fmt.Errorf("Sync2: %v", err)
	}
	//Updating existing issue units
	var units []*RepoUnit
	x.Where("type = ?", V16UnitTypeIssues).Find(&units)
	for _, unit := range units {
		if unit.Config == nil {
			unit.Config = make(map[string]interface{})
		}
		changes := false
		if _, ok := unit.Config["EnableTimetracker"]; !ok {
			unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
			changes = true
		}
		if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
			unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
			changes = true
		}
		if changes {
			if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
				return err
			}
		}
	}
	return nil
}