aboutsummaryrefslogtreecommitdiffstats
path: root/models/org_team.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-21 23:41:00 +0800
committerGitHub <noreply@github.com>2021-11-21 23:41:00 +0800
commitd710af6669654f27f02b69d7ef1ba563e7d58a90 (patch)
tree9727f468a570106293dc90beb70035180bbb7e8e /models/org_team.go
parent0add627182388ac63fd04b94cdf912fb87fd0326 (diff)
downloadgitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.tar.gz
gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.zip
Remove NewSession method from db.Engine interface (#17577)
* Remove NewSession method from db.Engine interface * Fix bug * Some improvements * Fix bug * Fix test * Use XXXBean instead of XXXExample
Diffstat (limited to 'models/org_team.go')
-rw-r--r--models/org_team.go121
1 files changed, 52 insertions, 69 deletions
diff --git a/models/org_team.go b/models/org_team.go
index e3ad23c111..1bdffe2cb1 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -83,8 +83,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
+ sess := db.GetEngine(db.DefaultContext)
count, err := sess.
Where(cond).
@@ -265,17 +264,17 @@ func (t *Team) addAllRepositories(e db.Engine) error {
// AddAllRepositories adds all repositories to the team
func (t *Team) AddAllRepositories() (err error) {
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if err = t.addAllRepositories(sess); err != nil {
+ if err = t.addAllRepositories(db.GetEngine(ctx)); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// AddRepository adds new repository to team of organization.
@@ -286,17 +285,17 @@ func (t *Team) AddRepository(repo *Repository) (err error) {
return nil
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if err = t.addRepository(sess, repo); err != nil {
+ if err = t.addRepository(db.GetEngine(ctx), repo); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// RemoveAllRepositories removes all repositories from team and recalculates access
@@ -305,17 +304,17 @@ func (t *Team) RemoveAllRepositories() (err error) {
return nil
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if err = t.removeAllRepositories(sess); err != nil {
+ if err = t.removeAllRepositories(db.GetEngine(ctx)); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// removeAllRepositories removes all repositories from team and recalculates access
@@ -422,17 +421,17 @@ func (t *Team) RemoveRepository(repoID int64) error {
return err
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if err = t.removeRepository(sess, repo, true); err != nil {
+ if err = t.removeRepository(db.GetEngine(ctx), repo, true); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// UnitEnabled returns if the team has the given unit type enabled
@@ -494,17 +493,13 @@ func NewTeam(t *Team) (err error) {
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if _, err = sess.Insert(t); err != nil {
- errRollback := sess.Rollback()
- if errRollback != nil {
- log.Error("NewTeam sess.Rollback: %v", errRollback)
- }
+ if err = db.Insert(ctx, t); err != nil {
return err
}
@@ -513,32 +508,24 @@ func NewTeam(t *Team) (err error) {
for _, unit := range t.Units {
unit.TeamID = t.ID
}
- if _, err = sess.Insert(&t.Units); err != nil {
- errRollback := sess.Rollback()
- if errRollback != nil {
- log.Error("NewTeam sess.Rollback: %v", errRollback)
- }
+ if err = db.Insert(ctx, &t.Units); err != nil {
return err
}
}
// Add all repositories to the team if it has access to all of them.
if t.IncludesAllRepositories {
- err = t.addAllRepositories(sess)
+ err = t.addAllRepositories(db.GetEngine(ctx))
if err != nil {
return fmt.Errorf("addAllRepositories: %v", err)
}
}
// Update organization number of teams.
- if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
- errRollback := sess.Rollback()
- if errRollback != nil {
- log.Error("NewTeam sess.Rollback: %v", errRollback)
- }
+ if _, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
func getTeam(e db.Engine, orgID int64, name string) (*Team, error) {
@@ -624,11 +611,12 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
t.Description = t.Description[:255]
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
+ sess := db.GetEngine(ctx)
t.LowerName = strings.ToLower(t.Name)
has, err := sess.
@@ -659,10 +647,6 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
return err
}
if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil {
- errRollback := sess.Rollback()
- if errRollback != nil {
- log.Error("UpdateTeam sess.Rollback: %v", errRollback)
- }
return err
}
}
@@ -688,7 +672,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
}
}
- return sess.Commit()
+ return committer.Commit()
}
// DeleteTeam deletes given team.
@@ -698,11 +682,12 @@ func DeleteTeam(t *Team) error {
return err
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err := sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
+ sess := db.GetEngine(ctx)
if err := t.getMembers(sess); err != nil {
return err
@@ -736,7 +721,7 @@ func DeleteTeam(t *Team) error {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// ___________ ____ ___
@@ -839,13 +824,15 @@ func AddTeamMember(team *Team, userID int64) error {
return err
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err := sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if _, err := sess.Insert(&TeamUser{
+ sess := db.GetEngine(ctx)
+
+ if err := db.Insert(ctx, &TeamUser{
UID: userID,
OrgID: team.OrgID,
TeamID: team.ID,
@@ -869,7 +856,7 @@ func AddTeamMember(team *Team, userID int64) error {
}
}
- return sess.Commit()
+ return committer.Commit()
}
func removeTeamMember(ctx context.Context, team *Team, userID int64) error {
@@ -1048,25 +1035,21 @@ func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error)
// UpdateTeamUnits updates a teams's units
func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) {
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if _, err = sess.Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
+ if _, err = db.GetEngine(ctx).Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
return err
}
if len(units) > 0 {
- if _, err = sess.Insert(units); err != nil {
- errRollback := sess.Rollback()
- if errRollback != nil {
- log.Error("UpdateTeamUnits sess.Rollback: %v", errRollback)
- }
+ if err = db.Insert(ctx, units); err != nil {
return err
}
}
- return sess.Commit()
+ return committer.Commit()
}