summaryrefslogtreecommitdiffstats
path: root/services/packages
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-11-13 04:18:50 +0800
committerGitHub <noreply@github.com>2022-11-12 21:18:50 +0100
commit34283a74e85278fed2c9b70d6f8749dc6a4001ca (patch)
treef8fa43399cb723d55efc34dacf7947faf0ed5acf /services/packages
parenta0a425a13ba587829a831aaecd8469d39d372111 (diff)
downloadgitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.tar.gz
gitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.zip
Allow detect whether it's in a database transaction for a context.Context (#21756)
Fix #19513 This PR introduce a new db method `InTransaction(context.Context)`, and also builtin check on `db.TxContext` and `db.WithTx`. There is also a new method `db.AutoTx` has been introduced but could be used by other PRs. `WithTx` will always open a new transaction, if a transaction exist in context, return an error. `AutoTx` will try to open a new transaction if no transaction exist in context. That means it will always enter a transaction if there is no error. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'services/packages')
-rw-r--r--services/packages/packages.go8
1 files changed, 4 insertions, 4 deletions
diff --git a/services/packages/packages.go b/services/packages/packages.go
index 443976e174..4513215ae1 100644
--- a/services/packages/packages.go
+++ b/services/packages/packages.go
@@ -76,7 +76,7 @@ func CreatePackageOrAddFileToExisting(pvci *PackageCreationInfo, pfci *PackageFi
}
func createPackageAndAddFile(pvci *PackageCreationInfo, pfci *PackageFileCreationInfo, allowDuplicate bool) (*packages_model.PackageVersion, *packages_model.PackageFile, error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return nil, nil, err
}
@@ -190,7 +190,7 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all
// AddFileToExistingPackage adds a file to an existing package. If the package does not exist, ErrPackageNotExist is returned
func AddFileToExistingPackage(pvi *PackageInfo, pfci *PackageFileCreationInfo) (*packages_model.PackageVersion, *packages_model.PackageFile, error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return nil, nil, err
}
@@ -388,7 +388,7 @@ func RemovePackageVersionByNameAndVersion(doer *user_model.User, pvi *PackageInf
// RemovePackageVersion deletes the package version and all associated files
func RemovePackageVersion(doer *user_model.User, pv *packages_model.PackageVersion) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
@@ -444,7 +444,7 @@ func DeletePackageFile(ctx context.Context, pf *packages_model.PackageFile) erro
// Cleanup removes expired package data
func Cleanup(unused context.Context, olderThan time.Duration) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}