mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/mutation: reject querying old values of finished mutations (#495)
This commit is contained in:
@@ -78,6 +78,7 @@ func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
|
||||
}
|
||||
cc.mutation = mutation
|
||||
node, err = cc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(cc.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -47,6 +47,7 @@ func (cd *CarDelete) Exec(ctx context.Context) (int, error) {
|
||||
}
|
||||
cd.mutation = mutation
|
||||
affected, err = cd.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(cd.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -87,6 +87,7 @@ func (cu *CarUpdate) Save(ctx context.Context) (int, error) {
|
||||
}
|
||||
cu.mutation = mutation
|
||||
affected, err = cu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(cu.hooks) - 1; i >= 0; i-- {
|
||||
@@ -260,6 +261,7 @@ func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) {
|
||||
}
|
||||
cuo.mutation = mutation
|
||||
node, err = cuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(cuo.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -69,6 +69,7 @@ func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
}
|
||||
gc.mutation = mutation
|
||||
node, err = gc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(gc.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -47,6 +47,7 @@ func (gd *GroupDelete) Exec(ctx context.Context) (int, error) {
|
||||
}
|
||||
gd.mutation = mutation
|
||||
affected, err = gd.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(gd.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -90,6 +90,7 @@ func (gu *GroupUpdate) Save(ctx context.Context) (int, error) {
|
||||
}
|
||||
gu.mutation = mutation
|
||||
affected, err = gu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(gu.hooks) - 1; i >= 0; i-- {
|
||||
@@ -263,6 +264,7 @@ func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
|
||||
}
|
||||
guo.mutation = mutation
|
||||
node, err = guo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(guo.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -45,6 +45,7 @@ type CarMutation struct {
|
||||
clearedFields map[string]struct{}
|
||||
owner *int
|
||||
clearedowner bool
|
||||
done bool
|
||||
oldValue func(context.Context) (*Car, error)
|
||||
}
|
||||
|
||||
@@ -77,7 +78,11 @@ func withCarID(id int) carOption {
|
||||
)
|
||||
m.oldValue = func(ctx context.Context) (*Car, error) {
|
||||
once.Do(func() {
|
||||
value, err = m.Client().Car.Get(ctx, id)
|
||||
if m.done {
|
||||
err = fmt.Errorf("querying old values post mutation is not allowed")
|
||||
} else {
|
||||
value, err = m.Client().Car.Get(ctx, id)
|
||||
}
|
||||
})
|
||||
return value, err
|
||||
}
|
||||
@@ -452,6 +457,7 @@ type GroupMutation struct {
|
||||
clearedFields map[string]struct{}
|
||||
users map[int]struct{}
|
||||
removedusers map[int]struct{}
|
||||
done bool
|
||||
oldValue func(context.Context) (*Group, error)
|
||||
}
|
||||
|
||||
@@ -484,7 +490,11 @@ func withGroupID(id int) groupOption {
|
||||
)
|
||||
m.oldValue = func(ctx context.Context) (*Group, error) {
|
||||
once.Do(func() {
|
||||
value, err = m.Client().Group.Get(ctx, id)
|
||||
if m.done {
|
||||
err = fmt.Errorf("querying old values post mutation is not allowed")
|
||||
} else {
|
||||
value, err = m.Client().Group.Get(ctx, id)
|
||||
}
|
||||
})
|
||||
return value, err
|
||||
}
|
||||
@@ -817,6 +827,7 @@ type UserMutation struct {
|
||||
removedcars map[int]struct{}
|
||||
groups map[int]struct{}
|
||||
removedgroups map[int]struct{}
|
||||
done bool
|
||||
oldValue func(context.Context) (*User, error)
|
||||
}
|
||||
|
||||
@@ -849,7 +860,11 @@ func withUserID(id int) userOption {
|
||||
)
|
||||
m.oldValue = func(ctx context.Context) (*User, error) {
|
||||
once.Do(func() {
|
||||
value, err = m.Client().User.Get(ctx, id)
|
||||
if m.done {
|
||||
err = fmt.Errorf("querying old values post mutation is not allowed")
|
||||
} else {
|
||||
value, err = m.Client().User.Get(ctx, id)
|
||||
}
|
||||
})
|
||||
return value, err
|
||||
}
|
||||
|
||||
@@ -103,6 +103,7 @@ func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
}
|
||||
uc.mutation = mutation
|
||||
node, err = uc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(uc.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -47,6 +47,7 @@ func (ud *UserDelete) Exec(ctx context.Context) (int, error) {
|
||||
}
|
||||
ud.mutation = mutation
|
||||
affected, err = ud.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(ud.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -142,6 +142,7 @@ func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
}
|
||||
uu.mutation = mutation
|
||||
affected, err = uu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(uu.hooks) - 1; i >= 0; i-- {
|
||||
@@ -418,6 +419,7 @@ func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
}
|
||||
uuo.mutation = mutation
|
||||
node, err = uuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(uuo.hooks) - 1; i >= 0; i-- {
|
||||
|
||||
Reference in New Issue
Block a user