summaryrefslogtreecommitdiffstats
path: root/models/migrations/v69.go
blob: 8d964688aec13539506a4ecda02ecac92d857125 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2018 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"

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

func moveTeamUnitsToTeamUnitTable(x *xorm.Engine) error {
	// Team see models/team.go
	type Team struct {
		ID        int64
		OrgID     int64
		UnitTypes []int `xorm:"json"`
	}

	// TeamUnit see models/org_team.go
	type TeamUnit struct {
		ID     int64 `xorm:"pk autoincr"`
		OrgID  int64 `xorm:"INDEX"`
		TeamID int64 `xorm:"UNIQUE(s)"`
		Type   int   `xorm:"UNIQUE(s)"`
	}

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

	sess := x.NewSession()
	defer sess.Close()

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

	// Update team unit types
	const batchSize = 100
	for start := 0; ; start += batchSize {
		teams := make([]*Team, 0, batchSize)
		if err := x.Limit(batchSize, start).Find(&teams); err != nil {
			return err
		}
		if len(teams) == 0 {
			break
		}

		for _, team := range teams {
			var unitTypes []int
			if len(team.UnitTypes) == 0 {
				unitTypes = allUnitTypes
			} else {
				unitTypes = team.UnitTypes
			}

			// insert units for team
			var units = make([]TeamUnit, 0, len(unitTypes))
			for _, tp := range unitTypes {
				units = append(units, TeamUnit{
					OrgID:  team.OrgID,
					TeamID: team.ID,
					Type:   tp,
				})
			}

			if _, err := sess.Insert(&units); err != nil {
				return fmt.Errorf("Insert team units: %v", err)
			}

		}
	}

	if err := dropTableColumns(sess, "team", "unit_types"); err != nil {
		return err
	}
	return sess.Commit()
}