aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations/v38.go
blob: c85688db788be8969a64c3efe4d3b6f82005b4f6 (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
// 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 (
	"code.gitea.io/gitea/models"

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

func removeCommitsUnitType(x *xorm.Engine) (err error) {
	// Update team unit types
	const batchSize = 100
	for start := 0; ; start += batchSize {
		teams := make([]*models.Team, 0, batchSize)
		if err := x.Limit(batchSize, start).Find(&teams); err != nil {
			return err
		}
		if len(teams) == 0 {
			break
		}
		for _, team := range teams {
			ut := make([]models.UnitType, 0, len(team.UnitTypes))
			for _, u := range team.UnitTypes {
				if u < V16UnitTypeCommits {
					ut = append(ut, u)
				} else if u > V16UnitTypeSettings {
					ut = append(ut, u-2)
				} else if u > V16UnitTypeCommits && u != V16UnitTypeSettings {
					ut = append(ut, u-1)
				}
			}
			team.UnitTypes = ut
			if _, err := x.ID(team.ID).Cols("unit_types").Update(team); err != nil {
				return err
			}
		}
	}

	// Delete commits and settings unit types
	if _, err = x.In("`type`", []models.UnitType{V16UnitTypeCommits, V16UnitTypeSettings}).Delete(new(RepoUnit)); err != nil {
		return err
	}
	// Fix renumber unit types that where in enumeration after settings unit type
	if _, err = x.Where("`type` > ?", V16UnitTypeSettings).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
		return err
	}
	// Fix renumber unit types that where in enumeration after commits unit type
	if _, err = x.Where("`type` > ?", V16UnitTypeCommits).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
		return err
	}

	return nil
}