Skip to content

Commit 6ea4173

Browse files
committed
add migration
1 parent 4593393 commit 6ea4173

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ var migrations = []Migration{
511511
NewMigration("Add git_size and lfs_size columns to repository table", v1_21.AddGitSizeAndLFSSizeToRepositoryTable),
512512
// v264 -> v265
513513
NewMigration("Add branch table", v1_21.AddBranchTable),
514+
// v265 -> v266
515+
NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords),
514516
}
515517

516518
// GetCurrentDBVersion returns the current db version

models/migrations/v1_21/v265.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_21 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/container"
8+
"xorm.io/xorm"
9+
)
10+
11+
func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error {
12+
type UnitType int
13+
type AccessMode int
14+
15+
type Team struct {
16+
ID int64 `xorm:"pk autoincr"`
17+
OrgID int64 `xorm:"INDEX"`
18+
AccessMode AccessMode `xorm:"'authorize'"`
19+
}
20+
21+
type TeamUnit struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
OrgID int64 `xorm:"INDEX"`
24+
TeamID int64 `xorm:"UNIQUE(s)"`
25+
Type UnitType `xorm:"UNIQUE(s)"`
26+
AccessMode AccessMode
27+
}
28+
29+
const (
30+
// AccessModeRead read access
31+
AccessModeRead = 1
32+
// AccessModeAdmin admin access
33+
AccessModeAdmin = 3
34+
35+
// Unit Type
36+
TypeInvalid UnitType = iota // 0 invalid
37+
TypeCode // 1 code
38+
TypeIssues // 2 issues
39+
TypePullRequests // 3 PRs
40+
TypeReleases // 4 Releases
41+
TypeUncyclo // 5 Uncyclo
42+
TypeExternalUncyclo // 6 ExternalUncyclo
43+
TypeExternalTracker // 7 ExternalTracker
44+
TypeProjects // 8 Kanban board
45+
TypePackages // 9 Packages
46+
TypeActions // 10 Actions
47+
)
48+
49+
var AllRepoUnitTypes = []UnitType{
50+
TypeCode,
51+
TypeIssues,
52+
TypePullRequests,
53+
TypeReleases,
54+
TypeUncyclo,
55+
TypeExternalUncyclo,
56+
TypeExternalTracker,
57+
TypeProjects,
58+
TypePackages,
59+
TypeActions,
60+
}
61+
62+
sess := x.NewSession()
63+
defer sess.Close()
64+
65+
if err := sess.Begin(); err != nil {
66+
return err
67+
}
68+
69+
// find all admin teams
70+
teams := make([]*Team, 0)
71+
err := sess.Where("team.authorize == ?", AccessModeAdmin).Find(&teams)
72+
if err != nil {
73+
return err
74+
}
75+
76+
for _, team := range teams {
77+
// find all existing records
78+
teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes))
79+
err := sess.Where("`team_unit`.team_id == ?", team.ID).Find(&teamunits)
80+
if err != nil {
81+
return err
82+
}
83+
existingUnitTypes := make(container.Set[UnitType], 0)
84+
for _, tu := range teamunits {
85+
if tu.Type > 0 {
86+
existingUnitTypes.Add(tu.Type)
87+
}
88+
}
89+
90+
// insert or update records
91+
for _, u := range AllRepoUnitTypes {
92+
newTeamUnit := &TeamUnit{
93+
OrgID: team.OrgID,
94+
TeamID: team.ID,
95+
Type: u,
96+
}
97+
// external unit should be read
98+
if u == TypeExternalUncyclo || u == TypeExternalTracker {
99+
newTeamUnit.AccessMode = AccessModeRead
100+
} else {
101+
newTeamUnit.AccessMode = AccessModeAdmin
102+
}
103+
104+
if existingUnitTypes.Contains(u) {
105+
sess.Cols("access_mode").Update(newTeamUnit)
106+
} else {
107+
sess.Insert(newTeamUnit)
108+
}
109+
}
110+
}
111+
112+
return sess.Commit()
113+
}

0 commit comments

Comments
 (0)