Add transaction for CreateRepoByExample

This commit is contained in:
Lunny Xiao 2024-05-27 14:37:19 +08:00
parent 2c02c2eb47
commit d66b9059a2
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 25 additions and 4 deletions

View File

@ -66,7 +66,10 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
} }
} }
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil { // create the repository database operations first
if err := db.WithTx(ctx, func(ctx context.Context) error {
return repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false)
}); err != nil {
return nil, err return nil, err
} }

View File

@ -241,9 +241,11 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
ObjectFormatName: opts.ObjectFormatName, ObjectFormatName: opts.ObjectFormatName,
} }
needsUpdateStatus := opts.Status == 0 needsUpdateStatus := opts.Status != repo_model.RepositoryReady
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, false, false); err != nil { if err := db.WithTx(ctx, func(ctx context.Context) error {
return repo_module.CreateRepositoryByExample(ctx, doer, u, repo, false, false)
}); err != nil {
return nil, err return nil, err
} }

View File

@ -15,8 +15,10 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git" git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
system_model "code.gitea.io/gitea/models/system"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
@ -338,12 +340,26 @@ func generateRepository(ctx context.Context, doer, owner *user_model.User, templ
Status: repo_model.RepositoryBeingMigrated, Status: repo_model.RepositoryBeingMigrated,
} }
if err = repo_module.CreateRepositoryByExample(ctx, doer, owner, generateRepo, false, false); err != nil { if err := db.WithTx(ctx, func(ctx context.Context) error {
return repo_module.CreateRepositoryByExample(ctx, doer, owner, generateRepo, false, false)
}); err != nil {
return nil, err return nil, err
} }
repoPath := generateRepo.RepoPath() repoPath := generateRepo.RepoPath()
isExist, err := util.IsExist(repoPath) isExist, err := util.IsExist(repoPath)
defer func() {
if err != nil {
if errDelete := DeleteRepositoryDirectly(ctx, doer, generateRepo.ID); errDelete != nil {
log.Error("Rollback deleteRepository: %v", errDelete)
// add system notice
if err := system_model.CreateRepositoryNotice("DeleteRepositoryDirectly failed when generate repository: %v", errDelete); err != nil {
log.Error("CreateRepositoryNotice: %v", err)
}
}
}
}()
if err != nil { if err != nil {
log.Error("Unable to check if %s exists. Error: %v", repoPath, err) log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
return nil, err return nil, err