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
This commit is contained in:
Lunny Xiao 2021-11-21 23:41:00 +08:00 committed by GitHub
parent 0add627182
commit d710af6669
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 600 additions and 620 deletions

View file

@ -614,12 +614,13 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist
// RenameBranch rename a branch // RenameBranch rename a branch
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) { func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// 1. update default branch if needed // 1. update default branch if needed
isDefault := repo.DefaultBranch == from isDefault := repo.DefaultBranch == from
if isDefault { if isDefault {
@ -663,10 +664,10 @@ func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault b
From: from, From: from,
To: to, To: to,
} }
_, err = sess.Insert(renamedBranch) err = db.Insert(ctx, renamedBranch)
if err != nil { if err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -6,11 +6,11 @@ package db
import ( import (
"context" "context"
"database/sql"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// DefaultContext is the default context to run xorm queries in // DefaultContext is the default context to run xorm queries in
@ -44,15 +44,6 @@ func (ctx *Context) Engine() Engine {
return ctx.e return ctx.e
} }
// NewSession returns a new session
func (ctx *Context) NewSession() *xorm.Session {
e, ok := ctx.e.(*xorm.Engine)
if ok {
return e.NewSession()
}
return nil
}
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object // Value shadows Value for context.Context but allows us to get ourselves and an Engined object
func (ctx *Context) Value(key interface{}) interface{} { func (ctx *Context) Value(key interface{}) interface{} {
if key == EnginedContextKey { if key == EnginedContextKey {
@ -64,7 +55,6 @@ func (ctx *Context) Value(key interface{}) interface{} {
// Engined structs provide an Engine // Engined structs provide an Engine
type Engined interface { type Engined interface {
Engine() Engine Engine() Engine
NewSession() *xorm.Session
} }
// GetEngine will get a db Engine from this context or return an Engine restricted to this context // GetEngine will get a db Engine from this context or return an Engine restricted to this context
@ -79,24 +69,6 @@ func GetEngine(ctx context.Context) Engine {
return x.Context(ctx) return x.Context(ctx)
} }
// NewSession will get a db Session from this context or return a session restricted to this context
func NewSession(ctx context.Context) *xorm.Session {
if engined, ok := ctx.(Engined); ok {
return engined.NewSession()
}
enginedInterface := ctx.Value(EnginedContextKey)
if enginedInterface != nil {
sess := enginedInterface.(Engined).NewSession()
if sess != nil {
return sess.Context(ctx)
}
return nil
}
return x.NewSession().Context(ctx)
}
// Committer represents an interface to Commit or Close the Context // Committer represents an interface to Commit or Close the Context
type Committer interface { type Committer interface {
Commit() error Commit() error
@ -155,3 +127,28 @@ func Insert(ctx context.Context, beans ...interface{}) error {
_, err := GetEngine(ctx).Insert(beans...) _, err := GetEngine(ctx).Insert(beans...)
return err return err
} }
// Exec executes a sql with args
func Exec(ctx context.Context, sqlAndArgs ...interface{}) (sql.Result, error) {
return GetEngine(ctx).Exec(sqlAndArgs...)
}
// GetByBean filled empty fields of the bean according non-empty fields to query in database.
func GetByBean(ctx context.Context, bean interface{}) (bool, error) {
return GetEngine(ctx).Get(bean)
}
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Delete(bean)
}
// CountByBean counts the number of database records according non-empty fields of the bean as conditions.
func CountByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Count(bean)
}
// TableName returns the table name according a bean object
func TableName(bean interface{}) string {
return x.TableName(bean)
}

View file

@ -55,6 +55,7 @@ type Engine interface {
Asc(colNames ...string) *xorm.Session Asc(colNames ...string) *xorm.Session
Desc(colNames ...string) *xorm.Session Desc(colNames ...string) *xorm.Session
Limit(limit int, start ...int) *xorm.Session Limit(limit int, start ...int) *xorm.Session
NoAutoTime() *xorm.Session
SumInt(bean interface{}, columnName string) (res int64, err error) SumInt(bean interface{}, columnName string) (res int64, err error)
Sync2(...interface{}) error Sync2(...interface{}) error
Select(string) *xorm.Session Select(string) *xorm.Session

View file

@ -24,7 +24,7 @@ func GetPaginatedSession(p Paginator) *xorm.Session {
} }
// SetSessionPagination sets pagination for a database session // SetSessionPagination sets pagination for a database session
func SetSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session { func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
skip, take := p.GetSkipTake() skip, take := p.GetSkipTake()
return sess.Limit(take, skip) return sess.Limit(take, skip)

View file

@ -1349,10 +1349,9 @@ func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64)
// CountIssuesByRepo map from repoID to number of issues matching the options // CountIssuesByRepo map from repoID to number of issues matching the options
func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) { func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
sess := db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
defer sess.Close()
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSession(sess) opts.setupSession(sess)
@ -1377,10 +1376,9 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
// GetRepoIDsForIssuesOptions find all repo ids for the given options // GetRepoIDsForIssuesOptions find all repo ids for the given options
func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *User) ([]int64, error) { func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *User) ([]int64, error) {
repoIDs := make([]int64, 0, 5) repoIDs := make([]int64, 0, 5)
sess := db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
defer sess.Close()
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSession(sess) opts.setupSession(sess)
@ -1397,10 +1395,9 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *User) ([]int64, error
// Issues returns a list of issues by given conditions. // Issues returns a list of issues by given conditions.
func Issues(opts *IssuesOptions) ([]*Issue, error) { func Issues(opts *IssuesOptions) ([]*Issue, error) {
sess := db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
defer sess.Close()
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSession(sess) opts.setupSession(sess)
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID) sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
@ -1419,15 +1416,14 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
// CountIssues number return of issues by given conditions. // CountIssues number return of issues by given conditions.
func CountIssues(opts *IssuesOptions) (int64, error) { func CountIssues(opts *IssuesOptions) (int64, error) {
sess := db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
defer sess.Close()
countsSlice := make([]*struct { countsSlice := make([]*struct {
RepoID int64 RepoID int64
Count int64 Count int64
}, 0, 1) }, 0, 1)
sess.Select("COUNT(issue.id) AS count").Table("issue") sess := e.Select("COUNT(issue.id) AS count").Table("issue")
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSession(sess) opts.setupSession(sess)
if err := sess.Find(&countsSlice); err != nil { if err := sess.Find(&countsSlice); err != nil {
@ -1901,7 +1897,6 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *Us
if issue.DeadlineUnix == deadlineUnix { if issue.DeadlineUnix == deadlineUnix {
return nil return nil
} }
ctx, committer, err := db.TxContext() ctx, committer, err := db.TxContext()
if err != nil { if err != nil {
return err return err

View file

@ -1098,17 +1098,17 @@ func UpdateComment(c *Comment, doer *User) error {
// DeleteComment deletes the comment // DeleteComment deletes the comment
func DeleteComment(comment *Comment) error { func DeleteComment(comment *Comment) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := deleteComment(db.GetEngine(ctx), comment); err != nil {
return err return err
} }
if err := deleteComment(sess, comment); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func deleteComment(e db.Engine, comment *Comment) error { func deleteComment(e db.Engine, comment *Comment) error {

View file

@ -61,7 +61,7 @@ func CreateIssueDependency(user *User, issue, dep *Issue) error {
return ErrCircularDependency{issue.ID, dep.ID} return ErrCircularDependency{issue.ID, dep.ID}
} }
if _, err := sess.Insert(&IssueDependency{ if err := db.Insert(ctx, &IssueDependency{
UserID: user.ID, UserID: user.ID,
IssueID: issue.ID, IssueID: issue.ID,
DependencyID: dep.ID, DependencyID: dep.ID,

View file

@ -297,11 +297,13 @@ func DeleteLabel(id, labelID int64) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if label.BelongsToOrg() && label.OrgID != id { if label.BelongsToOrg() && label.OrgID != id {
return nil return nil
@ -323,7 +325,7 @@ func DeleteLabel(id, labelID int64) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// getLabelByID returns a label by label id // getLabelByID returns a label by label id

View file

@ -85,22 +85,22 @@ func (m *Milestone) State() api.StateType {
// NewMilestone creates new milestone of repository. // NewMilestone creates new milestone of repository.
func NewMilestone(m *Milestone) (err error) { func NewMilestone(m *Milestone) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
m.Name = strings.TrimSpace(m.Name) m.Name = strings.TrimSpace(m.Name)
if _, err = sess.Insert(m); err != nil { if err = db.Insert(ctx, m); err != nil {
return err return err
} }
if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil { if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func getMilestoneByRepoID(e db.Engine, repoID, id int64) (*Milestone, error) { func getMilestoneByRepoID(e db.Engine, repoID, id int64) (*Milestone, error) {
@ -150,11 +150,13 @@ func getMilestoneByID(e db.Engine, id int64) (*Milestone, error) {
// UpdateMilestone updates information of given milestone. // UpdateMilestone updates information of given milestone.
func UpdateMilestone(m *Milestone, oldIsClosed bool) error { func UpdateMilestone(m *Milestone, oldIsClosed bool) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if m.IsClosed && !oldIsClosed { if m.IsClosed && !oldIsClosed {
m.ClosedDateUnix = timeutil.TimeStampNow() m.ClosedDateUnix = timeutil.TimeStampNow()
@ -171,7 +173,7 @@ func UpdateMilestone(m *Milestone, oldIsClosed bool) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
func updateMilestone(e db.Engine, m *Milestone) error { func updateMilestone(e db.Engine, m *Milestone) error {
@ -207,11 +209,13 @@ func updateMilestoneCounters(e db.Engine, id int64) error {
// ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo. // ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool) error { func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
m := &Milestone{ m := &Milestone{
ID: milestoneID, ID: milestoneID,
@ -229,22 +233,22 @@ func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// ChangeMilestoneStatus changes the milestone open/closed status. // ChangeMilestoneStatus changes the milestone open/closed status.
func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := changeMilestoneStatus(db.GetEngine(ctx), m, isClosed); err != nil {
return err return err
} }
if err := changeMilestoneStatus(sess, m, isClosed); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func changeMilestoneStatus(e db.Engine, m *Milestone, isClosed bool) error { func changeMilestoneStatus(e db.Engine, m *Milestone, isClosed bool) error {
@ -335,11 +339,13 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil { if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
return err return err
@ -360,10 +366,10 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
return err return err
} }
if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil { if _, err = db.Exec(ctx, "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// MilestoneList is a list of milestones offering additional functionality // MilestoneList is a list of milestones offering additional functionality

View file

@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// Reaction represents a reactions on issues and comments. // Reaction represents a reactions on issues and comments.
@ -102,7 +101,7 @@ func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, error)
return reactions, e.Find(&reactions) return reactions, e.Find(&reactions)
} }
func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { func createReaction(e db.Engine, opts *ReactionOptions) (*Reaction, error) {
reaction := &Reaction{ reaction := &Reaction{
Type: opts.Type, Type: opts.Type,
UserID: opts.Doer.ID, UserID: opts.Doer.ID,
@ -148,18 +147,18 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
return nil, ErrForbiddenIssueReaction{opts.Type} return nil, ErrForbiddenIssueReaction{opts.Type}
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
reaction, err := createReaction(sess, opts) reaction, err := createReaction(db.GetEngine(ctx), opts)
if err != nil { if err != nil {
return reaction, err return reaction, err
} }
if err := sess.Commit(); err != nil { if err := committer.Commit(); err != nil {
return nil, err return nil, err
} }
return reaction, nil return reaction, nil
@ -203,17 +202,17 @@ func deleteReaction(e db.Engine, opts *ReactionOptions) error {
// DeleteReaction deletes reaction for issue or comment. // DeleteReaction deletes reaction for issue or comment.
func DeleteReaction(opts *ReactionOptions) error { func DeleteReaction(opts *ReactionOptions) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := deleteReaction(db.GetEngine(ctx), opts); err != nil {
return err return err
} }
if err := deleteReaction(sess, opts); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// DeleteIssueReaction deletes a reaction on issue. // DeleteIssueReaction deletes a reaction on issue.

View file

@ -44,27 +44,27 @@ var ErrLFSObjectNotExist = errors.New("LFS Meta object does not exist")
func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
var err error var err error
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
has, err := sess.Get(m) has, err := db.GetByBean(ctx, m)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if has { if has {
m.Existing = true m.Existing = true
return m, sess.Commit() return m, committer.Commit()
} }
if _, err = sess.Insert(m); err != nil { if err = db.Insert(ctx, m); err != nil {
return nil, err return nil, err
} }
return m, sess.Commit() return m, committer.Commit()
} }
// GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID. // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID.
@ -92,29 +92,28 @@ func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) (int64, error) {
return 0, ErrLFSObjectNotExist return 0, ErrLFSObjectNotExist
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return 0, err
return -1, err
} }
defer committer.Close()
m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repo.ID} m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repo.ID}
if _, err := sess.Delete(m); err != nil { if _, err := db.DeleteByBean(ctx, m); err != nil {
return -1, err return -1, err
} }
count, err := sess.Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) count, err := db.CountByBean(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
if err != nil { if err != nil {
return count, err return count, err
} }
return count, sess.Commit() return count, committer.Commit()
} }
// GetLFSMetaObjects returns all LFSMetaObjects associated with a repository // GetLFSMetaObjects returns all LFSMetaObjects associated with a repository
func (repo *Repository) GetLFSMetaObjects(page, pageSize int) ([]*LFSMetaObject, error) { func (repo *Repository) GetLFSMetaObjects(page, pageSize int) ([]*LFSMetaObject, error) {
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
defer sess.Close()
if page >= 0 && pageSize > 0 { if page >= 0 && pageSize > 0 {
start := 0 start := 0
@ -145,11 +144,13 @@ func LFSObjectAccessible(user *User, oid string) (bool, error) {
// LFSAutoAssociate auto associates accessible LFSMetaObjects // LFSAutoAssociate auto associates accessible LFSMetaObjects
func LFSAutoAssociate(metas []*LFSMetaObject, user *User, repoID int64) error { func LFSAutoAssociate(metas []*LFSMetaObject, user *User, repoID int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
oids := make([]interface{}, len(metas)) oids := make([]interface{}, len(metas))
oidMap := make(map[string]*LFSMetaObject, len(metas)) oidMap := make(map[string]*LFSMetaObject, len(metas))
@ -171,20 +172,21 @@ func LFSAutoAssociate(metas []*LFSMetaObject, user *User, repoID int64) error {
newMetas[i].Size = oidMap[newMetas[i].Oid].Size newMetas[i].Size = oidMap[newMetas[i].Oid].Size
newMetas[i].RepositoryID = repoID newMetas[i].RepositoryID = repoID
} }
if _, err := sess.InsertMulti(newMetas); err != nil { if err := db.Insert(ctx, newMetas); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// IterateLFS iterates lfs object // IterateLFS iterates lfs object
func IterateLFS(f func(mo *LFSMetaObject) error) error { func IterateLFS(f func(mo *LFSMetaObject) error) error {
var start int var start int
const batchSize = 100 const batchSize = 100
var e = db.GetEngine(db.DefaultContext)
for { for {
mos := make([]*LFSMetaObject, 0, batchSize) mos := make([]*LFSMetaObject, 0, batchSize)
if err := db.GetEngine(db.DefaultContext).Limit(batchSize, start).Find(&mos); err != nil { if err := e.Limit(batchSize, start).Find(&mos); err != nil {
return err return err
} }
if len(mos) == 0 { if len(mos) == 0 {

View file

@ -105,18 +105,16 @@ func GetLFSLockByID(id int64) (*LFSLock, error) {
// GetLFSLockByRepoID returns a list of locks of repository. // GetLFSLockByRepoID returns a list of locks of repository.
func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) { func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
sess := db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
defer sess.Close()
if page >= 0 && pageSize > 0 { if page >= 0 && pageSize > 0 {
start := 0 start := 0
if page > 0 { if page > 0 {
start = (page - 1) * pageSize start = (page - 1) * pageSize
} }
sess.Limit(pageSize, start) e.Limit(pageSize, start)
} }
lfsLocks := make([]*LFSLock, 0, pageSize) lfsLocks := make([]*LFSLock, 0, pageSize)
return lfsLocks, sess.Find(&lfsLocks, &LFSLock{RepoID: repoID}) return lfsLocks, e.Find(&lfsLocks, &LFSLock{RepoID: repoID})
} }
// CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository. // CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.

View file

@ -190,11 +190,12 @@ type UpdateOAuth2ApplicationOptions struct {
// UpdateOAuth2Application updates an oauth2 application // UpdateOAuth2Application updates an oauth2 application
func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
if err := sess.Begin(); err != nil { if err != nil {
return nil, err return nil, err
} }
defer sess.Close() defer committer.Close()
sess := db.GetEngine(ctx)
app, err := getOAuth2ApplicationByID(sess, opts.ID) app, err := getOAuth2ApplicationByID(sess, opts.ID)
if err != nil { if err != nil {
@ -212,7 +213,7 @@ func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Applic
} }
app.ClientSecret = "" app.ClientSecret = ""
return app, sess.Commit() return app, committer.Commit()
} }
func updateOAuth2Application(e db.Engine, app *OAuth2Application) error { func updateOAuth2Application(e db.Engine, app *OAuth2Application) error {
@ -222,7 +223,7 @@ func updateOAuth2Application(e db.Engine, app *OAuth2Application) error {
return nil return nil
} }
func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error { func deleteOAuth2Application(sess db.Engine, id, userid int64) error {
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil { if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
return err return err
} else if deleted == 0 { } else if deleted == 0 {
@ -251,15 +252,15 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
// DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app. // DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app.
func DeleteOAuth2Application(id, userid int64) error { func DeleteOAuth2Application(id, userid int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
if err := deleteOAuth2Application(sess, id, userid); err != nil { defer committer.Close()
if err := deleteOAuth2Application(db.GetEngine(ctx), id, userid); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// ListOAuth2Applications returns a list of oauth2 applications belongs to given user. // ListOAuth2Applications returns a list of oauth2 applications belongs to given user.

View file

@ -36,23 +36,23 @@ func ReadSession(key string) (*Session, error) {
session := Session{ session := Session{
Key: key, Key: key,
} }
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return nil, err
}
if has, err := sess.Get(&session); err != nil { ctx, committer, err := db.TxContext()
return nil, err
} else if !has {
session.Expiry = timeutil.TimeStampNow()
_, err := sess.Insert(&session)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer committer.Close()
if has, err := db.GetByBean(ctx, &session); err != nil {
return nil, err
} else if !has {
session.Expiry = timeutil.TimeStampNow()
if err := db.Insert(ctx, &session); err != nil {
return nil, err
}
} }
return &session, sess.Commit() return &session, committer.Commit()
} }
// ExistSession checks if a session exists // ExistSession checks if a session exists
@ -73,13 +73,13 @@ func DestroySession(key string) error {
// RegenerateSession regenerates a session from the old id // RegenerateSession regenerates a session from the old id
func RegenerateSession(oldKey, newKey string) (*Session, error) { func RegenerateSession(oldKey, newKey string) (*Session, error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
if has, err := sess.Get(&Session{ if has, err := db.GetByBean(ctx, &Session{
Key: newKey, Key: newKey,
}); err != nil { }); err != nil {
return nil, err return nil, err
@ -87,32 +87,31 @@ func RegenerateSession(oldKey, newKey string) (*Session, error) {
return nil, fmt.Errorf("session Key: %s already exists", newKey) return nil, fmt.Errorf("session Key: %s already exists", newKey)
} }
if has, err := sess.Get(&Session{ if has, err := db.GetByBean(ctx, &Session{
Key: oldKey, Key: oldKey,
}); err != nil { }); err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
_, err := sess.Insert(&Session{ if err := db.Insert(ctx, &Session{
Key: oldKey, Key: oldKey,
Expiry: timeutil.TimeStampNow(), Expiry: timeutil.TimeStampNow(),
}) }); err != nil {
if err != nil {
return nil, err return nil, err
} }
} }
if _, err := sess.Exec("UPDATE "+sess.Engine().TableName(&Session{})+" SET `key` = ? WHERE `key`=?", newKey, oldKey); err != nil { if _, err := db.Exec(ctx, "UPDATE "+db.TableName(&Session{})+" SET `key` = ? WHERE `key`=?", newKey, oldKey); err != nil {
return nil, err return nil, err
} }
s := Session{ s := Session{
Key: newKey, Key: newKey,
} }
if _, err := sess.Get(&s); err != nil { if _, err := db.GetByBean(ctx, &s); err != nil {
return nil, err return nil, err
} }
return &s, sess.Commit() return &s, committer.Commit()
} }
// CountSessions returns the number of sessions // CountSessions returns the number of sessions

View file

@ -9,7 +9,6 @@ import (
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// InsertMilestones creates milestones of repository. // InsertMilestones creates milestones of repository.
@ -18,11 +17,12 @@ func InsertMilestones(ms ...*Milestone) (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// to return the id, so we should not use batch insert // to return the id, so we should not use batch insert
for _, m := range ms { for _, m := range ms {
@ -31,29 +31,29 @@ func InsertMilestones(ms ...*Milestone) (err error) {
} }
} }
if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil { if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// InsertIssues insert issues to database // InsertIssues insert issues to database
func InsertIssues(issues ...*Issue) error { func InsertIssues(issues ...*Issue) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
for _, issue := range issues { for _, issue := range issues {
if err := insertIssue(sess, issue); err != nil { if err := insertIssue(db.GetEngine(ctx), issue); err != nil {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }
func insertIssue(sess *xorm.Session, issue *Issue) error { func insertIssue(sess db.Engine, issue *Issue) error {
if _, err := sess.NoAutoTime().Insert(issue); err != nil { if _, err := sess.NoAutoTime().Insert(issue); err != nil {
return err return err
} }
@ -144,13 +144,13 @@ func InsertIssueComments(comments []*Comment) error {
issueIDs[comment.IssueID] = true issueIDs[comment.IssueID] = true
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
for _, comment := range comments { for _, comment := range comments {
if _, err := sess.NoAutoTime().Insert(comment); err != nil { if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil {
return err return err
} }
@ -159,27 +159,28 @@ func InsertIssueComments(comments []*Comment) error {
reaction.CommentID = comment.ID reaction.CommentID = comment.ID
} }
if len(comment.Reactions) > 0 { if len(comment.Reactions) > 0 {
if _, err := sess.Insert(comment.Reactions); err != nil { if err := db.Insert(ctx, comment.Reactions); err != nil {
return err return err
} }
} }
} }
for issueID := range issueIDs { for issueID := range issueIDs {
if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil { if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }
// InsertPullRequests inserted pull requests // InsertPullRequests inserted pull requests
func InsertPullRequests(prs ...*PullRequest) error { func InsertPullRequests(prs ...*PullRequest) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
for _, pr := range prs { for _, pr := range prs {
if err := insertIssue(sess, pr.Issue); err != nil { if err := insertIssue(sess, pr.Issue); err != nil {
return err return err
@ -190,16 +191,17 @@ func InsertPullRequests(prs ...*PullRequest) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
// InsertReleases migrates release // InsertReleases migrates release
func InsertReleases(rels ...*Release) error { func InsertReleases(rels ...*Release) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
for _, rel := range rels { for _, rel := range rels {
if _, err := sess.NoAutoTime().Insert(rel); err != nil { if _, err := sess.NoAutoTime().Insert(rel); err != nil {
@ -217,7 +219,7 @@ func InsertReleases(rels ...*Release) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
func migratedIssueCond(tp structs.GitServiceType) builder.Cond { func migratedIssueCond(tp structs.GitServiceType) builder.Cond {

View file

@ -140,15 +140,16 @@ func CountNotifications(opts *FindNotificationOptions) (int64, error) {
// CreateRepoTransferNotification creates notification for the user a repository was transferred to // CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) error { func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
var notify []*Notification var notify []*Notification
if newOwner.IsOrganization() { if newOwner.IsOrganization() {
users, err := getUsersWhoCanCreateOrgRepo(sess, newOwner.ID) users, err := getUsersWhoCanCreateOrgRepo(db.GetEngine(ctx), newOwner.ID)
if err != nil || len(users) == 0 { if err != nil || len(users) == 0 {
return err return err
} }
@ -171,28 +172,28 @@ func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) erro
}} }}
} }
if _, err := sess.InsertMulti(notify); err != nil { if err := db.Insert(ctx, notify); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// CreateOrUpdateIssueNotifications creates an issue notification // CreateOrUpdateIssueNotifications creates an issue notification
// for each watcher, or updates it if already exists // for each watcher, or updates it if already exists
// receiverID > 0 just send to reciver, else send to all watcher // receiverID > 0 just send to reciver, else send to all watcher
func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error { func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := createOrUpdateIssueNotifications(db.GetEngine(ctx), issueID, commentID, notificationAuthorID, receiverID); err != nil {
return err return err
} }
if err := createOrUpdateIssueNotifications(sess, issueID, commentID, notificationAuthorID, receiverID); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificationAuthorID, receiverID int64) error { func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificationAuthorID, receiverID int64) error {

View file

@ -18,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// Organization represents an organization // Organization represents an organization
@ -441,13 +440,13 @@ func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*User, error) {
And("team_user.org_id = ?", orgID).Asc("`user`.name").Find(&users) And("team_user.org_id = ?", orgID).Asc("`user`.name").Find(&users)
} }
func getOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*Organization, error) { func getOrgsByUserID(e db.Engine, userID int64, showAll bool) ([]*Organization, error) {
orgs := make([]*Organization, 0, 10) orgs := make([]*Organization, 0, 10)
sess := e.Where("`org_user`.uid=?", userID)
if !showAll { if !showAll {
sess.And("`org_user`.is_public=?", true) sess = sess.And("`org_user`.is_public=?", true)
} }
return orgs, sess. return orgs, sess.
And("`org_user`.uid=?", userID).
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id"). Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").
Asc("`user`.name"). Asc("`user`.name").
Find(&orgs) Find(&orgs)
@ -456,9 +455,7 @@ func getOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*Organiz
// 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) ([]*Organization, error) { func GetOrgsByUserID(userID int64, showAll bool) ([]*Organization, error) {
sess := db.NewSession(db.DefaultContext) return getOrgsByUserID(db.GetEngine(db.DefaultContext), userID, showAll)
defer sess.Close()
return getOrgsByUserID(sess, userID, showAll)
} }
// MinimalOrg represents a simple orgnization with only needed columns // MinimalOrg represents a simple orgnization with only needed columns
@ -466,9 +463,6 @@ type MinimalOrg = Organization
// GetUserOrgsList returns one user's all orgs list // GetUserOrgsList returns one user's all orgs list
func GetUserOrgsList(user *User) ([]*MinimalOrg, error) { func GetUserOrgsList(user *User) ([]*MinimalOrg, error) {
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
schema, err := db.TableInfo(new(User)) schema, err := db.TableInfo(new(User))
if err != nil { if err != nil {
return nil, err return nil, err
@ -491,7 +485,8 @@ func GetUserOrgsList(user *User) ([]*MinimalOrg, error) {
groupByStr := groupByCols.String() groupByStr := groupByCols.String()
groupByStr = groupByStr[0 : len(groupByStr)-1] groupByStr = groupByStr[0 : len(groupByStr)-1]
sess.Select(groupByStr+", count(distinct repo_id) as org_count"). sess := db.GetEngine(db.DefaultContext)
sess = sess.Select(groupByStr+", count(distinct repo_id) as org_count").
Table("user"). Table("user").
Join("INNER", "team", "`team`.org_id = `user`.id"). Join("INNER", "team", "`team`.org_id = `user`.id").
Join("INNER", "team_user", "`team`.id = `team_user`.team_id"). Join("INNER", "team_user", "`team`.id = `team_user`.team_id").
@ -524,7 +519,7 @@ func GetUserOrgsList(user *User) ([]*MinimalOrg, error) {
return orgs, nil return orgs, nil
} }
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { func getOwnedOrgsByUserID(sess db.Engine, userID int64) ([]*User, error) {
orgs := make([]*User, 0, 10) orgs := make([]*User, 0, 10)
return orgs, sess. return orgs, sess.
Join("INNER", "`team_user`", "`team_user`.org_id=`user`.id"). Join("INNER", "`team_user`", "`team_user`.org_id=`user`.id").
@ -572,9 +567,7 @@ func HasOrgsVisible(orgs []*Organization, user *User) bool {
// 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 := db.NewSession(db.DefaultContext) return getOwnedOrgsByUserID(db.GetEngine(db.DefaultContext), userID)
defer sess.Close()
return getOwnedOrgsByUserID(sess, userID)
} }
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by // GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by
@ -664,11 +657,11 @@ func AddOrgUser(orgID, uid int64) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
ou := &OrgUser{ ou := &OrgUser{
UID: uid, UID: uid,
@ -676,19 +669,13 @@ func AddOrgUser(orgID, uid int64) error {
IsPublic: setting.Service.DefaultOrgMemberVisible, IsPublic: setting.Service.DefaultOrgMemberVisible,
} }
if _, err := sess.Insert(ou); err != nil { if err := db.Insert(ctx, ou); err != nil {
if err := sess.Rollback(); err != nil {
log.Error("AddOrgUser: sess.Rollback: %v", err)
}
return err return err
} else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgID); err != nil { } else if _, err = db.Exec(ctx, "UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgID); err != nil {
if err := sess.Rollback(); err != nil {
log.Error("AddOrgUser: sess.Rollback: %v", err)
}
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// GetOrgByIDCtx returns the user object by given ID if exists. // GetOrgByIDCtx returns the user object by given ID if exists.

View file

@ -83,8 +83,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
cond = cond.And(builder.Eq{"org_id": opts.OrgID}) cond = cond.And(builder.Eq{"org_id": opts.OrgID})
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
defer sess.Close()
count, err := sess. count, err := sess.
Where(cond). Where(cond).
@ -265,17 +264,17 @@ func (t *Team) addAllRepositories(e db.Engine) error {
// AddAllRepositories adds all repositories to the team // AddAllRepositories adds all repositories to the team
func (t *Team) AddAllRepositories() (err error) { func (t *Team) AddAllRepositories() (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = t.addAllRepositories(db.GetEngine(ctx)); err != nil {
return err return err
} }
if err = t.addAllRepositories(sess); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// AddRepository adds new repository to team of organization. // AddRepository adds new repository to team of organization.
@ -286,17 +285,17 @@ func (t *Team) AddRepository(repo *Repository) (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = t.addRepository(db.GetEngine(ctx), repo); err != nil {
return err return err
} }
if err = t.addRepository(sess, repo); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// RemoveAllRepositories removes all repositories from team and recalculates access // RemoveAllRepositories removes all repositories from team and recalculates access
@ -305,17 +304,17 @@ func (t *Team) RemoveAllRepositories() (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = t.removeAllRepositories(db.GetEngine(ctx)); err != nil {
return err return err
} }
if err = t.removeAllRepositories(sess); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// removeAllRepositories removes all repositories from team and recalculates access // removeAllRepositories removes all repositories from team and recalculates access
@ -422,17 +421,17 @@ func (t *Team) RemoveRepository(repoID int64) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = t.removeRepository(db.GetEngine(ctx), repo, true); err != nil {
return err return err
} }
if err = t.removeRepository(sess, repo, true); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// UnitEnabled returns if the team has the given unit type enabled // UnitEnabled returns if the team has the given unit type enabled
@ -494,17 +493,13 @@ func NewTeam(t *Team) (err error) {
return ErrTeamAlreadyExist{t.OrgID, t.LowerName} return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
if _, err = sess.Insert(t); err != nil { if err = db.Insert(ctx, t); err != nil {
errRollback := sess.Rollback()
if errRollback != nil {
log.Error("NewTeam sess.Rollback: %v", errRollback)
}
return err return err
} }
@ -513,32 +508,24 @@ func NewTeam(t *Team) (err error) {
for _, unit := range t.Units { for _, unit := range t.Units {
unit.TeamID = t.ID unit.TeamID = t.ID
} }
if _, err = sess.Insert(&t.Units); err != nil { if err = db.Insert(ctx, &t.Units); err != nil {
errRollback := sess.Rollback()
if errRollback != nil {
log.Error("NewTeam sess.Rollback: %v", errRollback)
}
return err return err
} }
} }
// Add all repositories to the team if it has access to all of them. // Add all repositories to the team if it has access to all of them.
if t.IncludesAllRepositories { if t.IncludesAllRepositories {
err = t.addAllRepositories(sess) err = t.addAllRepositories(db.GetEngine(ctx))
if err != nil { if err != nil {
return fmt.Errorf("addAllRepositories: %v", err) return fmt.Errorf("addAllRepositories: %v", err)
} }
} }
// Update organization number of teams. // Update organization number of teams.
if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil { if _, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
errRollback := sess.Rollback()
if errRollback != nil {
log.Error("NewTeam sess.Rollback: %v", errRollback)
}
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func getTeam(e db.Engine, orgID int64, name string) (*Team, error) { func getTeam(e db.Engine, orgID int64, name string) (*Team, error) {
@ -624,11 +611,12 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
t.Description = t.Description[:255] t.Description = t.Description[:255]
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
t.LowerName = strings.ToLower(t.Name) t.LowerName = strings.ToLower(t.Name)
has, err := sess. has, err := sess.
@ -659,10 +647,6 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
return err return err
} }
if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil { if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil {
errRollback := sess.Rollback()
if errRollback != nil {
log.Error("UpdateTeam sess.Rollback: %v", errRollback)
}
return err return err
} }
} }
@ -688,7 +672,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
} }
} }
return sess.Commit() return committer.Commit()
} }
// DeleteTeam deletes given team. // DeleteTeam deletes given team.
@ -698,11 +682,12 @@ func DeleteTeam(t *Team) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if err := t.getMembers(sess); err != nil { if err := t.getMembers(sess); err != nil {
return err return err
@ -736,7 +721,7 @@ func DeleteTeam(t *Team) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// ___________ ____ ___ // ___________ ____ ___
@ -839,13 +824,15 @@ func AddTeamMember(team *Team, userID int64) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
if _, err := sess.Insert(&TeamUser{ sess := db.GetEngine(ctx)
if err := db.Insert(ctx, &TeamUser{
UID: userID, UID: userID,
OrgID: team.OrgID, OrgID: team.OrgID,
TeamID: team.ID, TeamID: team.ID,
@ -869,7 +856,7 @@ func AddTeamMember(team *Team, userID int64) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
func removeTeamMember(ctx context.Context, team *Team, userID int64) error { func removeTeamMember(ctx context.Context, team *Team, userID int64) error {
@ -1048,25 +1035,21 @@ func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error)
// UpdateTeamUnits updates a teams's units // UpdateTeamUnits updates a teams's units
func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) { func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
if _, err = sess.Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil { if _, err = db.GetEngine(ctx).Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
return err return err
} }
if len(units) > 0 { if len(units) > 0 {
if _, err = sess.Insert(units); err != nil { if err = db.Insert(ctx, units); err != nil {
errRollback := sess.Rollback()
if errRollback != nil {
log.Error("UpdateTeamUnits sess.Rollback: %v", errRollback)
}
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -143,26 +143,25 @@ func NewProject(p *Project) error {
return errors.New("project type is not valid") return errors.New("project type is not valid")
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
return err
}
defer committer.Close()
if err := sess.Begin(); err != nil { if err := db.Insert(ctx, p); err != nil {
return err return err
} }
if _, err := sess.Insert(p); err != nil { if _, err := db.Exec(ctx, "UPDATE `repository` SET num_projects = num_projects + 1 WHERE id = ?", p.RepoID); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `repository` SET num_projects = num_projects + 1 WHERE id = ?", p.RepoID); err != nil { if err := createBoardsForProjectsType(db.GetEngine(ctx), p); err != nil {
return err return err
} }
if err := createBoardsForProjectsType(sess, p); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// GetProjectByID returns the projects in a repository // GetProjectByID returns the projects in a repository
@ -220,11 +219,12 @@ func updateRepositoryProjectCount(e db.Engine, repoID int64) error {
// ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed // ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed
func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) error { func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
p := new(Project) p := new(Project)
@ -239,22 +239,22 @@ func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) er
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// ChangeProjectStatus toggle a project between opened and closed // ChangeProjectStatus toggle a project between opened and closed
func ChangeProjectStatus(p *Project, isClosed bool) error { func ChangeProjectStatus(p *Project, isClosed bool) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := changeProjectStatus(db.GetEngine(ctx), p, isClosed); err != nil {
return err return err
} }
if err := changeProjectStatus(sess, p, isClosed); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func changeProjectStatus(e db.Engine, p *Project, isClosed bool) error { func changeProjectStatus(e db.Engine, p *Project, isClosed bool) error {
@ -273,17 +273,17 @@ func changeProjectStatus(e db.Engine, p *Project, isClosed bool) error {
// DeleteProjectByID deletes a project from a repository. // DeleteProjectByID deletes a project from a repository.
func DeleteProjectByID(id int64) error { func DeleteProjectByID(id int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := deleteProjectByID(db.GetEngine(ctx), id); err != nil {
return err return err
} }
if err := deleteProjectByID(sess, id); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func deleteProjectByID(e db.Engine, id int64) error { func deleteProjectByID(e db.Engine, id int64) error {

View file

@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
type ( type (
@ -69,7 +68,7 @@ func IsProjectBoardTypeValid(p ProjectBoardType) bool {
} }
} }
func createBoardsForProjectsType(sess *xorm.Session, project *Project) error { func createBoardsForProjectsType(sess db.Engine, project *Project) error {
var items []string var items []string
switch project.BoardType { switch project.BoardType {
@ -117,17 +116,17 @@ func NewProjectBoard(board *ProjectBoard) error {
// DeleteProjectBoardByID removes all issues references to the project board. // DeleteProjectBoardByID removes all issues references to the project board.
func DeleteProjectBoardByID(boardID int64) error { func DeleteProjectBoardByID(boardID int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := deleteProjectBoardByID(db.GetEngine(ctx), boardID); err != nil {
return err return err
} }
if err := deleteProjectBoardByID(sess, boardID); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func deleteProjectBoardByID(e db.Engine, boardID int64) error { func deleteProjectBoardByID(e db.Engine, boardID int64) error {

View file

@ -185,11 +185,12 @@ func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *User, newPro
// MoveIssueAcrossProjectBoards move a card from one board to another // MoveIssueAcrossProjectBoards move a card from one board to another
func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error { func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
var pis ProjectIssue var pis ProjectIssue
has, err := sess.Where("issue_id=?", issue.ID).Get(&pis) has, err := sess.Where("issue_id=?", issue.ID).Get(&pis)
@ -206,7 +207,7 @@ func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func (pb *ProjectBoard) removeIssues(e db.Engine) error { func (pb *ProjectBoard) removeIssues(e db.Engine) error {

View file

@ -285,11 +285,12 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// Note: This doesn't page as we only expect a very limited number of reviews // Note: This doesn't page as we only expect a very limited number of reviews
reviews, err := findReviews(sess, FindReviewOptions{ reviews, err := findReviews(sess, FindReviewOptions{
@ -326,7 +327,7 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
} }
reviewersWritten++ reviewersWritten++
} }
return sess.Commit() return committer.Commit()
} }
// GetDefaultSquashMessage returns default message used when squash and merging pull request // GetDefaultSquashMessage returns default message used when squash and merging pull request

View file

@ -1276,17 +1276,17 @@ func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err
} }
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
return fmt.Errorf("sess.Begin: %v", 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 err
} }
return sess.Commit() return committer.Commit()
} }
func getRepositoriesByForkID(e db.Engine, forkID int64) ([]*Repository, error) { 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 // UpdateRepository updates a repository
func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) { func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err 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 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) // 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 { if ownerID == 0 {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err 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, OwnerName: ownerName,
}); err != nil { }); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// UpdateRepositoryUpdatedTime updates a repository's updated time // 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 // UpdateRepositoryUnits updates a repository's units
func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) { func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
// Delete existing settings of units before adding again // Delete existing settings of units before adding again
for _, u := range units { for _, u := range units {
deleteUnitTypes = append(deleteUnitTypes, u.Type) 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 return err
} }
if len(units) > 0 { if len(units) > 0 {
if _, err = sess.Insert(units); err != nil { if err = db.Insert(ctx, units); err != nil {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }
// DeleteRepository deletes a repository for a user or organization. // 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) // 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 { func DeleteRepository(doer *User, uid, repoID int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// In case is a organization. // In case is a organization.
org, err := getUserByID(sess, uid) org, err := getUserByID(sess, uid)
@ -1639,11 +1640,11 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return err return err
} }
if err = sess.Commit(); err != nil { if err = committer.Commit(); err != nil {
return err return err
} }
sess.Close() committer.Close()
// We should always delete the files after the database transaction succeed. If // 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. // 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)) cond = cond.And(builder.In("lower_name", opts.LowerNames))
} }
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
defer sess.Close()
count, err := sess.Where(cond).Count(new(Repository)) count, err := sess.Where(cond).Count(new(Repository))
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err) 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) repos := make([]*Repository, 0, opts.PageSize)
return repos, count, db.SetSessionPagination(sess, opts).Find(&repos) return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
} }
@ -2136,32 +2136,36 @@ func (repo *Repository) GetTrustModel() TrustModelType {
return trustModel 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 // DoctorUserStarNum recalculate Stars number for all user
func DoctorUserStarNum() (err error) { func DoctorUserStarNum() (err error) {
const batchSize = 100 const batchSize = 100
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
for start := 0; ; start += batchSize { for start := 0; ; start += batchSize {
users := make([]User, 0, 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 return
} }
if len(users) == 0 { if len(users) == 0 {
break break
} }
if err = sess.Begin(); err != nil { if err = updateUserStarNumbers(users); 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 {
return return
} }
} }

View file

@ -129,18 +129,18 @@ func (repo *Repository) UploadAvatar(data []byte) error {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
oldAvatarPath := repo.CustomAvatarRelativePath() oldAvatarPath := repo.CustomAvatarRelativePath()
// Users can upload the same image to other repo - prefix it with ID // Users can upload the same image to other repo - prefix it with ID
// Then repo will be removed - only it avatar file will be removed // Then repo will be removed - only it avatar file will be removed
repo.Avatar = newAvatar repo.Avatar = newAvatar
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil { if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err) return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err)
} }
@ -159,7 +159,7 @@ func (repo *Repository) UploadAvatar(data []byte) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
// DeleteAvatar deletes the repos's custom avatar. // DeleteAvatar deletes the repos's custom avatar.
@ -172,14 +172,14 @@ func (repo *Repository) DeleteAvatar() error {
avatarPath := repo.CustomAvatarRelativePath() avatarPath := repo.CustomAvatarRelativePath()
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath) log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
repo.Avatar = "" repo.Avatar = ""
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil { if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err) return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err)
} }
@ -187,5 +187,5 @@ func (repo *Repository) DeleteAvatar() error {
return fmt.Errorf("DeleteAvatar: Failed to remove %s: %v", avatarPath, err) return fmt.Errorf("DeleteAvatar: Failed to remove %s: %v", avatarPath, err)
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -53,17 +53,17 @@ func (repo *Repository) addCollaborator(e db.Engine, u *User) error {
// AddCollaborator adds new collaboration to a repository with default access mode. // AddCollaborator adds new collaboration to a repository with default access mode.
func (repo *Repository) AddCollaborator(u *User) error { func (repo *Repository) AddCollaborator(u *User) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := repo.addCollaborator(db.GetEngine(ctx), u); err != nil {
return err return err
} }
if err := repo.addCollaborator(sess, u); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
func (repo *Repository) getCollaborations(e db.Engine, listOptions db.ListOptions) ([]*Collaboration, error) { func (repo *Repository) getCollaborations(e db.Engine, listOptions db.ListOptions) ([]*Collaboration, error) {
@ -176,17 +176,17 @@ func (repo *Repository) changeCollaborationAccessMode(e db.Engine, uid int64, mo
// ChangeCollaborationAccessMode sets new access mode for the collaboration. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error { func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
}
defer committer.Close()
if err := repo.changeCollaborationAccessMode(db.GetEngine(ctx), uid, mode); err != nil {
return err return err
} }
if err := repo.changeCollaborationAccessMode(sess, uid, mode); err != nil { return committer.Commit()
return err
}
return sess.Commit()
} }
// DeleteCollaboration removes collaboration relation between the user and repository. // DeleteCollaboration removes collaboration relation between the user and repository.
@ -196,11 +196,13 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
UserID: uid, UserID: uid,
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if has, err := sess.Delete(collaboration); err != nil || has == 0 { if has, err := sess.Delete(collaboration); err != nil || has == 0 {
return err return err
@ -221,7 +223,7 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func (repo *Repository) reconsiderIssueAssignees(e db.Engine, uid int64) error { func (repo *Repository) reconsiderIssueAssignees(e db.Engine, uid int64) error {

View file

@ -112,11 +112,12 @@ func (repo *Repository) GetTopLanguageStats(limit int) (LanguageStatList, error)
// UpdateLanguageStats updates the language statistics for repository // UpdateLanguageStats updates the language statistics for repository
func (repo *Repository) UpdateLanguageStats(commitID string, stats map[string]int64) error { func (repo *Repository) UpdateLanguageStats(commitID string, stats map[string]int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
if err := sess.Begin(); err != nil { if err != nil {
return err return err
} }
defer sess.Close() defer committer.Close()
sess := db.GetEngine(ctx)
oldstats, err := repo.getLanguageStats(sess) oldstats, err := repo.getLanguageStats(sess)
if err != nil { if err != nil {
@ -178,16 +179,18 @@ func (repo *Repository) UpdateLanguageStats(commitID string, stats map[string]in
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo) // CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo)
func CopyLanguageStat(originalRepo, destRepo *Repository) error { func CopyLanguageStat(originalRepo, destRepo *Repository) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
RepoLang := make(LanguageStatList, 0, 6) RepoLang := make(LanguageStatList, 0, 6)
if err := sess.Where("`repo_id` = ?", originalRepo.ID).Desc("`size`").Find(&RepoLang); err != nil { if err := sess.Where("`repo_id` = ?", originalRepo.ID).Desc("`size`").Find(&RepoLang); err != nil {
return err return err
@ -207,5 +210,5 @@ func CopyLanguageStat(originalRepo, destRepo *Repository) error {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// RepositoryListDefaultPageSize is the default number of repositories // RepositoryListDefaultPageSize is the default number of repositories
@ -373,7 +372,6 @@ func SearchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond, loa
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
defer sess.Close()
defaultSize := 50 defaultSize := 50
if opts.PageSize > 0 { if opts.PageSize > 0 {
@ -397,7 +395,7 @@ func SearchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond, loa
return repos, count, nil return repos, count, nil
} }
func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (*xorm.Session, int64, error) { func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (db.Engine, int64, error) {
if opts.Page <= 0 { if opts.Page <= 0 {
opts.Page = 1 opts.Page = 1
} }
@ -410,7 +408,7 @@ func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (*x
opts.OrderBy = SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy)) opts.OrderBy = SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy))
} }
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
var count int64 var count int64
if opts.PageSize > 0 { if opts.PageSize > 0 {
@ -419,14 +417,13 @@ func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (*x
Where(cond). Where(cond).
Count(new(Repository)) Count(new(Repository))
if err != nil { if err != nil {
_ = sess.Close()
return nil, 0, fmt.Errorf("Count: %v", err) return nil, 0, fmt.Errorf("Count: %v", err)
} }
} }
sess.Where(cond).OrderBy(opts.OrderBy.String()) sess = sess.Where(cond).OrderBy(opts.OrderBy.String())
if opts.PageSize > 0 { if opts.PageSize > 0 {
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
} }
return sess, count, nil return sess, count, nil
} }
@ -495,7 +492,6 @@ func SearchRepositoryIDs(opts *SearchRepoOptions) ([]int64, int64, error) {
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
defer sess.Close()
defaultSize := 50 defaultSize := 50
if opts.PageSize > 0 { if opts.PageSize > 0 {

View file

@ -279,8 +279,7 @@ func IsUserRealRepoAdmin(repo *Repository, user *User) (bool, error) {
return true, nil return true, nil
} }
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
defer sess.Close()
if err := repo.getOwner(sess); err != nil { if err := repo.getOwner(sess); err != nil {
return false, err return false, err

View file

@ -118,11 +118,12 @@ func deleteRepositoryTransfer(e db.Engine, repoID int64) error {
// CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry, // CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry,
// thus cancel the transfer process. // thus cancel the transfer process.
func CancelRepositoryTransfer(repo *Repository) error { func CancelRepositoryTransfer(repo *Repository) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
repo.Status = RepositoryReady repo.Status = RepositoryReady
if err := updateRepositoryCols(sess, repo, "status"); err != nil { if err := updateRepositoryCols(sess, repo, "status"); err != nil {
@ -133,7 +134,7 @@ func CancelRepositoryTransfer(repo *Repository) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// TestRepositoryReadyForTransfer make sure repo is ready to transfer // TestRepositoryReadyForTransfer make sure repo is ready to transfer
@ -150,11 +151,12 @@ func TestRepositoryReadyForTransfer(status RepositoryStatus) error {
// CreatePendingRepositoryTransfer transfer a repo from one owner to a new one. // CreatePendingRepositoryTransfer transfer a repo from one owner to a new one.
// it marks the repository transfer as "pending" // it marks the repository transfer as "pending"
func CreatePendingRepositoryTransfer(doer, newOwner *User, repoID int64, teams []*Team) error { func CreatePendingRepositoryTransfer(doer, newOwner *User, repoID int64, teams []*Team) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
repo, err := getRepositoryByID(sess, repoID) repo, err := getRepositoryByID(sess, repoID)
if err != nil { if err != nil {
@ -191,11 +193,11 @@ func CreatePendingRepositoryTransfer(doer, newOwner *User, repoID int64, teams [
transfer.TeamIDs = append(transfer.TeamIDs, teams[k].ID) transfer.TeamIDs = append(transfer.TeamIDs, teams[k].ID)
} }
if _, err := sess.Insert(transfer); err != nil { if err := db.Insert(ctx, transfer); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// TransferOwnership transfers all corresponding repository items from old user to new one. // TransferOwnership transfers all corresponding repository items from old user to new one.
@ -232,11 +234,13 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
} }
}() }()
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil { return err
return fmt.Errorf("sess.Begin: %v", err)
} }
defer committer.Close()
sess := db.GetEngine(ctx)
newOwner, err := getUserByName(sess, newOwnerName) newOwner, err := getUserByName(sess, newOwnerName)
if err != nil { if err != nil {
@ -408,5 +412,5 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
return fmt.Errorf("newRepoRedirect: %v", err) return fmt.Errorf("newRepoRedirect: %v", err)
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -290,17 +290,17 @@ func NotifyWatchers(actions ...*Action) error {
// NotifyWatchersActions creates batch of actions for every watcher. // NotifyWatchersActions creates batch of actions for every watcher.
func NotifyWatchersActions(acts []*Action) error { func NotifyWatchersActions(acts []*Action) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
for _, act := range acts { for _, act := range acts {
if err := notifyWatchers(sess, act); err != nil { if err := notifyWatchers(db.GetEngine(ctx), act); err != nil {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }
func watchIfAuto(e db.Engine, userID, repoID int64, isWrite bool) error { func watchIfAuto(e db.Engine, userID, repoID int64, isWrite bool) error {

View file

@ -472,11 +472,7 @@ func SubmitReview(doer *User, issue *Issue, reviewType ReviewType, content, comm
func GetReviewersByIssueID(issueID int64) ([]*Review, error) { func GetReviewersByIssueID(issueID int64) ([]*Review, error) {
reviews := make([]*Review, 0, 10) reviews := make([]*Review, 0, 10)
sess := db.NewSession(db.DefaultContext) sess := db.GetEngine(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return nil, err
}
// Get latest review of each reviewer, sorted in order they were made // Get latest review of each reviewer, sorted in order they were made
if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND dismissed = ? AND original_author_id = 0 GROUP BY issue_id, reviewer_id) ORDER BY review.updated_unix ASC", if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND dismissed = ? AND original_author_id = 0 GROUP BY issue_id, reviewer_id) ORDER BY review.updated_unix ASC",
@ -590,12 +586,12 @@ func DismissReview(review *Review, isDismiss bool) (err error) {
// InsertReviews inserts review and review comments // InsertReviews inserts review and review comments
func InsertReviews(reviews []*Review) error { func InsertReviews(reviews []*Review) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
for _, review := range reviews { for _, review := range reviews {
if _, err := sess.NoAutoTime().Insert(review); err != nil { if _, err := sess.NoAutoTime().Insert(review); err != nil {
@ -627,7 +623,7 @@ func InsertReviews(reviews []*Review) error {
} }
} }
return sess.Commit() return committer.Commit()
} }
// AddReviewRequest add a review request from one reviewer // AddReviewRequest add a review request from one reviewer
@ -920,12 +916,12 @@ func CanMarkConversation(issue *Issue, doer *User) (permResult bool, err error)
// DeleteReview delete a review and it's code comments // DeleteReview delete a review and it's code comments
func DeleteReview(r *Review) error { func DeleteReview(r *Review) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if r.ID == 0 { if r.ID == 0 {
return fmt.Errorf("review is not allowed to be 0") return fmt.Errorf("review is not allowed to be 0")
@ -959,7 +955,7 @@ func DeleteReview(r *Review) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// GetCodeCommentsCount return count of CodeComments a Review has // GetCodeCommentsCount return count of CodeComments a Review has

View file

@ -96,11 +96,12 @@ func AddPublicKey(ownerID int64, name, content string, loginSourceID int64) (*Pu
return nil, err return nil, err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if err := checkKeyFingerprint(sess, fingerprint); err != nil { if err := checkKeyFingerprint(sess, fingerprint); err != nil {
return nil, err return nil, err
@ -129,7 +130,7 @@ func AddPublicKey(ownerID int64, name, content string, loginSourceID int64) (*Pu
return nil, fmt.Errorf("addKey: %v", err) return nil, fmt.Errorf("addKey: %v", err)
} }
return key, sess.Commit() return key, committer.Commit()
} }
// GetPublicKeyByID returns public key by given ID. // GetPublicKeyByID returns public key by given ID.
@ -334,20 +335,20 @@ func DeletePublicKey(doer *User, id int64) (err error) {
return ErrKeyAccessDenied{doer.ID, key.ID, "public"} return ErrKeyAccessDenied{doer.ID, key.ID, "public"}
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = deletePublicKeys(db.GetEngine(ctx), id); err != nil {
return err return err
} }
if err = deletePublicKeys(sess, id); err != nil { if err = committer.Commit(); err != nil {
return err return err
} }
committer.Close()
if err = sess.Commit(); err != nil {
return err
}
sess.Close()
if key.Type == KeyTypePrincipal { if key.Type == KeyTypePrincipal {
return RewriteAllPrincipalKeys() return RewriteAllPrincipalKeys()
@ -359,11 +360,12 @@ func DeletePublicKey(doer *User, id int64) (err error) {
// deleteKeysMarkedForDeletion returns true if ssh keys needs update // deleteKeysMarkedForDeletion returns true if ssh keys needs update
func deleteKeysMarkedForDeletion(keys []string) (bool, error) { func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
// Start session // Start session
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return false, err return false, err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// Delete keys marked for deletion // Delete keys marked for deletion
var sshKeysNeedUpdate bool var sshKeysNeedUpdate bool
@ -380,7 +382,7 @@ func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
sshKeysNeedUpdate = true sshKeysNeedUpdate = true
} }
if err := sess.Commit(); err != nil { if err := committer.Commit(); err != nil {
return false, err return false, err
} }

View file

@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// ________ .__ ____ __. // ________ .__ ____ __.
@ -90,7 +89,7 @@ func checkDeployKey(e db.Engine, keyID, repoID int64, name string) error {
} }
// addDeployKey adds new key-repo relation. // addDeployKey adds new key-repo relation.
func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string, mode AccessMode) (*DeployKey, error) { func addDeployKey(e db.Engine, keyID, repoID int64, name, fingerprint string, mode AccessMode) (*DeployKey, error) {
if err := checkDeployKey(e, keyID, repoID, name); err != nil { if err := checkDeployKey(e, keyID, repoID, name); err != nil {
return nil, err return nil, err
} }
@ -126,11 +125,13 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
accessMode = AccessModeWrite accessMode = AccessModeWrite
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
pkey := &PublicKey{ pkey := &PublicKey{
Fingerprint: fingerprint, Fingerprint: fingerprint,
@ -160,7 +161,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
return nil, err return nil, err
} }
return key, sess.Commit() return key, committer.Commit()
} }
// GetDeployKeyByID returns deploy key by given ID. // GetDeployKeyByID returns deploy key by given ID.
@ -212,15 +213,16 @@ func UpdateDeployKey(key *DeployKey) error {
// DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed. // DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
func DeleteDeployKey(doer *User, id int64) error { func DeleteDeployKey(doer *User, id int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
if err := deleteDeployKey(sess, doer, id); err != nil { defer committer.Close()
if err := deleteDeployKey(db.GetEngine(ctx), doer, id); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func deleteDeployKey(sess db.Engine, doer *User, id int64) error { func deleteDeployKey(sess db.Engine, doer *User, id int64) error {

View file

@ -25,11 +25,12 @@ import (
// AddPrincipalKey adds new principal to database and authorized_principals file. // AddPrincipalKey adds new principal to database and authorized_principals file.
func AddPrincipalKey(ownerID int64, content string, loginSourceID int64) (*PublicKey, error) { func AddPrincipalKey(ownerID int64, content string, loginSourceID int64) (*PublicKey, error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// Principals cannot be duplicated. // Principals cannot be duplicated.
has, err := sess. has, err := sess.
@ -53,11 +54,11 @@ func AddPrincipalKey(ownerID int64, content string, loginSourceID int64) (*Publi
return nil, fmt.Errorf("addKey: %v", err) return nil, fmt.Errorf("addKey: %v", err)
} }
if err = sess.Commit(); err != nil { if err = committer.Commit(); err != nil {
return nil, err return nil, err
} }
sess.Close() committer.Close()
return key, RewriteAllPrincipalKeys() return key, RewriteAllPrincipalKeys()
} }

View file

@ -23,44 +23,44 @@ func init() {
// 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 := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
staring := isStaring(db.GetEngine(ctx), userID, repoID)
if star { if star {
if isStaring(sess, userID, repoID) { if staring {
return nil return nil
} }
if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil { if err := db.Insert(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil { if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
return err return err
} }
} else { } else {
if !isStaring(sess, userID, repoID) { if !staring {
return nil return nil
} }
if _, err := sess.Delete(&Star{UID: userID, RepoID: repoID}); err != nil { if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil { if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
return err return err
} }
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
return err return err
} }
} }
return sess.Commit() return committer.Commit()
} }
// IsStaring checks if user has starred given repository. // IsStaring checks if user has starred given repository.

View file

@ -253,14 +253,6 @@ func FinishMigrateTask(task *Task) error {
} }
task.PayloadContent = string(confBytes) task.PayloadContent = string(confBytes)
sess := db.NewSession(db.DefaultContext) _, err = db.GetEngine(db.DefaultContext).ID(task.ID).Cols("status", "end_time", "payload_content").Update(task)
defer sess.Close()
if err := sess.Begin(); err != nil {
return err return err
} }
if _, err := sess.ID(task.ID).Cols("status", "end_time", "payload_content").Update(task); err != nil {
return err
}
return sess.Commit()
}

View file

@ -225,11 +225,12 @@ func getRepoTopicByName(e db.Engine, repoID int64, topicName string) (*Topic, er
// AddTopic adds a topic name to a repository (if it does not already have it) // AddTopic adds a topic name to a repository (if it does not already have it)
func AddTopic(repoID int64, topicName string) (*Topic, error) { func AddTopic(repoID int64, topicName string) (*Topic, error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return nil, err return nil, err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
topic, err := getRepoTopicByName(sess, repoID, topicName) topic, err := getRepoTopicByName(sess, repoID, topicName)
if err != nil { if err != nil {
@ -258,7 +259,7 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
return nil, err return nil, err
} }
return topic, sess.Commit() return topic, committer.Commit()
} }
// DeleteTopic removes a topic name from a repository (if it has it) // DeleteTopic removes a topic name from a repository (if it has it)
@ -286,12 +287,12 @@ func SaveTopics(repoID int64, topicNames ...string) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
var addedTopicNames []string var addedTopicNames []string
for _, topicName := range topicNames { for _, topicName := range topicNames {
@ -352,5 +353,5 @@ func SaveTopics(repoID int64, topicNames ...string) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }

View file

@ -109,23 +109,23 @@ func DeleteUploads(uploads ...*Upload) (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
ids := make([]int64, len(uploads)) ids := make([]int64, len(uploads))
for i := 0; i < len(uploads); i++ { for i := 0; i < len(uploads); i++ {
ids[i] = uploads[i].ID ids[i] = uploads[i].ID
} }
if _, err = sess. if _, err = db.GetEngine(ctx).
In("id", ids). In("id", ids).
Delete(new(Upload)); err != nil { Delete(new(Upload)); err != nil {
return fmt.Errorf("delete uploads: %v", err) return fmt.Errorf("delete uploads: %v", err)
} }
if err = sess.Commit(); err != nil { if err = committer.Commit(); err != nil {
return err return err
} }

View file

@ -1105,11 +1105,13 @@ func updateUserCols(e db.Engine, u *User, cols ...string) error {
// UpdateUserSetting updates user's settings. // UpdateUserSetting updates user's settings.
func UpdateUserSetting(u *User) (err error) { func UpdateUserSetting(u *User) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
if !u.IsOrganization() { if !u.IsOrganization() {
if err = checkDupEmail(sess, u); err != nil { if err = checkDupEmail(sess, u); err != nil {
return err return err
@ -1118,7 +1120,7 @@ func UpdateUserSetting(u *User) (err error) {
if err = updateUser(sess, u); err != nil { if err = updateUser(sess, u); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// deleteBeans deletes all given beans, beans should contain delete conditions. // deleteBeans deletes all given beans, beans should contain delete conditions.
@ -1533,7 +1535,7 @@ type SearchUserOptions struct {
IsProhibitLogin util.OptionalBool IsProhibitLogin util.OptionalBool
} }
func (opts *SearchUserOptions) toSearchQueryBase() (sess *xorm.Session) { func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session {
var cond builder.Cond = builder.Eq{"type": opts.Type} var cond builder.Cond = builder.Eq{"type": opts.Type}
if len(opts.Keyword) > 0 { if len(opts.Keyword) > 0 {
lowerKeyword := strings.ToLower(opts.Keyword) lowerKeyword := strings.ToLower(opts.Keyword)
@ -1599,8 +1601,11 @@ func (opts *SearchUserOptions) toSearchQueryBase() (sess *xorm.Session) {
cond = cond.And(builder.Eq{"prohibit_login": opts.IsProhibitLogin.IsTrue()}) cond = cond.And(builder.Eq{"prohibit_login": opts.IsProhibitLogin.IsTrue()})
} }
sess = db.NewSession(db.DefaultContext) e := db.GetEngine(db.DefaultContext)
if !opts.IsTwoFactorEnabled.IsNone() { if opts.IsTwoFactorEnabled.IsNone() {
return e.Where(cond)
}
// 2fa filter uses LEFT JOIN to check whether a user has a 2fa record // 2fa filter uses LEFT JOIN to check whether a user has a 2fa record
// TODO: bad performance here, maybe there will be a column "is_2fa_enabled" in the future // TODO: bad performance here, maybe there will be a column "is_2fa_enabled" in the future
if opts.IsTwoFactorEnabled.IsTrue() { if opts.IsTwoFactorEnabled.IsTrue() {
@ -1608,10 +1613,9 @@ func (opts *SearchUserOptions) toSearchQueryBase() (sess *xorm.Session) {
} else { } else {
cond = cond.And(builder.Expr("two_factor.uid IS NULL")) cond = cond.And(builder.Expr("two_factor.uid IS NULL"))
} }
sess = sess.Join("LEFT OUTER", "two_factor", "two_factor.uid = `user`.id")
} return e.Join("LEFT OUTER", "two_factor", "two_factor.uid = `user`.id").
sess = sess.Where(cond) Where(cond)
return sess
} }
// SearchUsers takes options i.e. keyword and part of user name to search, // SearchUsers takes options i.e. keyword and part of user name to search,

View file

@ -33,24 +33,24 @@ func FollowUser(userID, followID int64) (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if err = db.Insert(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
return err return err
} }
if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil { if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
return err return err
} }
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil { if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
return err return err
} }
return committer.Commit()
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
return err
}
return sess.Commit()
} }
// UnfollowUser unmarks someone as another's follower. // UnfollowUser unmarks someone as another's follower.
@ -59,22 +59,22 @@ func UnfollowUser(userID, followID int64) (err error) {
return nil return nil
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil { return err
}
defer committer.Close()
if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
return err return err
} }
if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil { if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
return err return err
} }
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil { if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
return err return err
} }
return committer.Commit()
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
return err
}
return sess.Commit()
} }

View file

@ -109,11 +109,11 @@ func (u *User) UploadAvatar(data []byte) error {
return err return err
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
u.UseCustomAvatar = true u.UseCustomAvatar = true
// Different users can upload same image as avatar // Different users can upload same image as avatar
@ -121,7 +121,7 @@ func (u *User) UploadAvatar(data []byte) error {
// Otherwise, if any of the users delete his avatar // Otherwise, if any of the users delete his avatar
// Other users will lose their avatars too. // Other users will lose their avatars too.
u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data))))) u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data)))))
if err = updateUserCols(sess, u, "use_custom_avatar", "avatar"); err != nil { if err = updateUserCols(db.GetEngine(ctx), u, "use_custom_avatar", "avatar"); err != nil {
return fmt.Errorf("updateUser: %v", err) return fmt.Errorf("updateUser: %v", err)
} }
@ -134,7 +134,7 @@ func (u *User) UploadAvatar(data []byte) error {
return fmt.Errorf("Failed to create dir %s: %v", u.CustomAvatarRelativePath(), err) return fmt.Errorf("Failed to create dir %s: %v", u.CustomAvatarRelativePath(), err)
} }
return sess.Commit() return committer.Commit()
} }
// IsUploadAvatarChanged returns true if the current user's avatar would be changed with the provided data // IsUploadAvatarChanged returns true if the current user's avatar would be changed with the provided data

View file

@ -17,15 +17,15 @@ import (
// ActivateEmail activates the email address to given user. // ActivateEmail activates the email address to given user.
func ActivateEmail(email *user_model.EmailAddress) error { func ActivateEmail(email *user_model.EmailAddress) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
if err := updateActivation(sess, email, true); err != nil { defer committer.Close()
if err := updateActivation(db.GetEngine(ctx), email, true); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
func updateActivation(e db.Engine, email *user_model.EmailAddress, activate bool) error { func updateActivation(e db.Engine, email *user_model.EmailAddress, activate bool) error {
@ -64,11 +64,12 @@ func MakeEmailPrimary(email *user_model.EmailAddress) error {
return ErrUserNotExist{email.UID, "", 0} return ErrUserNotExist{email.UID, "", 0}
} }
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
sess := db.GetEngine(ctx)
// 1. Update user table // 1. Update user table
user.Email = email.Email user.Email = email.Email
@ -89,7 +90,7 @@ func MakeEmailPrimary(email *user_model.EmailAddress) error {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// SearchEmailOrderBy is used to sort the results from SearchEmails() // SearchEmailOrderBy is used to sort the results from SearchEmails()

View file

@ -269,9 +269,8 @@ func TestGetOrgRepositoryIDs(t *testing.T) {
func TestNewGitSig(t *testing.T) { func TestNewGitSig(t *testing.T) {
users := make([]*User, 0, 20) users := make([]*User, 0, 20)
sess := db.NewSession(db.DefaultContext) err := db.GetEngine(db.DefaultContext).Find(&users)
defer sess.Close() assert.NoError(t, err)
sess.Find(&users)
for _, user := range users { for _, user := range users {
sig := user.NewGitSig() sig := user.NewGitSig()
@ -284,9 +283,8 @@ func TestNewGitSig(t *testing.T) {
func TestDisplayName(t *testing.T) { func TestDisplayName(t *testing.T) {
users := make([]*User, 0, 20) users := make([]*User, 0, 20)
sess := db.NewSession(db.DefaultContext) err := db.GetEngine(db.DefaultContext).Find(&users)
defer sess.Close() assert.NoError(t, err)
sess.Find(&users)
for _, user := range users { for _, user := range users {
displayName := user.DisplayName() displayName := user.DisplayName()

View file

@ -507,21 +507,21 @@ func UpdateWebhookLastStatus(w *Webhook) error {
// deleteWebhook uses argument bean as query condition, // deleteWebhook uses argument bean as query condition,
// ID must be specified and do not assign unnecessary fields. // ID must be specified and do not assign unnecessary fields.
func deleteWebhook(bean *Webhook) (err error) { func deleteWebhook(bean *Webhook) (err error) {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err = sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
if count, err := sess.Delete(bean); err != nil { if count, err := db.DeleteByBean(ctx, bean); err != nil {
return err return err
} else if count == 0 { } else if count == 0 {
return ErrWebhookNotExist{ID: bean.ID} return ErrWebhookNotExist{ID: bean.ID}
} else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil { } else if _, err = db.DeleteByBean(ctx, &HookTask{HookID: bean.ID}); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// DeleteWebhookByRepoID deletes webhook of repository by given ID. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
@ -542,13 +542,13 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0) // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
func DeleteDefaultSystemWebhook(id int64) error { func DeleteDefaultSystemWebhook(id int64) error {
sess := db.NewSession(db.DefaultContext) ctx, committer, err := db.TxContext()
defer sess.Close() if err != nil {
if err := sess.Begin(); err != nil {
return err return err
} }
defer committer.Close()
count, err := sess. count, err := db.GetEngine(ctx).
Where("repo_id=? AND org_id=?", 0, 0). Where("repo_id=? AND org_id=?", 0, 0).
Delete(&Webhook{ID: id}) Delete(&Webhook{ID: id})
if err != nil { if err != nil {
@ -557,11 +557,11 @@ func DeleteDefaultSystemWebhook(id int64) error {
return ErrWebhookNotExist{ID: id} return ErrWebhookNotExist{ID: id}
} }
if _, err := sess.Delete(&HookTask{HookID: id}); err != nil { if _, err := db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil {
return err return err
} }
return sess.Commit() return committer.Commit()
} }
// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo // CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo