|
|
@ -7,7 +7,6 @@ import (
|
|
|
|
"entgo.io/ent/schema/field"
|
|
|
|
"entgo.io/ent/schema/field"
|
|
|
|
"entgo.io/ent/schema/mixin"
|
|
|
|
"entgo.io/ent/schema/mixin"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/casbin/ent-adapter/ent/hook"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// OptimisticLockMixin implements the ent.Mixin for sharing
|
|
|
|
// OptimisticLockMixin implements the ent.Mixin for sharing
|
|
|
@ -34,9 +33,9 @@ func WithVersion(parent context.Context, version int64) context.Context {
|
|
|
|
// Hooks of the OptimisticLockMixin.
|
|
|
|
// Hooks of the OptimisticLockMixin.
|
|
|
|
func (d OptimisticLockMixin) Hooks() []ent.Hook {
|
|
|
|
func (d OptimisticLockMixin) Hooks() []ent.Hook {
|
|
|
|
return []ent.Hook{
|
|
|
|
return []ent.Hook{
|
|
|
|
hook.On(
|
|
|
|
|
|
|
|
func(next ent.Mutator) ent.Mutator {
|
|
|
|
func(next ent.Mutator) ent.Mutator {
|
|
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
|
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
|
|
|
|
|
|
|
if m.Op().Is(ent.OpUpdate | ent.OpUpdateOne) {
|
|
|
|
mx, ok := m.(interface {
|
|
|
|
mx, ok := m.(interface {
|
|
|
|
AddVersion(i int64)
|
|
|
|
AddVersion(i int64)
|
|
|
|
WhereP(...func(*sql.Selector))
|
|
|
|
WhereP(...func(*sql.Selector))
|
|
|
@ -52,10 +51,10 @@ func (d OptimisticLockMixin) Hooks() []ent.Hook {
|
|
|
|
// set version=version+1
|
|
|
|
// set version=version+1
|
|
|
|
mx.AddVersion(1)
|
|
|
|
mx.AddVersion(1)
|
|
|
|
return next.Mutate(ctx, m)
|
|
|
|
return next.Mutate(ctx, m)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return next.Mutate(ctx, m)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ent.OpUpdate|ent.OpUpdateOne,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|