summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-01-28 00:11:41 +0800
committerGitHub <noreply@github.com>2017-01-28 00:11:41 +0800
commitbb5a6b7a07125b29604f479f45e59073cb3d4648 (patch)
tree590eeb774f6cb16bc8f8b12dc8f92fb6fd11b443
parent25663b58161c48bdc8e9cae900544378dede0d2b (diff)
downloadgitea-bb5a6b7a07125b29604f479f45e59073cb3d4648.tar.gz
gitea-bb5a6b7a07125b29604f479f45e59073cb3d4648.zip
fix xorm NewSession uncorrected usages (#774)
-rw-r--r--models/access_test.go16
-rw-r--r--models/org.go10
-rw-r--r--models/org_team.go2
-rw-r--r--models/pull_test.go4
-rw-r--r--models/repo.go23
-rw-r--r--models/star.go1
6 files changed, 23 insertions, 33 deletions
diff --git a/models/access_test.go b/models/access_test.go
index 8d8b156ccd..e06db5607b 100644
--- a/models/access_test.go
+++ b/models/access_test.go
@@ -98,16 +98,12 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner())
- sess := x.NewSession()
- defer sess.Close()
- _, err := sess.Delete(&Collaboration{UserID: 2, RepoID: 3})
+ _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
assert.NoError(t, err)
-
assert.NoError(t, repo1.RecalculateAccesses())
- sess = x.NewSession()
access := &Access{UserID: 2, RepoID: 3}
- has, err := sess.Get(access)
+ has, err := x.Get(access)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, AccessModeWrite, access.Mode)
@@ -119,15 +115,11 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
assert.NoError(t, repo1.GetOwner())
- sess := x.NewSession()
- defer sess.Close()
- _, err := sess.Delete(&Collaboration{UserID: 4, RepoID: 4})
+ _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
assert.NoError(t, err)
-
assert.NoError(t, repo1.RecalculateAccesses())
- sess = x.NewSession()
- has, err := sess.Get(&Access{UserID: 4, RepoID: 4})
+ has, err := x.Get(&Access{UserID: 4, RepoID: 4})
assert.NoError(t, err)
assert.False(t, has)
}
diff --git a/models/org.go b/models/org.go
index d44547a0f9..02f7bc4e7d 100644
--- a/models/org.go
+++ b/models/org.go
@@ -345,13 +345,15 @@ func getOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*User, e
// GetOrgsByUserID returns a list of organizations that the given user ID
// has joined.
func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
- return getOrgsByUserID(x.NewSession(), userID, showAll)
+ sess := x.NewSession()
+ defer sess.Close()
+ return getOrgsByUserID(sess, userID, showAll)
}
// GetOrgsByUserIDDesc returns a list of organizations that the given user ID
// has joined, ordered descending by the given condition.
func GetOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, error) {
- return getOrgsByUserID(x.NewSession().Desc(desc), userID, showAll)
+ return getOrgsByUserID(x.Desc(desc), userID, showAll)
}
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
@@ -367,14 +369,14 @@ func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
// GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
sess := x.NewSession()
+ defer sess.Close()
return getOwnedOrgsByUserID(sess, userID)
}
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by
// given user ID, ordered descending by the given condition.
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
- sess := x.NewSession()
- return getOwnedOrgsByUserID(sess.Desc(desc), userID)
+ return getOwnedOrgsByUserID(x.Desc(desc), userID)
}
// GetOrgUsersByUserID returns all organization-user relations by user ID.
diff --git a/models/org_team.go b/models/org_team.go
index 237a6fcf0c..d9ad0ba28f 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -309,7 +309,7 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
}
t.LowerName = strings.ToLower(t.Name)
- has, err := x.
+ has, err := sess.
Where("org_id=?", t.OrgID).
And("lower_name=?", t.LowerName).
And("id!=?", t.ID).
diff --git a/models/pull_test.go b/models/pull_test.go
index f0352be35e..b8794e1311 100644
--- a/models/pull_test.go
+++ b/models/pull_test.go
@@ -225,11 +225,9 @@ func TestChangeUsernameInPullRequests(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
const newUsername = "newusername"
assert.NoError(t, ChangeUsernameInPullRequests("user1", newUsername))
- sess := x.NewSession()
- defer sess.Close()
prs := make([]*PullRequest, 0, 10)
- assert.NoError(t, sess.Where("head_user_name = ?", newUsername).Find(&prs))
+ assert.NoError(t, x.Where("head_user_name = ?", newUsername).Find(&prs))
assert.Len(t, prs, 2)
for _, pr := range prs {
assert.Equal(t, newUsername, pr.HeadUserName)
diff --git a/models/repo.go b/models/repo.go
index c2480d9ddb..0e6665b3fb 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1515,16 +1515,16 @@ func DeleteRepository(uid, repoID int64) error {
return err
}
- if err = sess.Commit(); err != nil {
- return fmt.Errorf("Commit: %v", err)
- }
-
if repo.NumForks > 0 {
- if _, err = x.Exec("UPDATE `repository` SET fork_id=0,is_fork=? WHERE fork_id=?", false, repo.ID); err != nil {
+ if _, err = sess.Exec("UPDATE `repository` SET fork_id=0,is_fork=? WHERE fork_id=?", false, repo.ID); err != nil {
log.Error(4, "reset 'fork_id' and 'is_fork': %v", err)
}
}
+ if err = sess.Commit(); err != nil {
+ return fmt.Errorf("Commit: %v", err)
+ }
+
return nil
}
@@ -2217,30 +2217,29 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
if err != nil {
return nil, err
}
- sessionRelease(sess)
// Copy LFS meta objects in new session
- sess = x.NewSession()
- defer sessionRelease(sess)
- if err = sess.Begin(); err != nil {
+ sess2 := x.NewSession()
+ defer sessionRelease(sess2)
+ if err = sess2.Begin(); err != nil {
return nil, err
}
var lfsObjects []*LFSMetaObject
- if err = sess.Where("repository_id=?", oldRepo.ID).Find(&lfsObjects); err != nil {
+ if err = sess2.Where("repository_id=?", oldRepo.ID).Find(&lfsObjects); err != nil {
return nil, err
}
for _, v := range lfsObjects {
v.ID = 0
v.RepositoryID = repo.ID
- if _, err = sess.Insert(v); err != nil {
+ if _, err = sess2.Insert(v); err != nil {
return nil, err
}
}
- return repo, sess.Commit()
+ return repo, sess2.Commit()
}
// GetForks returns all the forks of the repository
diff --git a/models/star.go b/models/star.go
index 997b223a18..d7366f37b5 100644
--- a/models/star.go
+++ b/models/star.go
@@ -14,7 +14,6 @@ type Star struct {
// StarRepo or unstar repository.
func StarRepo(userID, repoID int64, star bool) error {
sess := x.NewSession()
-
defer sess.Close()
if err := sess.Begin(); err != nil {