forked from forgejo/forgejo
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>
This commit is contained in:
parent
a0a425a13b
commit
34283a74e8
91 changed files with 252 additions and 176 deletions
|
@ -54,7 +54,7 @@ func AdoptRepository(doer, u *user_model.User, opts repo_module.CreateRepoOption
|
|||
IsEmpty: !opts.AutoInit,
|
||||
}
|
||||
|
||||
if err := db.WithTx(func(ctx context.Context) error {
|
||||
if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||
repoPath := repo_model.RepoPath(u.Name, repo.Name)
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
if err != nil {
|
||||
|
|
|
@ -174,7 +174,7 @@ func (aReq *ArchiveRequest) Await(ctx context.Context) (*repo_model.RepoArchiver
|
|||
}
|
||||
|
||||
func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
|
||||
txCtx, committer, err := db.TxContext()
|
||||
txCtx, committer, err := db.TxContext(db.DefaultContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ func UploadAvatar(repo *repo_model.Repository, data []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext()
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ func DeleteAvatar(repo *repo_model.Repository) error {
|
|||
avatarPath := repo.CustomAvatarRelativePath()
|
||||
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
|
||||
|
||||
ctx, committer, err := db.TxContext()
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -112,7 +112,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
|
|||
panic(panicErr)
|
||||
}()
|
||||
|
||||
err = db.WithTx(func(txCtx context.Context) error {
|
||||
err = db.WithTx(ctx, func(txCtx context.Context) error {
|
||||
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -184,7 +184,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
|
|||
|
||||
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
|
||||
func ConvertForkToNormalRepository(repo *repo_model.Repository) error {
|
||||
err := db.WithTx(func(ctx context.Context) error {
|
||||
err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||
repo, err := repo_model.GetRepositoryByIDCtx(ctx, repo.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -290,7 +290,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
|
|||
|
||||
// PushUpdateAddDeleteTags updates a number of added and delete tags
|
||||
func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
|
||||
return db.WithTx(func(ctx context.Context) error {
|
||||
return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||
if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -90,7 +90,7 @@ func Init() error {
|
|||
|
||||
// UpdateRepository updates a repository
|
||||
func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) {
|
||||
ctx, committer, err := db.TxContext()
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ func GenerateRepository(doer, owner *user_model.User, templateRepo *repo_model.R
|
|||
}
|
||||
|
||||
var generateRepo *repo_model.Repository
|
||||
if err = db.WithTx(func(ctx context.Context) error {
|
||||
if err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||
generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue