summaryrefslogtreecommitdiffstats
path: root/models/migrations/v136.go
blob: da5706258eb672c1c4d9f181bc41ee90972eea38 (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
// Copyright 2020 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"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/setting"
	pull_service "code.gitea.io/gitea/services/pull"

	"xorm.io/xorm"
)

func addCommitDivergenceToPulls(x *xorm.Engine) error {

	if err := x.Sync2(new(models.PullRequest)); err != nil {
		return fmt.Errorf("Sync2: %v", err)
	}

	var last int
	batchSize := setting.Database.IterateBufferSize
	sess := x.NewSession()
	defer sess.Close()
	for {
		if err := sess.Begin(); err != nil {
			return err
		}
		var results = make([]*models.PullRequest, 0, batchSize)
		err := sess.Where("has_merged = ?", false).OrderBy("id").Limit(batchSize, last).Find(&results)
		if err != nil {
			return err
		}
		if len(results) == 0 {
			break
		}
		last += len(results)

		for _, pr := range results {
			divergence, err := pull_service.GetDiverging(pr)
			if err != nil {
				log.Warn("Could not recalculate Divergence for pull: %d", pr.ID)
				pr.CommitsAhead = 0
				pr.CommitsBehind = 0
			}
			if divergence != nil {
				pr.CommitsAhead = divergence.Ahead
				pr.CommitsBehind = divergence.Behind
			}
			if _, err = sess.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr); err != nil {
				return fmt.Errorf("Update Cols: %v", err)
			}
		}

		if err := sess.Commit(); err != nil {
			return err
		}
	}
	return nil
}