mirror of
https://github.com/go-gitea/gitea
synced 2025-08-29 04:48:28 +00:00
Remove global context from db package (#35371)
This commit is contained in:
@@ -21,28 +21,8 @@ type engineContextKeyType struct{}
|
|||||||
|
|
||||||
var engineContextKey = engineContextKeyType{}
|
var engineContextKey = engineContextKeyType{}
|
||||||
|
|
||||||
type xormContextType struct {
|
func withContextEngine(ctx context.Context, e Engine) context.Context {
|
||||||
context.Context
|
return context.WithValue(ctx, engineContextKey, e)
|
||||||
engine Engine
|
|
||||||
}
|
|
||||||
|
|
||||||
var xormContext *xormContextType
|
|
||||||
|
|
||||||
func newContext(ctx context.Context, e Engine) *xormContextType {
|
|
||||||
return &xormContextType{Context: ctx, engine: e}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object
|
|
||||||
func (ctx *xormContextType) Value(key any) any {
|
|
||||||
if key == engineContextKey {
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
return ctx.Context.Value(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithContext returns this engine tied to this context
|
|
||||||
func (ctx *xormContextType) WithContext(other context.Context) *xormContextType {
|
|
||||||
return newContext(ctx, ctx.engine.Context(other))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -89,8 +69,8 @@ func contextSafetyCheck(e Engine) {
|
|||||||
// GetEngine gets an existing db Engine/Statement or creates a new Session
|
// GetEngine gets an existing db Engine/Statement or creates a new Session
|
||||||
func GetEngine(ctx context.Context) (e Engine) {
|
func GetEngine(ctx context.Context) (e Engine) {
|
||||||
defer func() { contextSafetyCheck(e) }()
|
defer func() { contextSafetyCheck(e) }()
|
||||||
if e := getExistingEngine(ctx); e != nil {
|
if engine, ok := ctx.Value(engineContextKey).(Engine); ok {
|
||||||
return e
|
return engine
|
||||||
}
|
}
|
||||||
return xormEngine.Context(ctx)
|
return xormEngine.Context(ctx)
|
||||||
}
|
}
|
||||||
@@ -99,17 +79,6 @@ func GetXORMEngineForTesting() *xorm.Engine {
|
|||||||
return xormEngine
|
return xormEngine
|
||||||
}
|
}
|
||||||
|
|
||||||
// getExistingEngine gets an existing db Engine/Statement from this context or returns nil
|
|
||||||
func getExistingEngine(ctx context.Context) (e Engine) {
|
|
||||||
if engined, ok := ctx.(*xormContextType); ok {
|
|
||||||
return engined.engine
|
|
||||||
}
|
|
||||||
if engined, ok := ctx.Value(engineContextKey).(*xormContextType); ok {
|
|
||||||
return engined.engine
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
@@ -152,8 +121,8 @@ func (c *halfCommitter) Close() error {
|
|||||||
// And all operations submitted by the caller stack will be rollbacked as well, not only the operations in the current function.
|
// And all operations submitted by the caller stack will be rollbacked as well, not only the operations in the current function.
|
||||||
// d. It doesn't mean rollback is forbidden, but always do it only when there is an error, and you do want to rollback.
|
// d. It doesn't mean rollback is forbidden, but always do it only when there is an error, and you do want to rollback.
|
||||||
func TxContext(parentCtx context.Context) (context.Context, Committer, error) {
|
func TxContext(parentCtx context.Context) (context.Context, Committer, error) {
|
||||||
if sess, ok := inTransaction(parentCtx); ok {
|
if sess := getTransactionSession(parentCtx); sess != nil {
|
||||||
return newContext(parentCtx, sess), &halfCommitter{committer: sess}, nil
|
return withContextEngine(parentCtx, sess), &halfCommitter{committer: sess}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := xormEngine.NewSession()
|
sess := xormEngine.NewSession()
|
||||||
@@ -161,15 +130,14 @@ func TxContext(parentCtx context.Context) (context.Context, Committer, error) {
|
|||||||
_ = sess.Close()
|
_ = sess.Close()
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
return withContextEngine(parentCtx, sess), sess, nil
|
||||||
return newContext(xormContext, sess), sess, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithTx represents executing database operations on a transaction, if the transaction exist,
|
// WithTx represents executing database operations on a transaction, if the transaction exist,
|
||||||
// this function will reuse it otherwise will create a new one and close it when finished.
|
// this function will reuse it otherwise will create a new one and close it when finished.
|
||||||
func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error {
|
func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error {
|
||||||
if sess, ok := inTransaction(parentCtx); ok {
|
if sess := getTransactionSession(parentCtx); sess != nil {
|
||||||
err := f(newContext(parentCtx, sess))
|
err := f(withContextEngine(parentCtx, sess))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// rollback immediately, in case the caller ignores returned error and tries to commit the transaction.
|
// rollback immediately, in case the caller ignores returned error and tries to commit the transaction.
|
||||||
_ = sess.Close()
|
_ = sess.Close()
|
||||||
@@ -195,7 +163,7 @@ func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := f(newContext(parentCtx, sess)); err != nil {
|
if err := f(withContextEngine(parentCtx, sess)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,25 +308,13 @@ func TableName(bean any) string {
|
|||||||
|
|
||||||
// InTransaction returns true if the engine is in a transaction otherwise return false
|
// InTransaction returns true if the engine is in a transaction otherwise return false
|
||||||
func InTransaction(ctx context.Context) bool {
|
func InTransaction(ctx context.Context) bool {
|
||||||
_, ok := inTransaction(ctx)
|
return getTransactionSession(ctx) != nil
|
||||||
return ok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func inTransaction(ctx context.Context) (*xorm.Session, bool) {
|
func getTransactionSession(ctx context.Context) *xorm.Session {
|
||||||
e := getExistingEngine(ctx)
|
e, _ := ctx.Value(engineContextKey).(Engine)
|
||||||
if e == nil {
|
if sess, ok := e.(*xorm.Session); ok && sess.IsInTx() {
|
||||||
return nil, false
|
return sess
|
||||||
}
|
|
||||||
|
|
||||||
switch t := e.(type) {
|
|
||||||
case *xorm.Engine:
|
|
||||||
return nil, false
|
|
||||||
case *xorm.Session:
|
|
||||||
if t.IsInTx() {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return nil, false
|
|
||||||
default:
|
|
||||||
return nil, false
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -86,7 +86,6 @@ func InitEngine(ctx context.Context) error {
|
|||||||
func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) {
|
func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) {
|
||||||
xormEngine = eng
|
xormEngine = eng
|
||||||
xormEngine.SetDefaultContext(ctx)
|
xormEngine.SetDefaultContext(ctx)
|
||||||
xormContext = &xormContextType{Context: ctx, engine: xormEngine}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsetDefaultEngine closes and unsets the default engine
|
// UnsetDefaultEngine closes and unsets the default engine
|
||||||
@@ -98,7 +97,6 @@ func UnsetDefaultEngine() {
|
|||||||
_ = xormEngine.Close()
|
_ = xormEngine.Close()
|
||||||
xormEngine = nil
|
xormEngine = nil
|
||||||
}
|
}
|
||||||
xormContext = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitEngineWithMigration initializes a new xorm.Engine and sets it as the XORM's default context
|
// InitEngineWithMigration initializes a new xorm.Engine and sets it as the XORM's default context
|
||||||
|
Reference in New Issue
Block a user