diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-06-13 17:37:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 17:37:59 +0800 |
commit | 1a9821f57a0293db3adc0eab8aff08ca5fa1026c (patch) | |
tree | 3c3d02813eb63c0d0827ef6d9745f6dcdd2636cb /models/db | |
parent | 3708ca8e2849ca7e36e6bd15ec6935a2a2d81e55 (diff) | |
download | gitea-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')
-rw-r--r-- | models/db/consistency.go | 27 | ||||
-rw-r--r-- | models/db/index.go | 23 | ||||
-rw-r--r-- | models/db/list_options.go | 5 |
3 files changed, 43 insertions, 12 deletions
diff --git a/models/db/consistency.go b/models/db/consistency.go new file mode 100644 index 0000000000..7addb174c4 --- /dev/null +++ b/models/db/consistency.go @@ -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 +} 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 diff --git a/models/db/list_options.go b/models/db/list_options.go index 843e73c8ae..d1d52b6667 100644 --- a/models/db/list_options.go +++ b/models/db/list_options.go @@ -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) |