summaryrefslogtreecommitdiffstats
path: root/models/org.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/org.go')
-rw-r--r--models/org.go26
1 files changed, 12 insertions, 14 deletions
diff --git a/models/org.go b/models/org.go
index c6d2562f15..1840cae9fa 100644
--- a/models/org.go
+++ b/models/org.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
+ user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
@@ -163,25 +164,25 @@ func CreateOrganization(org, owner *User) (err error) {
org.NumMembers = 1
org.Type = UserTypeOrganization
- 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 = deleteUserRedirect(sess, org.Name); err != nil {
+ if err = user_model.DeleteUserRedirect(ctx, org.Name); err != nil {
return err
}
- if _, err = sess.Insert(org); err != nil {
+ if err = db.Insert(ctx, org); err != nil {
return fmt.Errorf("insert organization: %v", err)
}
- if err = org.generateRandomAvatar(sess); err != nil {
+ if err = org.generateRandomAvatar(db.GetEngine(ctx)); err != nil {
return fmt.Errorf("generate random avatar: %v", err)
}
// Add initial creator to organization and owner team.
- if _, err = sess.Insert(&OrgUser{
+ if err = db.Insert(ctx, &OrgUser{
UID: owner.ID,
OrgID: org.ID,
}); err != nil {
@@ -198,7 +199,7 @@ func CreateOrganization(org, owner *User) (err error) {
IncludesAllRepositories: true,
CanCreateOrgRepo: true,
}
- if _, err = sess.Insert(t); err != nil {
+ if err = db.Insert(ctx, t); err != nil {
return fmt.Errorf("insert owner team: %v", err)
}
@@ -212,14 +213,11 @@ func CreateOrganization(org, owner *User) (err error) {
})
}
- if _, err = sess.Insert(&units); err != nil {
- if err := sess.Rollback(); err != nil {
- log.Error("CreateOrganization: sess.Rollback: %v", err)
- }
+ if err = db.Insert(ctx, &units); err != nil {
return err
}
- if _, err = sess.Insert(&TeamUser{
+ if err = db.Insert(ctx, &TeamUser{
UID: owner.ID,
OrgID: org.ID,
TeamID: t.ID,
@@ -227,7 +225,7 @@ func CreateOrganization(org, owner *User) (err error) {
return fmt.Errorf("insert team-user relation: %v", err)
}
- return sess.Commit()
+ return committer.Commit()
}
// GetOrgByName returns organization by given name.