0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 00:24:16 +01:00

Use session for rolling back

This commit is contained in:
Unknown 2014-04-04 18:55:17 -04:00
parent eb803ec5eb
commit e41ab839c7
4 changed files with 63 additions and 11 deletions

View file

@ -19,7 +19,7 @@ import (
// Test that go1.2 tag above is included in builds. main.go refers to this definition. // Test that go1.2 tag above is included in builds. main.go refers to this definition.
const go12tag = true const go12tag = true
const APP_VER = "0.2.0.0403 Alpha" const APP_VER = "0.2.0.0404 Alpha"
func init() { func init() {
base.AppVer = APP_VER base.AppVer = APP_VER

View file

@ -7,6 +7,8 @@ package models
import ( import (
"strings" "strings"
"time" "time"
"github.com/lunny/xorm"
) )
// Access types. // Access types.
@ -40,6 +42,15 @@ func UpdateAccess(access *Access) error {
return err return err
} }
// UpdateAccess updates access information with session for rolling back.
func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
if _, err := sess.Id(access.Id).Update(access); err != nil {
sess.Rollback()
return err
}
return nil
}
// HasAccess returns true if someone can read or write to given repository. // HasAccess returns true if someone can read or write to given repository.
func HasAccess(userName, repoName string, mode int) (bool, error) { func HasAccess(userName, repoName string, mode int) (bool, error) {
return orm.Get(&Access{ return orm.Get(&Access{

View file

@ -381,45 +381,62 @@ func TransferOwnership(user *User, newOwner string, repo *Repository) (err error
if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil { if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil {
return err return err
} }
sess := orm.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
for i := range accesses { for i := range accesses {
accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName
if accesses[i].UserName == user.LowerName { if accesses[i].UserName == user.LowerName {
accesses[i].UserName = newUser.LowerName accesses[i].UserName = newUser.LowerName
} }
if err = UpdateAccess(&accesses[i]); err != nil { if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err return err
} }
} }
// Update repository. // Update repository.
repo.OwnerId = newUser.Id repo.OwnerId = newUser.Id
if _, err := orm.Id(repo.Id).Update(repo); err != nil { if _, err := sess.Id(repo.Id).Update(repo); err != nil {
sess.Rollback()
return err return err
} }
// Update user repository number. // Update user repository number.
rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
if _, err = orm.Exec(rawSql, newUser.Id); err != nil { if _, err = sess.Exec(rawSql, newUser.Id); err != nil {
sess.Rollback()
return err return err
} }
rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?"
if _, err = orm.Exec(rawSql, user.Id); err != nil { if _, err = sess.Exec(rawSql, user.Id); err != nil {
sess.Rollback()
return err return err
} }
// Add watch of new owner to repository. // Add watch of new owner to repository.
if !IsWatching(newUser.Id, repo.Id) { if !IsWatching(newUser.Id, repo.Id) {
if err = WatchRepo(newUser.Id, repo.Id, true); err != nil { if err = WatchRepo(newUser.Id, repo.Id, true); err != nil {
sess.Rollback()
return err return err
} }
} }
if err = TransferRepoAction(user, newUser, repo); err != nil { if err = TransferRepoAction(user, newUser, repo); err != nil {
sess.Rollback()
return err return err
} }
// Change repository directory name. // Change repository directory name.
return os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)) if err = os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil {
sess.Rollback()
return err
}
return sess.Commit()
} }
// ChangeRepositoryName changes all corresponding setting from old repository name to new one. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
@ -429,15 +446,27 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error)
if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil { if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil {
return err return err
} }
sess := orm.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
for i := range accesses { for i := range accesses {
accesses[i].RepoName = userName + "/" + newRepoName accesses[i].RepoName = userName + "/" + newRepoName
if err = UpdateAccess(&accesses[i]); err != nil { if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err return err
} }
} }
// Change repository directory name. // Change repository directory name.
return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)) if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil {
sess.Rollback()
return err
}
return sess.Commit()
} }
func UpdateRepository(repo *Repository) error { func UpdateRepository(repo *Repository) error {

View file

@ -218,11 +218,18 @@ func ChangeUserName(user *User, newUserName string) (err error) {
if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil { if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil {
return err return err
} }
sess := orm.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
for i := range accesses { for i := range accesses {
accesses[i].UserName = newUserName accesses[i].UserName = newUserName
if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") { if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") {
accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1) accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1)
if err = UpdateAccess(&accesses[i]); err != nil { if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err return err
} }
} }
@ -241,14 +248,19 @@ func ChangeUserName(user *User, newUserName string) (err error) {
for j := range accesses { for j := range accesses {
accesses[j].RepoName = newUserName + "/" + repos[i].LowerName accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
if err = UpdateAccess(&accesses[j]); err != nil { if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
return err return err
} }
} }
} }
// Change user directory name. // Change user directory name.
return os.Rename(UserPath(user.LowerName), UserPath(newUserName)) if err = os.Rename(UserPath(user.LowerName), UserPath(newUserName)); err != nil {
sess.Rollback()
return err
}
return sess.Commit()
} }
// UpdateUser updates user's information. // UpdateUser updates user's information.