mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	Move issues related files into models/issues (#19931)
* Move access and repo permission to models/perm/access * fix test * fix git test * Move functions sequence * Some improvements per @KN4CK3R and @delvh * Move issues related code to models/issues * Move some issues related sub package * Merge * Fix test * Fix test * Fix test * Fix test * Rename some files
This commit is contained in:
		
							
								
								
									
										27
									
								
								models/db/consistency.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								models/db/consistency.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| // Copyright 2022 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 db | ||||
|  | ||||
| import "xorm.io/builder" | ||||
|  | ||||
| // CountOrphanedObjects count subjects with have no existing refobject anymore | ||||
| func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) { | ||||
| 	return GetEngine(DefaultContext).Table("`"+subject+"`"). | ||||
| 		Join("LEFT", "`"+refobject+"`", joinCond). | ||||
| 		Where(builder.IsNull{"`" + refobject + "`.id"}). | ||||
| 		Select("COUNT(`" + subject + "`.`id`)"). | ||||
| 		Count() | ||||
| } | ||||
|  | ||||
| // DeleteOrphanedObjects delete subjects with have no existing refobject anymore | ||||
| func DeleteOrphanedObjects(subject, refobject, joinCond string) error { | ||||
| 	subQuery := builder.Select("`"+subject+"`.id"). | ||||
| 		From("`"+subject+"`"). | ||||
| 		Join("LEFT", "`"+refobject+"`", joinCond). | ||||
| 		Where(builder.IsNull{"`" + refobject + "`.id"}) | ||||
| 	b := builder.Delete(builder.In("id", subQuery)).From("`" + subject + "`") | ||||
| 	_, err := GetEngine(DefaultContext).Exec(b) | ||||
| 	return err | ||||
| } | ||||
| @@ -20,21 +20,21 @@ type ResourceIndex struct { | ||||
| } | ||||
|  | ||||
| // UpsertResourceIndex the function will not return until it acquires the lock or receives an error. | ||||
| func UpsertResourceIndex(e Engine, tableName string, groupID int64) (err error) { | ||||
| func UpsertResourceIndex(ctx context.Context, tableName string, groupID int64) (err error) { | ||||
| 	// An atomic UPSERT operation (INSERT/UPDATE) is the only operation | ||||
| 	// that ensures that the key is actually locked. | ||||
| 	switch { | ||||
| 	case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL: | ||||
| 		_, err = e.Exec(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ | ||||
| 		_, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ | ||||
| 			"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1", | ||||
| 			tableName, tableName), groupID) | ||||
| 	case setting.Database.UseMySQL: | ||||
| 		_, err = e.Exec(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ | ||||
| 		_, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ | ||||
| 			"VALUES (?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", tableName), | ||||
| 			groupID) | ||||
| 	case setting.Database.UseMSSQL: | ||||
| 		// https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/ | ||||
| 		_, err = e.Exec(fmt.Sprintf("MERGE %s WITH (HOLDLOCK) as target "+ | ||||
| 		_, err = Exec(ctx, fmt.Sprintf("MERGE %s WITH (HOLDLOCK) as target "+ | ||||
| 			"USING (SELECT ? AS group_id) AS src "+ | ||||
| 			"ON src.group_id = target.group_id "+ | ||||
| 			"WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+ | ||||
| @@ -82,30 +82,29 @@ func DeleteResouceIndex(ctx context.Context, tableName string, groupID int64) er | ||||
|  | ||||
| // getNextResourceIndex return the next index | ||||
| func getNextResourceIndex(tableName string, groupID int64) (int64, error) { | ||||
| 	sess := x.NewSession() | ||||
| 	defer sess.Close() | ||||
| 	if err := sess.Begin(); err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	var preIdx int64 | ||||
| 	_, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx) | ||||
| 	ctx, commiter, err := TxContext() | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	defer commiter.Close() | ||||
| 	var preIdx int64 | ||||
| 	if _, err := GetEngine(ctx).SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx); err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
|  | ||||
| 	if err := UpsertResourceIndex(sess, tableName, groupID); err != nil { | ||||
| 	if err := UpsertResourceIndex(ctx, tableName, groupID); err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
|  | ||||
| 	var curIdx int64 | ||||
| 	has, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ? AND max_index=?", tableName), groupID, preIdx+1).Get(&curIdx) | ||||
| 	has, err := GetEngine(ctx).SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ? AND max_index=?", tableName), groupID, preIdx+1).Get(&curIdx) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	if !has { | ||||
| 		return 0, ErrResouceOutdated | ||||
| 	} | ||||
| 	if err := sess.Commit(); err != nil { | ||||
| 	if err := commiter.Commit(); err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	return curIdx, nil | ||||
|   | ||||
| @@ -10,6 +10,11 @@ import ( | ||||
| 	"xorm.io/xorm" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	// DefaultMaxInSize represents default variables number on IN () in SQL | ||||
| 	DefaultMaxInSize = 50 | ||||
| ) | ||||
|  | ||||
| // Paginator is the base for different ListOptions types | ||||
| type Paginator interface { | ||||
| 	GetSkipTake() (skip, take int) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user