entc/gen: adding tx callback support (#414)

Signed-off-by: Alex Snast <alexsn@fb.com>
This commit is contained in:
Alex Snast
2020-03-31 18:58:10 +03:00
committed by GitHub
parent cdd855c8c9
commit d6d95dd363
50 changed files with 1237 additions and 156 deletions

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -19,16 +20,47 @@ type Tx struct {
City *CityClient
// Street is the client for interacting with the Street builders.
Street *StreetClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -19,16 +20,47 @@ type Tx struct {
Group *GroupClient
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -19,16 +20,47 @@ type Tx struct {
Pet *PetClient
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// Node is the client for interacting with the Node builders.
Node *NodeClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -19,16 +20,47 @@ type Tx struct {
Card *CardClient
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -17,16 +18,47 @@ type Tx struct {
config
// Node is the client for interacting with the Node builders.
Node *NodeClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -21,16 +22,47 @@ type Tx struct {
Group *GroupClient
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.

View File

@@ -10,15 +10,28 @@ import (
"context"
)
type contextKey struct{}
type clientCtxKey struct{}
// FromContext returns the Client stored in a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(contextKey{}).(*Client)
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}
// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, contextKey{}, c)
return context.WithValue(parent, clientCtxKey{}, c)
}
type txCtxKey struct{}
// TxFromContext returns the Tx stored in a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}
// NewTxContext returns a new context with the given Client attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

View File

@@ -8,6 +8,7 @@ package ent
import (
"context"
"sync"
"github.com/facebookincubator/ent/dialect"
)
@@ -21,16 +22,47 @@ type Tx struct {
Pet *PetClient
// User is the client for interacting with the User builders.
User *UserClient
// completion callbacks.
mu sync.Mutex
onCommit []func(error)
onRollback []func(error)
}
// Commit commits the transaction.
func (tx *Tx) Commit() error {
return tx.config.driver.(*txDriver).tx.Commit()
err := tx.config.driver.(*txDriver).tx.Commit()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onCommit {
f(err)
}
return err
}
// OnCommit adds a function to call on commit.
func (tx *Tx) OnCommit(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onCommit = append(tx.onCommit, f)
}
// Rollback rollbacks the transaction.
func (tx *Tx) Rollback() error {
return tx.config.driver.(*txDriver).tx.Rollback()
err := tx.config.driver.(*txDriver).tx.Rollback()
tx.mu.Lock()
defer tx.mu.Unlock()
for _, f := range tx.onRollback {
f(err)
}
return err
}
// OnRollback adds a function to call on rollback.
func (tx *Tx) OnRollback(f func(error)) {
tx.mu.Lock()
defer tx.mu.Unlock()
tx.onRollback = append(tx.onRollback, f)
}
// Client returns a Client that binds to current transaction.