Skip to content

Commit 0e0424c

Browse files
authored
Add Doctor FixWrongUserType (#14522)
* Add Doctor FixWrongUserType * use NoAutoTime
1 parent 0536581 commit 0e0424c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

models/consistency.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,17 @@ func CountNullArchivedRepository() (int64, error) {
291291

292292
// FixNullArchivedRepository sets is_archived to false where it is null
293293
func FixNullArchivedRepository() (int64, error) {
294-
return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{
294+
return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").NoAutoTime().Update(&Repository{
295295
IsArchived: false,
296296
})
297297
}
298+
299+
// CountWrongUserType count OrgUser who have wrong type
300+
func CountWrongUserType() (int64, error) {
301+
return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Count(new(User))
302+
}
303+
304+
// FixWrongUserType fix OrgUser who have wrong type
305+
func FixWrongUserType() (int64, error) {
306+
return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").NoAutoTime().Update(&User{Type: 1})
307+
}

modules/doctor/usertype.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package doctor
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/log"
10+
)
11+
12+
func checkUserType(logger log.Logger, autofix bool) error {
13+
count, err := models.CountWrongUserType()
14+
if err != nil {
15+
logger.Critical("Error: %v whilst counting wrong user types")
16+
return err
17+
}
18+
if count > 0 {
19+
if autofix {
20+
if count, err = models.FixWrongUserType(); err != nil {
21+
logger.Critical("Error: %v whilst fixing wrong user types")
22+
return err
23+
}
24+
logger.Info("%d users with wrong type fixed", count)
25+
} else {
26+
logger.Warn("%d users with wrong type exist", count)
27+
}
28+
}
29+
return nil
30+
}
31+
32+
func init() {
33+
Register(&Check{
34+
Title: "Check if user with wrong type exist",
35+
Name: "check-user-type",
36+
IsDefault: true,
37+
Run: checkUserType,
38+
Priority: 3,
39+
})
40+
}

0 commit comments

Comments
 (0)