Skip to content

Commit 63ccf82

Browse files
committed
no db.WithTx
1 parent 4b9f09c commit 63ccf82

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

models/git/protected_branch.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -414,34 +414,32 @@ func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, prote
414414
}
415415
protectBranch.ApprovalsWhitelistTeamIDs = whitelist
416416

417-
return db.WithTx(ctx, func(ctx context.Context) error {
418-
// Looks like it's a new rule
419-
if protectBranch.ID == 0 {
420-
// as it's a new rule and if priority was not set, we need to calc it.
421-
if protectBranch.Priority == 0 {
422-
var lowestPrio int64
423-
// because of mssql we can not use builder or save xorm syntax, so raw sql it is
424-
if _, err := db.GetEngine(ctx).SQL(`SELECT MAX(priority) FROM protected_branch WHERE repo_id = ?`, protectBranch.RepoID).
425-
Get(&lowestPrio); err != nil {
426-
return err
427-
}
428-
log.Trace("Create new ProtectedBranch at repo[%d] and detect current lowest priority '%d'", protectBranch.RepoID, lowestPrio)
429-
protectBranch.Priority = lowestPrio + 1
430-
}
431-
432-
if _, err = db.GetEngine(ctx).Insert(protectBranch); err != nil {
433-
return fmt.Errorf("Insert: %v", err)
417+
// Looks like it's a new rule
418+
if protectBranch.ID == 0 {
419+
// as it's a new rule and if priority was not set, we need to calc it.
420+
if protectBranch.Priority == 0 {
421+
var lowestPrio int64
422+
// because of mssql we can not use builder or save xorm syntax, so raw sql it is
423+
if _, err := db.GetEngine(ctx).SQL(`SELECT MAX(priority) FROM protected_branch WHERE repo_id = ?`, protectBranch.RepoID).
424+
Get(&lowestPrio); err != nil {
425+
return err
434426
}
435-
return nil
427+
log.Trace("Create new ProtectedBranch at repo[%d] and detect current lowest priority '%d'", protectBranch.RepoID, lowestPrio)
428+
protectBranch.Priority = lowestPrio + 1
436429
}
437430

438-
// update the rule
439-
if _, err = db.GetEngine(ctx).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
440-
return fmt.Errorf("Update: %v", err)
431+
if _, err = db.GetEngine(ctx).Insert(protectBranch); err != nil {
432+
return fmt.Errorf("Insert: %v", err)
441433
}
442-
443434
return nil
444-
})
435+
}
436+
437+
// update the rule
438+
if _, err = db.GetEngine(ctx).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
439+
return fmt.Errorf("Update: %v", err)
440+
}
441+
442+
return nil
445443
}
446444

447445
func UpdateProtectBranchPriorities(ctx context.Context, repo *repo_model.Repository, ids []int64) error {

0 commit comments

Comments
 (0)