Chore: Deduplicate sqlstore transaction code (#17069)

* Deduplicate transaction code

* More deduplication
This commit is contained in:
Andrej Ocenas
2019-05-16 12:39:59 +02:00
committed by GitHub
parent 2904e2d9fe
commit 3dbc3251d1
3 changed files with 29 additions and 102 deletions
+15
View File
@@ -18,6 +18,11 @@ func (sess *DBSession) publishAfterCommit(msg interface{}) {
sess.events = append(sess.events, msg)
}
// NewSession returns a new DBSession
func (ss *SqlStore) NewSession() *DBSession {
return &DBSession{Session: ss.engine.NewSession()}
}
func newSession() *DBSession {
return &DBSession{Session: x.NewSession()}
}
@@ -41,6 +46,16 @@ func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DB
return newSess, nil
}
// WithDbSession calls the callback with an session attached to the context.
func (ss *SqlStore) WithDbSession(ctx context.Context, callback dbTransactionFunc) error {
sess, err := startSession(ctx, ss.engine, false)
if err != nil {
return err
}
return callback(sess)
}
func withDbSession(ctx context.Context, callback dbTransactionFunc) error {
sess, err := startSession(ctx, x, false)
if err != nil {