diff options
Diffstat (limited to 'models/db/context.go')
-rw-r--r-- | models/db/context.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/models/db/context.go b/models/db/context.go index 1cd23d453c..c823952cf6 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -103,7 +103,14 @@ func WithContext(f func(ctx *Context) error) error { } // WithTx represents executing database operations on a transaction -func WithTx(f func(ctx context.Context) error) error { +// you can optionally change the context to a parrent one +func WithTx(f func(ctx context.Context) error, stdCtx ...context.Context) error { + parentCtx := DefaultContext + if len(stdCtx) != 0 && stdCtx[0] != nil { + // TODO: make sure parent context has no open session + parentCtx = stdCtx[0] + } + sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -111,7 +118,7 @@ func WithTx(f func(ctx context.Context) error) error { } if err := f(&Context{ - Context: DefaultContext, + Context: parentCtx, e: sess, }); err != nil { return err |