fix xorm NewSession uncorrected usages (#774)

This commit is contained in:
Lunny Xiao 2017-01-28 00:11:41 +08:00 committed by GitHub
parent 25663b5816
commit bb5a6b7a07
6 changed files with 23 additions and 33 deletions

View file

@ -98,16 +98,12 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner()) assert.NoError(t, repo1.GetOwner())
sess := x.NewSession() _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
defer sess.Close()
_, err := sess.Delete(&Collaboration{UserID: 2, RepoID: 3})
assert.NoError(t, err) assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses()) assert.NoError(t, repo1.RecalculateAccesses())
sess = x.NewSession()
access := &Access{UserID: 2, RepoID: 3} access := &Access{UserID: 2, RepoID: 3}
has, err := sess.Get(access) has, err := x.Get(access)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, has) assert.True(t, has)
assert.Equal(t, AccessModeWrite, access.Mode) assert.Equal(t, AccessModeWrite, access.Mode)
@ -119,15 +115,11 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
assert.NoError(t, repo1.GetOwner()) assert.NoError(t, repo1.GetOwner())
sess := x.NewSession() _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
defer sess.Close()
_, err := sess.Delete(&Collaboration{UserID: 4, RepoID: 4})
assert.NoError(t, err) assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses()) assert.NoError(t, repo1.RecalculateAccesses())
sess = x.NewSession() has, err := x.Get(&Access{UserID: 4, RepoID: 4})
has, err := sess.Get(&Access{UserID: 4, RepoID: 4})
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, has) assert.False(t, has)
} }

View file

@ -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 // GetOrgsByUserID returns a list of organizations that the given user ID
// has joined. // has joined.
func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) { 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 // GetOrgsByUserIDDesc returns a list of organizations that the given user ID
// has joined, ordered descending by the given condition. // has joined, ordered descending by the given condition.
func GetOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, error) { 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) { 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. // GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
func GetOwnedOrgsByUserID(userID int64) ([]*User, error) { func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close()
return getOwnedOrgsByUserID(sess, userID) return getOwnedOrgsByUserID(sess, userID)
} }
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by // GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by
// given user ID, ordered descending by the given condition. // given user ID, ordered descending by the given condition.
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
sess := x.NewSession() return getOwnedOrgsByUserID(x.Desc(desc), userID)
return getOwnedOrgsByUserID(sess.Desc(desc), userID)
} }
// GetOrgUsersByUserID returns all organization-user relations by user ID. // GetOrgUsersByUserID returns all organization-user relations by user ID.

View file

@ -309,7 +309,7 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
} }
t.LowerName = strings.ToLower(t.Name) t.LowerName = strings.ToLower(t.Name)
has, err := x. has, err := sess.
Where("org_id=?", t.OrgID). Where("org_id=?", t.OrgID).
And("lower_name=?", t.LowerName). And("lower_name=?", t.LowerName).
And("id!=?", t.ID). And("id!=?", t.ID).

View file

@ -225,11 +225,9 @@ func TestChangeUsernameInPullRequests(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
const newUsername = "newusername" const newUsername = "newusername"
assert.NoError(t, ChangeUsernameInPullRequests("user1", newUsername)) assert.NoError(t, ChangeUsernameInPullRequests("user1", newUsername))
sess := x.NewSession()
defer sess.Close()
prs := make([]*PullRequest, 0, 10) 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) assert.Len(t, prs, 2)
for _, pr := range prs { for _, pr := range prs {
assert.Equal(t, newUsername, pr.HeadUserName) assert.Equal(t, newUsername, pr.HeadUserName)

View file

@ -1515,16 +1515,16 @@ func DeleteRepository(uid, repoID int64) error {
return err return err
} }
if err = sess.Commit(); err != nil {
return fmt.Errorf("Commit: %v", err)
}
if repo.NumForks > 0 { 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) 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 return nil
} }
@ -2217,30 +2217,29 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
if err != nil { if err != nil {
return nil, err return nil, err
} }
sessionRelease(sess)
// Copy LFS meta objects in new session // Copy LFS meta objects in new session
sess = x.NewSession() sess2 := x.NewSession()
defer sessionRelease(sess) defer sessionRelease(sess2)
if err = sess.Begin(); err != nil { if err = sess2.Begin(); err != nil {
return nil, err return nil, err
} }
var lfsObjects []*LFSMetaObject 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 return nil, err
} }
for _, v := range lfsObjects { for _, v := range lfsObjects {
v.ID = 0 v.ID = 0
v.RepositoryID = repo.ID v.RepositoryID = repo.ID
if _, err = sess.Insert(v); err != nil { if _, err = sess2.Insert(v); err != nil {
return nil, err return nil, err
} }
} }
return repo, sess.Commit() return repo, sess2.Commit()
} }
// GetForks returns all the forks of the repository // GetForks returns all the forks of the repository

View file

@ -14,7 +14,6 @@ type Star struct {
// StarRepo or unstar repository. // StarRepo or unstar repository.
func StarRepo(userID, repoID int64, star bool) error { func StarRepo(userID, repoID int64, star bool) error {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if err := sess.Begin(); err != nil { if err := sess.Begin(); err != nil {