aboutsummaryrefslogtreecommitdiffstats
path: root/models/db/index.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-06-13 17:37:59 +0800
committerGitHub <noreply@github.com>2022-06-13 17:37:59 +0800
commit1a9821f57a0293db3adc0eab8aff08ca5fa1026c (patch)
tree3c3d02813eb63c0d0827ef6d9745f6dcdd2636cb /models/db/index.go
parent3708ca8e2849ca7e36e6bd15ec6935a2a2d81e55 (diff)
downloadgitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.tar.gz
gitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.zip
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
Diffstat (limited to 'models/db/index.go')
-rw-r--r--models/db/index.go23
1 files changed, 11 insertions, 12 deletions
diff --git a/models/db/index.go b/models/db/index.go
index 8598de9498..9b164db1fa 100644
--- a/models/db/index.go
+++ b/models/db/index.go
@@ -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 {
+ ctx, commiter, err := TxContext()
+ if err != nil {
return 0, err
}
+ defer commiter.Close()
var preIdx int64
- _, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx)
- if err != nil {
+ 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