entc/gen/mutation: add IDs method for mutations

This commit is contained in:
Ariel Mashraki
2021-11-03 14:46:54 +02:00
committed by Ariel Mashraki
parent e8e4401d27
commit caa673826a
33 changed files with 2958 additions and 929 deletions

View File

@@ -9,6 +9,7 @@ package ent
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
@@ -82,7 +83,7 @@ func withUserID(id int) userOption {
m.oldValue = func(ctx context.Context) (*User, error) {
once.Do(func() {
if m.done {
err = fmt.Errorf("querying old values post mutation is not allowed")
err = errors.New("querying old values post mutation is not allowed")
} else {
value, err = m.Client().User.Get(ctx, id)
}
@@ -115,7 +116,7 @@ func (m UserMutation) Client() *Client {
// it returns an error otherwise.
func (m UserMutation) Tx() (*Tx, error) {
if _, ok := m.driver.(*txDriver); !ok {
return nil, fmt.Errorf("ent: mutation is not running in a transaction")
return nil, errors.New("ent: mutation is not running in a transaction")
}
tx := &Tx{config: m.config}
tx.init()
@@ -131,6 +132,25 @@ func (m *UserMutation) ID() (id int, exists bool) {
return *m.id, true
}
// IDs queries the database and returns the entity ids that match the mutation's predicate.
// That means, if the mutation is applied within a transaction with an isolation level such
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
// or updated by the mutation.
func (m *UserMutation) IDs(ctx context.Context) ([]int, error) {
switch {
case m.op.Is(OpUpdateOne | OpDeleteOne):
id, exists := m.ID()
if exists {
return []int{id}, nil
}
fallthrough
case m.op.Is(OpUpdate | OpDelete):
return m.Client().User.Query().Where(m.predicates...).IDs(ctx)
default:
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op)
}
}
// SetT sets the "t" field.
func (m *UserMutation) SetT(s *schema.T) {
m.t = &s
@@ -150,10 +170,10 @@ func (m *UserMutation) T() (r *schema.T, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldT(ctx context.Context) (v *schema.T, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldT is only allowed on UpdateOne operations")
return v, errors.New("OldT is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldT requires an ID field in the mutation")
return v, errors.New("OldT requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -199,10 +219,10 @@ func (m *UserMutation) URL() (r *url.URL, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldURL(ctx context.Context) (v *url.URL, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldURL is only allowed on UpdateOne operations")
return v, errors.New("OldURL is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldURL requires an ID field in the mutation")
return v, errors.New("OldURL requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -248,10 +268,10 @@ func (m *UserMutation) Raw() (r json.RawMessage, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldRaw(ctx context.Context) (v json.RawMessage, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldRaw is only allowed on UpdateOne operations")
return v, errors.New("OldRaw is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldRaw requires an ID field in the mutation")
return v, errors.New("OldRaw requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -297,10 +317,10 @@ func (m *UserMutation) Dirs() (r []http.Dir, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldDirs(ctx context.Context) (v []http.Dir, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldDirs is only allowed on UpdateOne operations")
return v, errors.New("OldDirs is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldDirs requires an ID field in the mutation")
return v, errors.New("OldDirs requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -333,10 +353,10 @@ func (m *UserMutation) Ints() (r []int, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldInts(ctx context.Context) (v []int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldInts is only allowed on UpdateOne operations")
return v, errors.New("OldInts is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldInts requires an ID field in the mutation")
return v, errors.New("OldInts requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -382,10 +402,10 @@ func (m *UserMutation) Floats() (r []float64, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldFloats(ctx context.Context) (v []float64, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldFloats is only allowed on UpdateOne operations")
return v, errors.New("OldFloats is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldFloats requires an ID field in the mutation")
return v, errors.New("OldFloats requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -431,10 +451,10 @@ func (m *UserMutation) Strings() (r []string, exists bool) {
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldStrings(ctx context.Context) (v []string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldStrings is only allowed on UpdateOne operations")
return v, errors.New("OldStrings is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldStrings requires an ID field in the mutation")
return v, errors.New("OldStrings requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {