From 14481d6205094218414d3bc45fd1688b3079ce63 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 29 Jan 2021 17:37:45 +0100 Subject: [PATCH 1/2] Add Doctor FixWrongUserType --- models/consistency.go | 10 ++++++++++ modules/doctor/usertype.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 modules/doctor/usertype.go diff --git a/models/consistency.go b/models/consistency.go index fbb99ca80c883..59e9511d90ae9 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -295,3 +295,13 @@ func FixNullArchivedRepository() (int64, error) { IsArchived: false, }) } + +// CountWrongUserType count OrgUser who have wrong type +func CountWrongUserType() (int64, error) { + return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Count(new(User)) +} + +// FixWrongUserType fix OrgUser who have wrong type +func FixWrongUserType() (int64, error) { + return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").Update(&User{Type: 1}) +} diff --git a/modules/doctor/usertype.go b/modules/doctor/usertype.go new file mode 100644 index 0000000000000..26c0d34cdad71 --- /dev/null +++ b/modules/doctor/usertype.go @@ -0,0 +1,40 @@ +// Copyright 2021 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 doctor + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" +) + +func checkUserType(logger log.Logger, autofix bool) error { + count, err := models.CountWrongUserType() + if err != nil { + logger.Critical("Error: %v whilst counting wrong user types") + return err + } + if count > 0 { + if autofix { + if count, err = models.FixWrongUserType(); err != nil { + logger.Critical("Error: %v whilst fixing wrong user types") + return err + } + logger.Info("%d users with wrong type fixed", count) + } else { + logger.Warn("%d users with wrong type exist", count) + } + } + return nil +} + +func init() { + Register(&Check{ + Title: "Check if user with wrong type exist", + Name: "check-user-type", + IsDefault: true, + Run: checkUserType, + Priority: 3, + }) +} From 512dcf8ffc14cdbdba3aae5f534978327b98cbd6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 29 Jan 2021 18:03:08 +0100 Subject: [PATCH 2/2] use NoAutoTime --- models/consistency.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/consistency.go b/models/consistency.go index 59e9511d90ae9..f689261a55a18 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -291,7 +291,7 @@ func CountNullArchivedRepository() (int64, error) { // FixNullArchivedRepository sets is_archived to false where it is null func FixNullArchivedRepository() (int64, error) { - return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{ + return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").NoAutoTime().Update(&Repository{ IsArchived: false, }) } @@ -303,5 +303,5 @@ func CountWrongUserType() (int64, error) { // FixWrongUserType fix OrgUser who have wrong type func FixWrongUserType() (int64, error) { - return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").Update(&User{Type: 1}) + return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").NoAutoTime().Update(&User{Type: 1}) }