Add Doctor FixWrongUserType (#14522)
* Add Doctor FixWrongUserType * use NoAutoTime
This commit is contained in:
		
							parent
							
								
									05365816ab
								
							
						
					
					
						commit
						0e0424c8ec
					
				
					 2 changed files with 51 additions and 1 deletions
				
			
		|  | @ -291,7 +291,17 @@ func CountNullArchivedRepository() (int64, error) { | ||||||
| 
 | 
 | ||||||
| // FixNullArchivedRepository sets is_archived to false where it is null
 | // FixNullArchivedRepository sets is_archived to false where it is null
 | ||||||
| func FixNullArchivedRepository() (int64, error) { | 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, | 		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").NoAutoTime().Update(&User{Type: 1}) | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										40
									
								
								modules/doctor/usertype.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								modules/doctor/usertype.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -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, | ||||||
|  | 	}) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue