summaryrefslogtreecommitdiffstats
path: root/models/repo.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/repo.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/repo.go')
-rw-r--r--models/repo.go92
1 files changed, 48 insertions, 44 deletions
diff --git a/models/repo.go b/models/repo.go
index 5921441afa..1347de3045 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1276,17 +1276,17 @@ func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err
}
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
- if err = sess.Begin(); err != nil {
- return fmt.Errorf("sess.Begin: %v", err)
+ ctx, committer, err := db.TxContext()
+ if err != nil {
+ return err
}
+ defer committer.Close()
- if err := newRepoRedirect(sess, repo.Owner.ID, repo.ID, oldRepoName, newRepoName); err != nil {
+ if err := newRepoRedirect(db.GetEngine(ctx), repo.Owner.ID, repo.ID, oldRepoName, newRepoName); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
func getRepositoriesByForkID(e db.Engine, forkID int64) ([]*Repository, error) {
@@ -1367,17 +1367,17 @@ func UpdateRepositoryCtx(ctx context.Context, repo *Repository, visibilityChange
// UpdateRepository updates a repository
func UpdateRepository(repo *Repository, visibilityChanged bool) (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 = updateRepository(sess, repo, visibilityChanged); err != nil {
+ if err = updateRepository(db.GetEngine(ctx), repo, visibilityChanged); err != nil {
return fmt.Errorf("updateRepository: %v", err)
}
- return sess.Commit()
+ return committer.Commit()
}
// UpdateRepositoryOwnerNames updates repository owner_names (this should only be used when the ownerName has changed case)
@@ -1385,19 +1385,19 @@ func UpdateRepositoryOwnerNames(ownerID int64, ownerName string) error {
if ownerID == 0 {
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 := sess.Where("owner_id = ?", ownerID).Cols("owner_name").Update(&Repository{
+ if _, err := db.GetEngine(ctx).Where("owner_id = ?", ownerID).Cols("owner_name").Update(&Repository{
OwnerName: ownerName,
}); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
// UpdateRepositoryUpdatedTime updates a repository's updated time
@@ -1408,38 +1408,39 @@ func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
// UpdateRepositoryUnits updates a repository's units
func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (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()
// Delete existing settings of units before adding again
for _, u := range units {
deleteUnitTypes = append(deleteUnitTypes, u.Type)
}
- if _, err = sess.Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
+ if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
return err
}
if len(units) > 0 {
- if _, err = sess.Insert(units); err != nil {
+ if err = db.Insert(ctx, units); err != nil {
return err
}
}
- return sess.Commit()
+ return committer.Commit()
}
// DeleteRepository deletes a repository for a user or organization.
// make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock)
func DeleteRepository(doer *User, uid, repoID int64) 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()
+ sess := db.GetEngine(ctx)
// In case is a organization.
org, err := getUserByID(sess, uid)
@@ -1639,11 +1640,11 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return err
}
- if err = sess.Commit(); err != nil {
+ if err = committer.Commit(); err != nil {
return err
}
- sess.Close()
+ committer.Close()
// We should always delete the files after the database transaction succeed. If
// we delete the file but the database rollback, the repository will be broken.
@@ -1769,15 +1770,14 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error)
cond = cond.And(builder.In("lower_name", opts.LowerNames))
}
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
+ sess := db.GetEngine(db.DefaultContext)
count, err := sess.Where(cond).Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
- sess.Where(cond).OrderBy(opts.OrderBy.String())
+ sess = sess.Where(cond).OrderBy(opts.OrderBy.String())
repos := make([]*Repository, 0, opts.PageSize)
return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
}
@@ -2136,32 +2136,36 @@ func (repo *Repository) GetTrustModel() TrustModelType {
return trustModel
}
+func updateUserStarNumbers(users []User) error {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
+ return err
+ }
+ defer committer.Close()
+
+ for _, user := range users {
+ if _, err = db.Exec(ctx, "UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
+ return err
+ }
+ }
+
+ return committer.Commit()
+}
+
// DoctorUserStarNum recalculate Stars number for all user
func DoctorUserStarNum() (err error) {
const batchSize = 100
- sess := db.NewSession(db.DefaultContext)
- defer sess.Close()
for start := 0; ; start += batchSize {
users := make([]User, 0, batchSize)
- if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
+ if err = db.GetEngine(db.DefaultContext).Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
return
}
if len(users) == 0 {
break
}
- if err = sess.Begin(); err != nil {
- return
- }
-
- for _, user := range users {
- if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
- return
- }
- }
-
- if err = sess.Commit(); err != nil {
+ if err = updateUserStarNumbers(users); err != nil {
return
}
}