diff --git a/entc/gen/template/builder/mutation.tmpl b/entc/gen/template/builder/mutation.tmpl index b2458bb3f..b05c569e9 100644 --- a/entc/gen/template/builder/mutation.tmpl +++ b/entc/gen/template/builder/mutation.tmpl @@ -100,7 +100,7 @@ func {{ $opt }}(id {{ $n.ID.Type }}) {{ $mutationOption }} { m.oldValue = func(ctx context.Context) (*{{ $n.Name }}, 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().{{ $n.Name }}.Get(ctx, id) } @@ -134,7 +134,7 @@ func (m {{ $mutation }}) Client() *Client { // it returns an error otherwise. func (m {{ $mutation }}) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("{{ $pkg }}: mutation is not running in a transaction") + return nil, errors.New("{{ $pkg }}: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -158,6 +158,26 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) { return *m.{{ $n.ID.BuilderField }}, 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 *{{ $mutation }}) IDs(ctx context.Context) ([]{{ $n.ID.Type }}, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []{{ $n.ID.Type }}{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().{{ $n.Name }}.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + + {{ range $f := $n.Fields }} {{ $const := print $n.Package "." $f.Constant }} {{ $p := receiver $f.Type.String }}{{ if eq $p "m" }} {{ $p = "value" }} {{ end }} @@ -185,10 +205,10 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *{{ $mutation }}) {{ $f.MutationGetOld }}(ctx context.Context) (v {{ if $f.NillableValue }}*{{ end }}{{ $f.Type }}, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("{{ $f.MutationGetOld }} is only allowed on UpdateOne operations") + return v, errors.New("{{ $f.MutationGetOld }} is only allowed on UpdateOne operations") } if m.{{ $n.ID.BuilderField }} == nil || m.oldValue == nil { - return v, fmt.Errorf("{{ $f.MutationGetOld }} requires an ID field in the mutation") + return v, errors.New("{{ $f.MutationGetOld }} requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/cascadelete/ent/mutation.go b/entc/integration/cascadelete/ent/mutation.go index d31ef5e61..1ec05e1b5 100644 --- a/entc/integration/cascadelete/ent/mutation.go +++ b/entc/integration/cascadelete/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -78,7 +79,7 @@ func withCommentID(id int) commentOption { m.oldValue = func(ctx context.Context) (*Comment, 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().Comment.Get(ctx, id) } @@ -111,7 +112,7 @@ func (m CommentMutation) Client() *Client { // it returns an error otherwise. func (m CommentMutation) 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() @@ -127,6 +128,25 @@ func (m *CommentMutation) 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 *CommentMutation) 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().Comment.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *CommentMutation) SetText(s string) { m.text = &s @@ -146,10 +166,10 @@ func (m *CommentMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -182,10 +202,10 @@ func (m *CommentMutation) PostID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldPostID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPostID is only allowed on UpdateOne operations") + return v, errors.New("OldPostID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPostID requires an ID field in the mutation") + return v, errors.New("OldPostID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -485,7 +505,7 @@ func withPostID(id int) postOption { m.oldValue = func(ctx context.Context) (*Post, 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().Post.Get(ctx, id) } @@ -518,7 +538,7 @@ func (m PostMutation) Client() *Client { // it returns an error otherwise. func (m PostMutation) 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() @@ -534,6 +554,25 @@ func (m *PostMutation) 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 *PostMutation) 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().Post.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *PostMutation) SetText(s string) { m.text = &s @@ -553,10 +592,10 @@ func (m *PostMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PostMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -589,10 +628,10 @@ func (m *PostMutation) AuthorID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PostMutation) OldAuthorID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAuthorID is only allowed on UpdateOne operations") + return v, errors.New("OldAuthorID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAuthorID requires an ID field in the mutation") + return v, errors.New("OldAuthorID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -992,7 +1031,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) } @@ -1025,7 +1064,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() @@ -1041,6 +1080,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -1060,10 +1118,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/config/ent/mutation.go b/entc/integration/config/ent/mutation.go index 35dd32dd5..1201a2830 100644 --- a/entc/integration/config/ent/mutation.go +++ b/entc/integration/config/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -73,7 +74,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) } @@ -106,7 +107,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() @@ -128,6 +129,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -147,10 +167,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -196,10 +216,10 @@ func (m *UserMutation) Label() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldLabel(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLabel is only allowed on UpdateOne operations") + return v, errors.New("OldLabel is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLabel requires an ID field in the mutation") + return v, errors.New("OldLabel requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/customid/ent/mutation.go b/entc/integration/customid/ent/mutation.go index 0cac9536c..435b85390 100644 --- a/entc/integration/customid/ent/mutation.go +++ b/entc/integration/customid/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -99,7 +100,7 @@ func withBlobID(id uuid.UUID) blobOption { m.oldValue = func(ctx context.Context) (*Blob, 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().Blob.Get(ctx, id) } @@ -132,7 +133,7 @@ func (m BlobMutation) Client() *Client { // it returns an error otherwise. func (m BlobMutation) 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() @@ -154,6 +155,25 @@ func (m *BlobMutation) ID() (id uuid.UUID, 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 *BlobMutation) IDs(ctx context.Context) ([]uuid.UUID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []uuid.UUID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Blob.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetUUID sets the "uuid" field. func (m *BlobMutation) SetUUID(u uuid.UUID) { m.uuid = &u @@ -173,10 +193,10 @@ func (m *BlobMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *BlobMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -210,10 +230,10 @@ func (m *BlobMutation) Count() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *BlobMutation) OldCount(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCount is only allowed on UpdateOne operations") + return v, errors.New("OldCount is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCount requires an ID field in the mutation") + return v, errors.New("OldCount requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -638,7 +658,7 @@ func withCarID(id int) carOption { m.oldValue = func(ctx context.Context) (*Car, 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().Car.Get(ctx, id) } @@ -671,7 +691,7 @@ func (m CarMutation) Client() *Client { // it returns an error otherwise. func (m CarMutation) 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() @@ -693,6 +713,25 @@ func (m *CarMutation) 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 *CarMutation) 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().Car.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetBeforeID sets the "before_id" field. func (m *CarMutation) SetBeforeID(f float64) { m.before_id = &f @@ -713,10 +752,10 @@ func (m *CarMutation) BeforeID() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldBeforeID(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBeforeID is only allowed on UpdateOne operations") + return v, errors.New("OldBeforeID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBeforeID requires an ID field in the mutation") + return v, errors.New("OldBeforeID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -783,10 +822,10 @@ func (m *CarMutation) AfterID() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldAfterID(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAfterID is only allowed on UpdateOne operations") + return v, errors.New("OldAfterID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAfterID requires an ID field in the mutation") + return v, errors.New("OldAfterID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -852,10 +891,10 @@ func (m *CarMutation) Model() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldModel(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldModel is only allowed on UpdateOne operations") + return v, errors.New("OldModel is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldModel requires an ID field in the mutation") + return v, errors.New("OldModel requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1223,7 +1262,7 @@ func withDeviceID(id schema.ID) deviceOption { m.oldValue = func(ctx context.Context) (*Device, 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().Device.Get(ctx, id) } @@ -1256,7 +1295,7 @@ func (m DeviceMutation) Client() *Client { // it returns an error otherwise. func (m DeviceMutation) 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() @@ -1278,6 +1317,25 @@ func (m *DeviceMutation) ID() (id schema.ID, 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 *DeviceMutation) IDs(ctx context.Context) ([]schema.ID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []schema.ID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Device.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetActiveSessionID sets the "active_session" edge to the Session entity by id. func (m *DeviceMutation) SetActiveSessionID(id schema.ID) { m.active_session = &id @@ -1612,7 +1670,7 @@ func withDocID(id schema.DocID) docOption { m.oldValue = func(ctx context.Context) (*Doc, 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().Doc.Get(ctx, id) } @@ -1645,7 +1703,7 @@ func (m DocMutation) Client() *Client { // it returns an error otherwise. func (m DocMutation) 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() @@ -1667,6 +1725,25 @@ func (m *DocMutation) ID() (id schema.DocID, 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 *DocMutation) IDs(ctx context.Context) ([]schema.DocID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []schema.DocID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Doc.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *DocMutation) SetText(s string) { m.text = &s @@ -1686,10 +1763,10 @@ func (m *DocMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *DocMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2081,7 +2158,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -2114,7 +2191,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -2136,6 +2213,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // AddUserIDs adds the "users" edge to the User entity by ids. func (m *GroupMutation) AddUserIDs(ids ...int) { if m.users == nil { @@ -2409,7 +2505,7 @@ func withMixinIDID(id uuid.UUID) mixinidOption { m.oldValue = func(ctx context.Context) (*MixinID, 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().MixinID.Get(ctx, id) } @@ -2442,7 +2538,7 @@ func (m MixinIDMutation) Client() *Client { // it returns an error otherwise. func (m MixinIDMutation) 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() @@ -2464,6 +2560,25 @@ func (m *MixinIDMutation) ID() (id uuid.UUID, 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 *MixinIDMutation) IDs(ctx context.Context) ([]uuid.UUID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []uuid.UUID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().MixinID.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetSomeField sets the "some_field" field. func (m *MixinIDMutation) SetSomeField(s string) { m.some_field = &s @@ -2483,10 +2598,10 @@ func (m *MixinIDMutation) SomeField() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MixinIDMutation) OldSomeField(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSomeField is only allowed on UpdateOne operations") + return v, errors.New("OldSomeField is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSomeField requires an ID field in the mutation") + return v, errors.New("OldSomeField requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2519,10 +2634,10 @@ func (m *MixinIDMutation) MixinField() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MixinIDMutation) OldMixinField(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMixinField is only allowed on UpdateOne operations") + return v, errors.New("OldMixinField is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMixinField requires an ID field in the mutation") + return v, errors.New("OldMixinField requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2765,7 +2880,7 @@ func withNoteID(id schema.NoteID) noteOption { m.oldValue = func(ctx context.Context) (*Note, 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().Note.Get(ctx, id) } @@ -2798,7 +2913,7 @@ func (m NoteMutation) Client() *Client { // it returns an error otherwise. func (m NoteMutation) 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() @@ -2820,6 +2935,25 @@ func (m *NoteMutation) ID() (id schema.NoteID, 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 *NoteMutation) IDs(ctx context.Context) ([]schema.NoteID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []schema.NoteID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Note.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *NoteMutation) SetText(s string) { m.text = &s @@ -2839,10 +2973,10 @@ func (m *NoteMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NoteMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3241,7 +3375,7 @@ func withPetID(id string) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -3274,7 +3408,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -3296,6 +3430,25 @@ func (m *PetMutation) ID() (id string, 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 *PetMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *PetMutation) SetOwnerID(id int) { m.owner = &id @@ -3763,7 +3916,7 @@ func withSessionID(id schema.ID) sessionOption { m.oldValue = func(ctx context.Context) (*Session, 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().Session.Get(ctx, id) } @@ -3796,7 +3949,7 @@ func (m SessionMutation) Client() *Client { // it returns an error otherwise. func (m SessionMutation) 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() @@ -3818,6 +3971,25 @@ func (m *SessionMutation) ID() (id schema.ID, 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 *SessionMutation) IDs(ctx context.Context) ([]schema.ID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []schema.ID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Session.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetDeviceID sets the "device" edge to the Device entity by id. func (m *SessionMutation) SetDeviceID(id schema.ID) { m.device = &id @@ -4077,7 +4249,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) } @@ -4110,7 +4282,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() @@ -4132,6 +4304,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) + } +} + // AddGroupIDs adds the "groups" edge to the Group entity by ids. func (m *UserMutation) AddGroupIDs(ids ...int) { if m.groups == nil { diff --git a/entc/integration/edgefield/ent/mutation.go b/entc/integration/edgefield/ent/mutation.go index dfffa0fc1..0de8c1081 100644 --- a/entc/integration/edgefield/ent/mutation.go +++ b/entc/integration/edgefield/ent/mutation.go @@ -9,6 +9,7 @@ package ent import ( "context" "encoding/json" + "errors" "fmt" "sync" "time" @@ -94,7 +95,7 @@ func withCarID(id uuid.UUID) carOption { m.oldValue = func(ctx context.Context) (*Car, 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().Car.Get(ctx, id) } @@ -127,7 +128,7 @@ func (m CarMutation) Client() *Client { // it returns an error otherwise. func (m CarMutation) 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() @@ -149,6 +150,25 @@ func (m *CarMutation) ID() (id uuid.UUID, 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 *CarMutation) IDs(ctx context.Context) ([]uuid.UUID, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []uuid.UUID{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Car.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetNumber sets the "number" field. func (m *CarMutation) SetNumber(s string) { m.number = &s @@ -168,10 +188,10 @@ func (m *CarMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -506,7 +526,7 @@ func withCardID(id int) cardOption { m.oldValue = func(ctx context.Context) (*Card, 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().Card.Get(ctx, id) } @@ -539,7 +559,7 @@ func (m CardMutation) Client() *Client { // it returns an error otherwise. func (m CardMutation) 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() @@ -555,6 +575,25 @@ func (m *CardMutation) 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 *CardMutation) 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().Card.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetNumber sets the "number" field. func (m *CardMutation) SetNumber(s string) { m.number = &s @@ -574,10 +613,10 @@ func (m *CardMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -623,10 +662,10 @@ func (m *CardMutation) OwnerID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldOwnerID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOwnerID is only allowed on UpdateOne operations") + return v, errors.New("OldOwnerID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOwnerID requires an ID field in the mutation") + return v, errors.New("OldOwnerID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -951,7 +990,7 @@ func withInfoID(id int) infoOption { m.oldValue = func(ctx context.Context) (*Info, 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().Info.Get(ctx, id) } @@ -984,7 +1023,7 @@ func (m InfoMutation) Client() *Client { // it returns an error otherwise. func (m InfoMutation) 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() @@ -1006,6 +1045,25 @@ func (m *InfoMutation) 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 *InfoMutation) 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().Info.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetContent sets the "content" field. func (m *InfoMutation) SetContent(jm json.RawMessage) { m.content = &jm @@ -1025,10 +1083,10 @@ func (m *InfoMutation) Content() (r json.RawMessage, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *InfoMutation) OldContent(ctx context.Context) (v json.RawMessage, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldContent is only allowed on UpdateOne operations") + return v, errors.New("OldContent is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldContent requires an ID field in the mutation") + return v, errors.New("OldContent requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1324,7 +1382,7 @@ func withMetadataID(id int) metadataOption { m.oldValue = func(ctx context.Context) (*Metadata, 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().Metadata.Get(ctx, id) } @@ -1357,7 +1415,7 @@ func (m MetadataMutation) Client() *Client { // it returns an error otherwise. func (m MetadataMutation) 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() @@ -1379,6 +1437,25 @@ func (m *MetadataMutation) 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 *MetadataMutation) 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().Metadata.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetAge sets the "age" field. func (m *MetadataMutation) SetAge(i int) { m.age = &i @@ -1399,10 +1476,10 @@ func (m *MetadataMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MetadataMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1454,10 +1531,10 @@ func (m *MetadataMutation) ParentID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MetadataMutation) OldParentID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldParentID is only allowed on UpdateOne operations") + return v, errors.New("OldParentID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldParentID requires an ID field in the mutation") + return v, errors.New("OldParentID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1928,7 +2005,7 @@ func withNodeID(id int) nodeOption { m.oldValue = func(ctx context.Context) (*Node, 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().Node.Get(ctx, id) } @@ -1961,7 +2038,7 @@ func (m NodeMutation) Client() *Client { // it returns an error otherwise. func (m NodeMutation) 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() @@ -1977,6 +2054,25 @@ func (m *NodeMutation) 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 *NodeMutation) 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().Node.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetValue sets the "value" field. func (m *NodeMutation) SetValue(i int) { m.value = &i @@ -1997,10 +2093,10 @@ func (m *NodeMutation) Value() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValue is only allowed on UpdateOne operations") + return v, errors.New("OldValue is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValue requires an ID field in the mutation") + return v, errors.New("OldValue requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2052,10 +2148,10 @@ func (m *NodeMutation) PrevID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldPrevID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPrevID is only allowed on UpdateOne operations") + return v, errors.New("OldPrevID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPrevID requires an ID field in the mutation") + return v, errors.New("OldPrevID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2442,7 +2538,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -2475,7 +2571,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -2491,6 +2587,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetOwnerID sets the "owner_id" field. func (m *PetMutation) SetOwnerID(i int) { m.owner = &i @@ -2510,10 +2625,10 @@ func (m *PetMutation) OwnerID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldOwnerID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOwnerID is only allowed on UpdateOne operations") + return v, errors.New("OldOwnerID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOwnerID requires an ID field in the mutation") + return v, errors.New("OldOwnerID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2815,7 +2930,7 @@ func withPostID(id int) postOption { m.oldValue = func(ctx context.Context) (*Post, 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().Post.Get(ctx, id) } @@ -2848,7 +2963,7 @@ func (m PostMutation) Client() *Client { // it returns an error otherwise. func (m PostMutation) 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() @@ -2864,6 +2979,25 @@ func (m *PostMutation) 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 *PostMutation) 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().Post.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *PostMutation) SetText(s string) { m.text = &s @@ -2883,10 +3017,10 @@ func (m *PostMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PostMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2919,10 +3053,10 @@ func (m *PostMutation) AuthorID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PostMutation) OldAuthorID(ctx context.Context) (v *int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAuthorID is only allowed on UpdateOne operations") + return v, errors.New("OldAuthorID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAuthorID requires an ID field in the mutation") + return v, errors.New("OldAuthorID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3243,7 +3377,7 @@ func withRentalID(id int) rentalOption { m.oldValue = func(ctx context.Context) (*Rental, 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().Rental.Get(ctx, id) } @@ -3276,7 +3410,7 @@ func (m RentalMutation) Client() *Client { // it returns an error otherwise. func (m RentalMutation) 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() @@ -3292,6 +3426,25 @@ func (m *RentalMutation) 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 *RentalMutation) 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().Rental.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetDate sets the "date" field. func (m *RentalMutation) SetDate(t time.Time) { m.date = &t @@ -3311,10 +3464,10 @@ func (m *RentalMutation) Date() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *RentalMutation) OldDate(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDate is only allowed on UpdateOne operations") + return v, errors.New("OldDate is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDate requires an ID field in the mutation") + return v, errors.New("OldDate requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3347,10 +3500,10 @@ func (m *RentalMutation) UserID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *RentalMutation) OldUserID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUserID is only allowed on UpdateOne operations") + return v, errors.New("OldUserID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUserID requires an ID field in the mutation") + return v, errors.New("OldUserID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3383,10 +3536,10 @@ func (m *RentalMutation) CarID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *RentalMutation) OldCarID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCarID is only allowed on UpdateOne operations") + return v, errors.New("OldCarID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCarID requires an ID field in the mutation") + return v, errors.New("OldCarID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3761,7 +3914,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) } @@ -3794,7 +3947,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() @@ -3816,6 +3969,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) + } +} + // SetParentID sets the "parent_id" field. func (m *UserMutation) SetParentID(i int) { m.parent = &i @@ -3835,10 +4007,10 @@ func (m *UserMutation) ParentID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldParentID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldParentID is only allowed on UpdateOne operations") + return v, errors.New("OldParentID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldParentID requires an ID field in the mutation") + return v, errors.New("OldParentID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3884,10 +4056,10 @@ func (m *UserMutation) SpouseID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldSpouseID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSpouseID is only allowed on UpdateOne operations") + return v, errors.New("OldSpouseID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSpouseID requires an ID field in the mutation") + return v, errors.New("OldSpouseID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index fbf5adac5..786c431d0 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "net" "net/http" @@ -114,7 +115,7 @@ func withCardID(id int) cardOption { m.oldValue = func(ctx context.Context) (*Card, 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().Card.Get(ctx, id) } @@ -147,7 +148,7 @@ func (m CardMutation) Client() *Client { // it returns an error otherwise. func (m CardMutation) 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() @@ -163,6 +164,25 @@ func (m *CardMutation) 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 *CardMutation) 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().Card.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetCreateTime sets the "create_time" field. func (m *CardMutation) SetCreateTime(t time.Time) { m.create_time = &t @@ -182,10 +202,10 @@ func (m *CardMutation) CreateTime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldCreateTime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCreateTime is only allowed on UpdateOne operations") + return v, errors.New("OldCreateTime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCreateTime requires an ID field in the mutation") + return v, errors.New("OldCreateTime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -218,10 +238,10 @@ func (m *CardMutation) UpdateTime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldUpdateTime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUpdateTime is only allowed on UpdateOne operations") + return v, errors.New("OldUpdateTime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUpdateTime requires an ID field in the mutation") + return v, errors.New("OldUpdateTime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -255,10 +275,10 @@ func (m *CardMutation) Balance() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldBalance(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBalance is only allowed on UpdateOne operations") + return v, errors.New("OldBalance is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBalance requires an ID field in the mutation") + return v, errors.New("OldBalance requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -310,10 +330,10 @@ func (m *CardMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -346,10 +366,10 @@ func (m *CardMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -827,7 +847,7 @@ func withCommentID(id int) commentOption { m.oldValue = func(ctx context.Context) (*Comment, 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().Comment.Get(ctx, id) } @@ -860,7 +880,7 @@ func (m CommentMutation) Client() *Client { // it returns an error otherwise. func (m CommentMutation) 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() @@ -876,6 +896,25 @@ func (m *CommentMutation) 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 *CommentMutation) 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().Comment.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetUniqueInt sets the "unique_int" field. func (m *CommentMutation) SetUniqueInt(i int) { m.unique_int = &i @@ -896,10 +935,10 @@ func (m *CommentMutation) UniqueInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldUniqueInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUniqueInt is only allowed on UpdateOne operations") + return v, errors.New("OldUniqueInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUniqueInt requires an ID field in the mutation") + return v, errors.New("OldUniqueInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -952,10 +991,10 @@ func (m *CommentMutation) UniqueFloat() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldUniqueFloat(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUniqueFloat is only allowed on UpdateOne operations") + return v, errors.New("OldUniqueFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUniqueFloat requires an ID field in the mutation") + return v, errors.New("OldUniqueFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1008,10 +1047,10 @@ func (m *CommentMutation) NillableInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldNillableInt(ctx context.Context) (v *int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt requires an ID field in the mutation") + return v, errors.New("OldNillableInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1441,7 +1480,7 @@ func withFieldTypeID(id int) fieldtypeOption { m.oldValue = func(ctx context.Context) (*FieldType, 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().FieldType.Get(ctx, id) } @@ -1474,7 +1513,7 @@ func (m FieldTypeMutation) Client() *Client { // it returns an error otherwise. func (m FieldTypeMutation) 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() @@ -1490,6 +1529,25 @@ func (m *FieldTypeMutation) 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 *FieldTypeMutation) 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().FieldType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetInt sets the "int" field. func (m *FieldTypeMutation) SetInt(i int) { m.int = &i @@ -1510,10 +1568,10 @@ func (m *FieldTypeMutation) Int() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt is only allowed on UpdateOne operations") + return v, errors.New("OldInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt requires an ID field in the mutation") + return v, errors.New("OldInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1566,10 +1624,10 @@ func (m *FieldTypeMutation) Int8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt8(ctx context.Context) (v int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt8 requires an ID field in the mutation") + return v, errors.New("OldInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1622,10 +1680,10 @@ func (m *FieldTypeMutation) Int16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt16(ctx context.Context) (v int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt16 requires an ID field in the mutation") + return v, errors.New("OldInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1678,10 +1736,10 @@ func (m *FieldTypeMutation) Int32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt32 requires an ID field in the mutation") + return v, errors.New("OldInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1734,10 +1792,10 @@ func (m *FieldTypeMutation) Int64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt64(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt64 requires an ID field in the mutation") + return v, errors.New("OldInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1790,10 +1848,10 @@ func (m *FieldTypeMutation) OptionalInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt requires an ID field in the mutation") + return v, errors.New("OldOptionalInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1860,10 +1918,10 @@ func (m *FieldTypeMutation) OptionalInt8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt8(ctx context.Context) (v int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt8 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1930,10 +1988,10 @@ func (m *FieldTypeMutation) OptionalInt16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt16(ctx context.Context) (v int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt16 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2000,10 +2058,10 @@ func (m *FieldTypeMutation) OptionalInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt32 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2070,10 +2128,10 @@ func (m *FieldTypeMutation) OptionalInt64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt64(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt64 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2140,10 +2198,10 @@ func (m *FieldTypeMutation) NillableInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt(ctx context.Context) (v *int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt requires an ID field in the mutation") + return v, errors.New("OldNillableInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2210,10 +2268,10 @@ func (m *FieldTypeMutation) NillableInt8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt8(ctx context.Context) (v *int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt8 requires an ID field in the mutation") + return v, errors.New("OldNillableInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2280,10 +2338,10 @@ func (m *FieldTypeMutation) NillableInt16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt16(ctx context.Context) (v *int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt16 requires an ID field in the mutation") + return v, errors.New("OldNillableInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2350,10 +2408,10 @@ func (m *FieldTypeMutation) NillableInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt32(ctx context.Context) (v *int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt32 requires an ID field in the mutation") + return v, errors.New("OldNillableInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2420,10 +2478,10 @@ func (m *FieldTypeMutation) NillableInt64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt64(ctx context.Context) (v *int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt64 requires an ID field in the mutation") + return v, errors.New("OldNillableInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2490,10 +2548,10 @@ func (m *FieldTypeMutation) ValidateOptionalInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldValidateOptionalInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValidateOptionalInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldValidateOptionalInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValidateOptionalInt32 requires an ID field in the mutation") + return v, errors.New("OldValidateOptionalInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2560,10 +2618,10 @@ func (m *FieldTypeMutation) OptionalUint() (r uint, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint(ctx context.Context) (v uint, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint requires an ID field in the mutation") + return v, errors.New("OldOptionalUint requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2630,10 +2688,10 @@ func (m *FieldTypeMutation) OptionalUint8() (r uint8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint8(ctx context.Context) (v uint8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint8 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint8 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2700,10 +2758,10 @@ func (m *FieldTypeMutation) OptionalUint16() (r uint16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint16(ctx context.Context) (v uint16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint16 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint16 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2770,10 +2828,10 @@ func (m *FieldTypeMutation) OptionalUint32() (r uint32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint32(ctx context.Context) (v uint32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint32 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2840,10 +2898,10 @@ func (m *FieldTypeMutation) OptionalUint64() (r uint64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint64(ctx context.Context) (v uint64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint64 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint64 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2909,10 +2967,10 @@ func (m *FieldTypeMutation) State() (r fieldtype.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldState(ctx context.Context) (v fieldtype.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2959,10 +3017,10 @@ func (m *FieldTypeMutation) OptionalFloat() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalFloat(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalFloat is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalFloat requires an ID field in the mutation") + return v, errors.New("OldOptionalFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3029,10 +3087,10 @@ func (m *FieldTypeMutation) OptionalFloat32() (r float32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalFloat32(ctx context.Context) (v float32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalFloat32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalFloat32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalFloat32 requires an ID field in the mutation") + return v, errors.New("OldOptionalFloat32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3098,10 +3156,10 @@ func (m *FieldTypeMutation) Datetime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDatetime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDatetime is only allowed on UpdateOne operations") + return v, errors.New("OldDatetime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDatetime requires an ID field in the mutation") + return v, errors.New("OldDatetime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3148,10 +3206,10 @@ func (m *FieldTypeMutation) Decimal() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDecimal(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDecimal is only allowed on UpdateOne operations") + return v, errors.New("OldDecimal is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDecimal requires an ID field in the mutation") + return v, errors.New("OldDecimal requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3217,10 +3275,10 @@ func (m *FieldTypeMutation) LinkOther() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLinkOther(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLinkOther is only allowed on UpdateOne operations") + return v, errors.New("OldLinkOther is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLinkOther requires an ID field in the mutation") + return v, errors.New("OldLinkOther requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3266,10 +3324,10 @@ func (m *FieldTypeMutation) LinkOtherFunc() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLinkOtherFunc(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLinkOtherFunc is only allowed on UpdateOne operations") + return v, errors.New("OldLinkOtherFunc is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLinkOtherFunc requires an ID field in the mutation") + return v, errors.New("OldLinkOtherFunc requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3315,10 +3373,10 @@ func (m *FieldTypeMutation) MAC() (r schema.MAC, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldMAC(ctx context.Context) (v schema.MAC, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMAC is only allowed on UpdateOne operations") + return v, errors.New("OldMAC is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMAC requires an ID field in the mutation") + return v, errors.New("OldMAC requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3364,10 +3422,10 @@ func (m *FieldTypeMutation) StringArray() (r schema.Strings, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStringArray(ctx context.Context) (v schema.Strings, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStringArray is only allowed on UpdateOne operations") + return v, errors.New("OldStringArray is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStringArray requires an ID field in the mutation") + return v, errors.New("OldStringArray requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3413,10 +3471,10 @@ func (m *FieldTypeMutation) Password() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPassword(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPassword is only allowed on UpdateOne operations") + return v, errors.New("OldPassword is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPassword requires an ID field in the mutation") + return v, errors.New("OldPassword requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3462,10 +3520,10 @@ func (m *FieldTypeMutation) StringScanner() (r schema.StringScanner, exists bool // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStringScanner(ctx context.Context) (v *schema.StringScanner, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStringScanner is only allowed on UpdateOne operations") + return v, errors.New("OldStringScanner is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStringScanner requires an ID field in the mutation") + return v, errors.New("OldStringScanner requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3512,10 +3570,10 @@ func (m *FieldTypeMutation) Duration() (r time.Duration, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDuration(ctx context.Context) (v time.Duration, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDuration is only allowed on UpdateOne operations") + return v, errors.New("OldDuration is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDuration requires an ID field in the mutation") + return v, errors.New("OldDuration requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3581,10 +3639,10 @@ func (m *FieldTypeMutation) Dir() (r http.Dir, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDir(ctx context.Context) (v http.Dir, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDir is only allowed on UpdateOne operations") + return v, errors.New("OldDir is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDir requires an ID field in the mutation") + return v, errors.New("OldDir requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3617,10 +3675,10 @@ func (m *FieldTypeMutation) Ndir() (r http.Dir, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNdir(ctx context.Context) (v *http.Dir, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNdir is only allowed on UpdateOne operations") + return v, errors.New("OldNdir is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNdir requires an ID field in the mutation") + return v, errors.New("OldNdir requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3666,10 +3724,10 @@ func (m *FieldTypeMutation) Str() (r sql.NullString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStr(ctx context.Context) (v sql.NullString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStr is only allowed on UpdateOne operations") + return v, errors.New("OldStr is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStr requires an ID field in the mutation") + return v, errors.New("OldStr requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3715,10 +3773,10 @@ func (m *FieldTypeMutation) NullStr() (r *sql.NullString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullStr(ctx context.Context) (v *sql.NullString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullStr is only allowed on UpdateOne operations") + return v, errors.New("OldNullStr is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullStr requires an ID field in the mutation") + return v, errors.New("OldNullStr requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3764,10 +3822,10 @@ func (m *FieldTypeMutation) Link() (r schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLink(ctx context.Context) (v schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLink is only allowed on UpdateOne operations") + return v, errors.New("OldLink is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLink requires an ID field in the mutation") + return v, errors.New("OldLink requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3813,10 +3871,10 @@ func (m *FieldTypeMutation) NullLink() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullLink(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullLink is only allowed on UpdateOne operations") + return v, errors.New("OldNullLink is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullLink requires an ID field in the mutation") + return v, errors.New("OldNullLink requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3862,10 +3920,10 @@ func (m *FieldTypeMutation) Active() (r schema.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldActive(ctx context.Context) (v schema.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldActive is only allowed on UpdateOne operations") + return v, errors.New("OldActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldActive requires an ID field in the mutation") + return v, errors.New("OldActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3911,10 +3969,10 @@ func (m *FieldTypeMutation) NullActive() (r schema.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullActive(ctx context.Context) (v *schema.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullActive is only allowed on UpdateOne operations") + return v, errors.New("OldNullActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullActive requires an ID field in the mutation") + return v, errors.New("OldNullActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3960,10 +4018,10 @@ func (m *FieldTypeMutation) Deleted() (r *sql.NullBool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDeleted(ctx context.Context) (v *sql.NullBool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDeleted is only allowed on UpdateOne operations") + return v, errors.New("OldDeleted is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDeleted requires an ID field in the mutation") + return v, errors.New("OldDeleted requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4009,10 +4067,10 @@ func (m *FieldTypeMutation) DeletedAt() (r *sql.NullTime, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDeletedAt(ctx context.Context) (v *sql.NullTime, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDeletedAt is only allowed on UpdateOne operations") + return v, errors.New("OldDeletedAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDeletedAt requires an ID field in the mutation") + return v, errors.New("OldDeletedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4058,10 +4116,10 @@ func (m *FieldTypeMutation) RawData() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldRawData(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRawData is only allowed on UpdateOne operations") + return v, errors.New("OldRawData is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRawData requires an ID field in the mutation") + return v, errors.New("OldRawData requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4107,10 +4165,10 @@ func (m *FieldTypeMutation) Sensitive() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSensitive(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSensitive is only allowed on UpdateOne operations") + return v, errors.New("OldSensitive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSensitive requires an ID field in the mutation") + return v, errors.New("OldSensitive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4156,10 +4214,10 @@ func (m *FieldTypeMutation) IP() (r net.IP, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldIP(ctx context.Context) (v net.IP, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldIP is only allowed on UpdateOne operations") + return v, errors.New("OldIP is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldIP requires an ID field in the mutation") + return v, errors.New("OldIP requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4205,10 +4263,10 @@ func (m *FieldTypeMutation) NullInt64() (r *sql.NullInt64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullInt64(ctx context.Context) (v *sql.NullInt64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldNullInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullInt64 requires an ID field in the mutation") + return v, errors.New("OldNullInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4255,10 +4313,10 @@ func (m *FieldTypeMutation) SchemaInt() (r schema.Int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt(ctx context.Context) (v schema.Int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt requires an ID field in the mutation") + return v, errors.New("OldSchemaInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4325,10 +4383,10 @@ func (m *FieldTypeMutation) SchemaInt8() (r schema.Int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt8(ctx context.Context) (v schema.Int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt8 requires an ID field in the mutation") + return v, errors.New("OldSchemaInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4395,10 +4453,10 @@ func (m *FieldTypeMutation) SchemaInt64() (r schema.Int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt64(ctx context.Context) (v schema.Int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt64 requires an ID field in the mutation") + return v, errors.New("OldSchemaInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4465,10 +4523,10 @@ func (m *FieldTypeMutation) SchemaFloat() (r schema.Float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaFloat(ctx context.Context) (v schema.Float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaFloat is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaFloat requires an ID field in the mutation") + return v, errors.New("OldSchemaFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4535,10 +4593,10 @@ func (m *FieldTypeMutation) SchemaFloat32() (r schema.Float32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaFloat32(ctx context.Context) (v schema.Float32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaFloat32 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaFloat32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaFloat32 requires an ID field in the mutation") + return v, errors.New("OldSchemaFloat32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4604,10 +4662,10 @@ func (m *FieldTypeMutation) NullFloat() (r *sql.NullFloat64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullFloat(ctx context.Context) (v *sql.NullFloat64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullFloat is only allowed on UpdateOne operations") + return v, errors.New("OldNullFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullFloat requires an ID field in the mutation") + return v, errors.New("OldNullFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4653,10 +4711,10 @@ func (m *FieldTypeMutation) Role() (r role.Role, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldRole(ctx context.Context) (v role.Role, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRole is only allowed on UpdateOne operations") + return v, errors.New("OldRole is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRole requires an ID field in the mutation") + return v, errors.New("OldRole requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4689,10 +4747,10 @@ func (m *FieldTypeMutation) Priority() (r role.Priority, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPriority(ctx context.Context) (v role.Priority, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPriority is only allowed on UpdateOne operations") + return v, errors.New("OldPriority is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPriority requires an ID field in the mutation") + return v, errors.New("OldPriority requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4738,10 +4796,10 @@ func (m *FieldTypeMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4787,10 +4845,10 @@ func (m *FieldTypeMutation) NillableUUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableUUID(ctx context.Context) (v *uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableUUID is only allowed on UpdateOne operations") + return v, errors.New("OldNillableUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableUUID requires an ID field in the mutation") + return v, errors.New("OldNillableUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4836,10 +4894,10 @@ func (m *FieldTypeMutation) Strings() (r []string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) 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 { @@ -4885,10 +4943,10 @@ func (m *FieldTypeMutation) Pair() (r schema.Pair, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPair(ctx context.Context) (v schema.Pair, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPair is only allowed on UpdateOne operations") + return v, errors.New("OldPair is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPair requires an ID field in the mutation") + return v, errors.New("OldPair requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4921,10 +4979,10 @@ func (m *FieldTypeMutation) NilPair() (r *schema.Pair, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNilPair(ctx context.Context) (v *schema.Pair, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNilPair is only allowed on UpdateOne operations") + return v, errors.New("OldNilPair is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNilPair requires an ID field in the mutation") + return v, errors.New("OldNilPair requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4970,10 +5028,10 @@ func (m *FieldTypeMutation) Vstring() (r schema.VString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldVstring(ctx context.Context) (v schema.VString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldVstring is only allowed on UpdateOne operations") + return v, errors.New("OldVstring is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldVstring requires an ID field in the mutation") + return v, errors.New("OldVstring requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5006,10 +5064,10 @@ func (m *FieldTypeMutation) Triple() (r schema.Triple, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldTriple(ctx context.Context) (v schema.Triple, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldTriple is only allowed on UpdateOne operations") + return v, errors.New("OldTriple is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldTriple requires an ID field in the mutation") + return v, errors.New("OldTriple requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5043,10 +5101,10 @@ func (m *FieldTypeMutation) BigInt() (r schema.BigInt, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldBigInt(ctx context.Context) (v schema.BigInt, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBigInt is only allowed on UpdateOne operations") + return v, errors.New("OldBigInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBigInt requires an ID field in the mutation") + return v, errors.New("OldBigInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5112,10 +5170,10 @@ func (m *FieldTypeMutation) PasswordOther() (r schema.Password, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPasswordOther(ctx context.Context) (v schema.Password, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPasswordOther is only allowed on UpdateOne operations") + return v, errors.New("OldPasswordOther is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPasswordOther requires an ID field in the mutation") + return v, errors.New("OldPasswordOther requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7134,7 +7192,7 @@ func withFileID(id int) fileOption { m.oldValue = func(ctx context.Context) (*File, 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().File.Get(ctx, id) } @@ -7167,7 +7225,7 @@ func (m FileMutation) Client() *Client { // it returns an error otherwise. func (m FileMutation) 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() @@ -7183,6 +7241,25 @@ func (m *FileMutation) 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 *FileMutation) 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().File.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetSize sets the "size" field. func (m *FileMutation) SetSize(i int) { m.size = &i @@ -7203,10 +7280,10 @@ func (m *FileMutation) Size() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldSize(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSize is only allowed on UpdateOne operations") + return v, errors.New("OldSize is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSize requires an ID field in the mutation") + return v, errors.New("OldSize requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7258,10 +7335,10 @@ func (m *FileMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7294,10 +7371,10 @@ func (m *FileMutation) User() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldUser(ctx context.Context) (v *string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUser is only allowed on UpdateOne operations") + return v, errors.New("OldUser is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUser requires an ID field in the mutation") + return v, errors.New("OldUser requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7343,10 +7420,10 @@ func (m *FileMutation) Group() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldGroup(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldGroup is only allowed on UpdateOne operations") + return v, errors.New("OldGroup is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldGroup requires an ID field in the mutation") + return v, errors.New("OldGroup requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7392,10 +7469,10 @@ func (m *FileMutation) GetOp() (r bool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldOp(ctx context.Context) (v bool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOp is only allowed on UpdateOne operations") + return v, errors.New("OldOp is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOp requires an ID field in the mutation") + return v, errors.New("OldOp requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7942,7 +8019,7 @@ func withFileTypeID(id int) filetypeOption { m.oldValue = func(ctx context.Context) (*FileType, 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().FileType.Get(ctx, id) } @@ -7975,7 +8052,7 @@ func (m FileTypeMutation) Client() *Client { // it returns an error otherwise. func (m FileTypeMutation) 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() @@ -7991,6 +8068,25 @@ func (m *FileTypeMutation) 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 *FileTypeMutation) 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().FileType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *FileTypeMutation) SetName(s string) { m.name = &s @@ -8010,10 +8106,10 @@ func (m *FileTypeMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8046,10 +8142,10 @@ func (m *FileTypeMutation) GetType() (r filetype.Type, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldType(ctx context.Context) (v filetype.Type, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldType is only allowed on UpdateOne operations") + return v, errors.New("OldType is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldType requires an ID field in the mutation") + return v, errors.New("OldType requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8082,10 +8178,10 @@ func (m *FileTypeMutation) State() (r filetype.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldState(ctx context.Context) (v filetype.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8429,7 +8525,7 @@ func withGoodsID(id int) goodsOption { m.oldValue = func(ctx context.Context) (*Goods, 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().Goods.Get(ctx, id) } @@ -8462,7 +8558,7 @@ func (m GoodsMutation) Client() *Client { // it returns an error otherwise. func (m GoodsMutation) 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() @@ -8478,6 +8574,25 @@ func (m *GoodsMutation) 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 *GoodsMutation) 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().Goods.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // Where appends a list predicates to the GoodsMutation builder. func (m *GoodsMutation) Where(ps ...predicate.Goods) { m.predicates = append(m.predicates, ps...) @@ -8676,7 +8791,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -8709,7 +8824,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -8725,6 +8840,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetActive sets the "active" field. func (m *GroupMutation) SetActive(b bool) { m.active = &b @@ -8744,10 +8878,10 @@ func (m *GroupMutation) Active() (r bool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldActive(ctx context.Context) (v bool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldActive is only allowed on UpdateOne operations") + return v, errors.New("OldActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldActive requires an ID field in the mutation") + return v, errors.New("OldActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8780,10 +8914,10 @@ func (m *GroupMutation) Expire() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldExpire(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldExpire is only allowed on UpdateOne operations") + return v, errors.New("OldExpire is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldExpire requires an ID field in the mutation") + return v, errors.New("OldExpire requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8816,10 +8950,10 @@ func (m *GroupMutation) GetType() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldType(ctx context.Context) (v *string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldType is only allowed on UpdateOne operations") + return v, errors.New("OldType is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldType requires an ID field in the mutation") + return v, errors.New("OldType requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8866,10 +9000,10 @@ func (m *GroupMutation) MaxUsers() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldMaxUsers(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMaxUsers is only allowed on UpdateOne operations") + return v, errors.New("OldMaxUsers is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMaxUsers requires an ID field in the mutation") + return v, errors.New("OldMaxUsers requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8935,10 +9069,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -9569,7 +9703,7 @@ func withGroupInfoID(id int) groupinfoOption { m.oldValue = func(ctx context.Context) (*GroupInfo, 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().GroupInfo.Get(ctx, id) } @@ -9602,7 +9736,7 @@ func (m GroupInfoMutation) Client() *Client { // it returns an error otherwise. func (m GroupInfoMutation) 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() @@ -9618,6 +9752,25 @@ func (m *GroupInfoMutation) 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 *GroupInfoMutation) 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().GroupInfo.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetDesc sets the "desc" field. func (m *GroupInfoMutation) SetDesc(s string) { m.desc = &s @@ -9637,10 +9790,10 @@ func (m *GroupInfoMutation) Desc() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupInfoMutation) OldDesc(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDesc is only allowed on UpdateOne operations") + return v, errors.New("OldDesc is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDesc requires an ID field in the mutation") + return v, errors.New("OldDesc requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -9674,10 +9827,10 @@ func (m *GroupInfoMutation) MaxUsers() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupInfoMutation) OldMaxUsers(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMaxUsers is only allowed on UpdateOne operations") + return v, errors.New("OldMaxUsers is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMaxUsers requires an ID field in the mutation") + return v, errors.New("OldMaxUsers requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10039,7 +10192,7 @@ func withItemID(id string) itemOption { m.oldValue = func(ctx context.Context) (*Item, 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().Item.Get(ctx, id) } @@ -10072,7 +10225,7 @@ func (m ItemMutation) Client() *Client { // it returns an error otherwise. func (m ItemMutation) 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() @@ -10094,6 +10247,25 @@ func (m *ItemMutation) ID() (id string, 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 *ItemMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Item.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *ItemMutation) SetText(s string) { m.text = &s @@ -10113,10 +10285,10 @@ func (m *ItemMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ItemMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10364,7 +10536,7 @@ func withNodeID(id int) nodeOption { m.oldValue = func(ctx context.Context) (*Node, 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().Node.Get(ctx, id) } @@ -10397,7 +10569,7 @@ func (m NodeMutation) Client() *Client { // it returns an error otherwise. func (m NodeMutation) 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() @@ -10413,6 +10585,25 @@ func (m *NodeMutation) 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 *NodeMutation) 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().Node.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetValue sets the "value" field. func (m *NodeMutation) SetValue(i int) { m.value = &i @@ -10433,10 +10624,10 @@ func (m *NodeMutation) Value() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValue is only allowed on UpdateOne operations") + return v, errors.New("OldValue is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValue requires an ID field in the mutation") + return v, errors.New("OldValue requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10846,7 +11037,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -10879,7 +11070,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -10895,6 +11086,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetAge sets the "age" field. func (m *PetMutation) SetAge(f float64) { m.age = &f @@ -10915,10 +11125,10 @@ func (m *PetMutation) Age() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldAge(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10970,10 +11180,10 @@ func (m *PetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11006,10 +11216,10 @@ func (m *PetMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11055,10 +11265,10 @@ func (m *PetMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11499,7 +11709,7 @@ func withSpecID(id int) specOption { m.oldValue = func(ctx context.Context) (*Spec, 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().Spec.Get(ctx, id) } @@ -11532,7 +11742,7 @@ func (m SpecMutation) Client() *Client { // it returns an error otherwise. func (m SpecMutation) 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() @@ -11548,6 +11758,25 @@ func (m *SpecMutation) 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 *SpecMutation) 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().Spec.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // AddCardIDs adds the "card" edge to the Card entity by ids. func (m *SpecMutation) AddCardIDs(ids ...int) { if m.card == nil { @@ -11821,7 +12050,7 @@ func withTaskID(id int) taskOption { m.oldValue = func(ctx context.Context) (*Task, 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().Task.Get(ctx, id) } @@ -11854,7 +12083,7 @@ func (m TaskMutation) Client() *Client { // it returns an error otherwise. func (m TaskMutation) 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() @@ -11870,6 +12099,25 @@ func (m *TaskMutation) 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 *TaskMutation) 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().Task.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetPriority sets the "priority" field. func (m *TaskMutation) SetPriority(s schema.Priority) { m.priority = &s @@ -11890,10 +12138,10 @@ func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPriority is only allowed on UpdateOne operations") + return v, errors.New("OldPriority is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPriority requires an ID field in the mutation") + return v, errors.New("OldPriority requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12189,7 +12437,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) } @@ -12222,7 +12470,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() @@ -12238,6 +12486,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) + } +} + // SetOptionalInt sets the "optional_int" field. func (m *UserMutation) SetOptionalInt(i int) { m.optional_int = &i @@ -12258,10 +12525,10 @@ func (m *UserMutation) OptionalInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldOptionalInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt requires an ID field in the mutation") + return v, errors.New("OldOptionalInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12328,10 +12595,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12383,10 +12650,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12419,10 +12686,10 @@ func (m *UserMutation) Last() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldLast(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLast is only allowed on UpdateOne operations") + return v, errors.New("OldLast is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLast requires an ID field in the mutation") + return v, errors.New("OldLast requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12455,10 +12722,10 @@ func (m *UserMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12504,10 +12771,10 @@ func (m *UserMutation) Address() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAddress(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAddress is only allowed on UpdateOne operations") + return v, errors.New("OldAddress is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAddress requires an ID field in the mutation") + return v, errors.New("OldAddress requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12553,10 +12820,10 @@ func (m *UserMutation) Phone() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldPhone(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPhone is only allowed on UpdateOne operations") + return v, errors.New("OldPhone is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPhone requires an ID field in the mutation") + return v, errors.New("OldPhone requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12602,10 +12869,10 @@ func (m *UserMutation) Password() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPassword is only allowed on UpdateOne operations") + return v, errors.New("OldPassword is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPassword requires an ID field in the mutation") + return v, errors.New("OldPassword requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12651,10 +12918,10 @@ func (m *UserMutation) Role() (r user.Role, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldRole(ctx context.Context) (v user.Role, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRole is only allowed on UpdateOne operations") + return v, errors.New("OldRole is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRole requires an ID field in the mutation") + return v, errors.New("OldRole requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12687,10 +12954,10 @@ func (m *UserMutation) Employment() (r user.Employment, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldEmployment(ctx context.Context) (v user.Employment, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldEmployment is only allowed on UpdateOne operations") + return v, errors.New("OldEmployment is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldEmployment requires an ID field in the mutation") + return v, errors.New("OldEmployment requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12723,10 +12990,10 @@ func (m *UserMutation) SSOCert() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldSSOCert(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSSOCert is only allowed on UpdateOne operations") + return v, errors.New("OldSSOCert is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSSOCert requires an ID field in the mutation") + return v, errors.New("OldSSOCert requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 1c37bb2f0..9a5cc66d3 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -9,6 +9,7 @@ package ent import ( "context" "database/sql" + "errors" "fmt" "net" "net/http" @@ -114,7 +115,7 @@ func withCardID(id string) cardOption { m.oldValue = func(ctx context.Context) (*Card, 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().Card.Get(ctx, id) } @@ -147,7 +148,7 @@ func (m CardMutation) Client() *Client { // it returns an error otherwise. func (m CardMutation) 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() @@ -163,6 +164,25 @@ func (m *CardMutation) ID() (id string, 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 *CardMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Card.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetCreateTime sets the "create_time" field. func (m *CardMutation) SetCreateTime(t time.Time) { m.create_time = &t @@ -182,10 +202,10 @@ func (m *CardMutation) CreateTime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldCreateTime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCreateTime is only allowed on UpdateOne operations") + return v, errors.New("OldCreateTime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCreateTime requires an ID field in the mutation") + return v, errors.New("OldCreateTime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -218,10 +238,10 @@ func (m *CardMutation) UpdateTime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldUpdateTime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUpdateTime is only allowed on UpdateOne operations") + return v, errors.New("OldUpdateTime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUpdateTime requires an ID field in the mutation") + return v, errors.New("OldUpdateTime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -255,10 +275,10 @@ func (m *CardMutation) Balance() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldBalance(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBalance is only allowed on UpdateOne operations") + return v, errors.New("OldBalance is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBalance requires an ID field in the mutation") + return v, errors.New("OldBalance requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -310,10 +330,10 @@ func (m *CardMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -346,10 +366,10 @@ func (m *CardMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -827,7 +847,7 @@ func withCommentID(id string) commentOption { m.oldValue = func(ctx context.Context) (*Comment, 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().Comment.Get(ctx, id) } @@ -860,7 +880,7 @@ func (m CommentMutation) Client() *Client { // it returns an error otherwise. func (m CommentMutation) 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() @@ -876,6 +896,25 @@ func (m *CommentMutation) ID() (id string, 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 *CommentMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Comment.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetUniqueInt sets the "unique_int" field. func (m *CommentMutation) SetUniqueInt(i int) { m.unique_int = &i @@ -896,10 +935,10 @@ func (m *CommentMutation) UniqueInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldUniqueInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUniqueInt is only allowed on UpdateOne operations") + return v, errors.New("OldUniqueInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUniqueInt requires an ID field in the mutation") + return v, errors.New("OldUniqueInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -952,10 +991,10 @@ func (m *CommentMutation) UniqueFloat() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldUniqueFloat(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUniqueFloat is only allowed on UpdateOne operations") + return v, errors.New("OldUniqueFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUniqueFloat requires an ID field in the mutation") + return v, errors.New("OldUniqueFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1008,10 +1047,10 @@ func (m *CommentMutation) NillableInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CommentMutation) OldNillableInt(ctx context.Context) (v *int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt requires an ID field in the mutation") + return v, errors.New("OldNillableInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1441,7 +1480,7 @@ func withFieldTypeID(id string) fieldtypeOption { m.oldValue = func(ctx context.Context) (*FieldType, 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().FieldType.Get(ctx, id) } @@ -1474,7 +1513,7 @@ func (m FieldTypeMutation) Client() *Client { // it returns an error otherwise. func (m FieldTypeMutation) 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() @@ -1490,6 +1529,25 @@ func (m *FieldTypeMutation) ID() (id string, 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 *FieldTypeMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().FieldType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetInt sets the "int" field. func (m *FieldTypeMutation) SetInt(i int) { m.int = &i @@ -1510,10 +1568,10 @@ func (m *FieldTypeMutation) Int() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt is only allowed on UpdateOne operations") + return v, errors.New("OldInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt requires an ID field in the mutation") + return v, errors.New("OldInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1566,10 +1624,10 @@ func (m *FieldTypeMutation) Int8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt8(ctx context.Context) (v int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt8 requires an ID field in the mutation") + return v, errors.New("OldInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1622,10 +1680,10 @@ func (m *FieldTypeMutation) Int16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt16(ctx context.Context) (v int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt16 requires an ID field in the mutation") + return v, errors.New("OldInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1678,10 +1736,10 @@ func (m *FieldTypeMutation) Int32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt32 requires an ID field in the mutation") + return v, errors.New("OldInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1734,10 +1792,10 @@ func (m *FieldTypeMutation) Int64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldInt64(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt64 requires an ID field in the mutation") + return v, errors.New("OldInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1790,10 +1848,10 @@ func (m *FieldTypeMutation) OptionalInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt requires an ID field in the mutation") + return v, errors.New("OldOptionalInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1860,10 +1918,10 @@ func (m *FieldTypeMutation) OptionalInt8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt8(ctx context.Context) (v int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt8 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1930,10 +1988,10 @@ func (m *FieldTypeMutation) OptionalInt16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt16(ctx context.Context) (v int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt16 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2000,10 +2058,10 @@ func (m *FieldTypeMutation) OptionalInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt32 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2070,10 +2128,10 @@ func (m *FieldTypeMutation) OptionalInt64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalInt64(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt64 requires an ID field in the mutation") + return v, errors.New("OldOptionalInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2140,10 +2198,10 @@ func (m *FieldTypeMutation) NillableInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt(ctx context.Context) (v *int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt requires an ID field in the mutation") + return v, errors.New("OldNillableInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2210,10 +2268,10 @@ func (m *FieldTypeMutation) NillableInt8() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt8(ctx context.Context) (v *int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt8 requires an ID field in the mutation") + return v, errors.New("OldNillableInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2280,10 +2338,10 @@ func (m *FieldTypeMutation) NillableInt16() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt16(ctx context.Context) (v *int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt16 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt16 requires an ID field in the mutation") + return v, errors.New("OldNillableInt16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2350,10 +2408,10 @@ func (m *FieldTypeMutation) NillableInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt32(ctx context.Context) (v *int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt32 requires an ID field in the mutation") + return v, errors.New("OldNillableInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2420,10 +2478,10 @@ func (m *FieldTypeMutation) NillableInt64() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableInt64(ctx context.Context) (v *int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldNillableInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableInt64 requires an ID field in the mutation") + return v, errors.New("OldNillableInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2490,10 +2548,10 @@ func (m *FieldTypeMutation) ValidateOptionalInt32() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldValidateOptionalInt32(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValidateOptionalInt32 is only allowed on UpdateOne operations") + return v, errors.New("OldValidateOptionalInt32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValidateOptionalInt32 requires an ID field in the mutation") + return v, errors.New("OldValidateOptionalInt32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2560,10 +2618,10 @@ func (m *FieldTypeMutation) OptionalUint() (r uint, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint(ctx context.Context) (v uint, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint requires an ID field in the mutation") + return v, errors.New("OldOptionalUint requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2630,10 +2688,10 @@ func (m *FieldTypeMutation) OptionalUint8() (r uint8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint8(ctx context.Context) (v uint8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint8 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint8 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2700,10 +2758,10 @@ func (m *FieldTypeMutation) OptionalUint16() (r uint16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint16(ctx context.Context) (v uint16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint16 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint16 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint16 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint16 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2770,10 +2828,10 @@ func (m *FieldTypeMutation) OptionalUint32() (r uint32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint32(ctx context.Context) (v uint32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint32 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2840,10 +2898,10 @@ func (m *FieldTypeMutation) OptionalUint64() (r uint64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalUint64(ctx context.Context) (v uint64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalUint64 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalUint64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalUint64 requires an ID field in the mutation") + return v, errors.New("OldOptionalUint64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2909,10 +2967,10 @@ func (m *FieldTypeMutation) State() (r fieldtype.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldState(ctx context.Context) (v fieldtype.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2959,10 +3017,10 @@ func (m *FieldTypeMutation) OptionalFloat() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalFloat(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalFloat is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalFloat requires an ID field in the mutation") + return v, errors.New("OldOptionalFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3029,10 +3087,10 @@ func (m *FieldTypeMutation) OptionalFloat32() (r float32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldOptionalFloat32(ctx context.Context) (v float32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalFloat32 is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalFloat32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalFloat32 requires an ID field in the mutation") + return v, errors.New("OldOptionalFloat32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3098,10 +3156,10 @@ func (m *FieldTypeMutation) Datetime() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDatetime(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDatetime is only allowed on UpdateOne operations") + return v, errors.New("OldDatetime is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDatetime requires an ID field in the mutation") + return v, errors.New("OldDatetime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3148,10 +3206,10 @@ func (m *FieldTypeMutation) Decimal() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDecimal(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDecimal is only allowed on UpdateOne operations") + return v, errors.New("OldDecimal is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDecimal requires an ID field in the mutation") + return v, errors.New("OldDecimal requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3217,10 +3275,10 @@ func (m *FieldTypeMutation) LinkOther() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLinkOther(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLinkOther is only allowed on UpdateOne operations") + return v, errors.New("OldLinkOther is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLinkOther requires an ID field in the mutation") + return v, errors.New("OldLinkOther requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3266,10 +3324,10 @@ func (m *FieldTypeMutation) LinkOtherFunc() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLinkOtherFunc(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLinkOtherFunc is only allowed on UpdateOne operations") + return v, errors.New("OldLinkOtherFunc is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLinkOtherFunc requires an ID field in the mutation") + return v, errors.New("OldLinkOtherFunc requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3315,10 +3373,10 @@ func (m *FieldTypeMutation) MAC() (r schema.MAC, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldMAC(ctx context.Context) (v schema.MAC, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMAC is only allowed on UpdateOne operations") + return v, errors.New("OldMAC is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMAC requires an ID field in the mutation") + return v, errors.New("OldMAC requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3364,10 +3422,10 @@ func (m *FieldTypeMutation) StringArray() (r schema.Strings, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStringArray(ctx context.Context) (v schema.Strings, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStringArray is only allowed on UpdateOne operations") + return v, errors.New("OldStringArray is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStringArray requires an ID field in the mutation") + return v, errors.New("OldStringArray requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3413,10 +3471,10 @@ func (m *FieldTypeMutation) Password() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPassword(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPassword is only allowed on UpdateOne operations") + return v, errors.New("OldPassword is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPassword requires an ID field in the mutation") + return v, errors.New("OldPassword requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3462,10 +3520,10 @@ func (m *FieldTypeMutation) StringScanner() (r schema.StringScanner, exists bool // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStringScanner(ctx context.Context) (v *schema.StringScanner, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStringScanner is only allowed on UpdateOne operations") + return v, errors.New("OldStringScanner is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStringScanner requires an ID field in the mutation") + return v, errors.New("OldStringScanner requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3512,10 +3570,10 @@ func (m *FieldTypeMutation) Duration() (r time.Duration, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDuration(ctx context.Context) (v time.Duration, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDuration is only allowed on UpdateOne operations") + return v, errors.New("OldDuration is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDuration requires an ID field in the mutation") + return v, errors.New("OldDuration requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3581,10 +3639,10 @@ func (m *FieldTypeMutation) Dir() (r http.Dir, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDir(ctx context.Context) (v http.Dir, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDir is only allowed on UpdateOne operations") + return v, errors.New("OldDir is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDir requires an ID field in the mutation") + return v, errors.New("OldDir requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3617,10 +3675,10 @@ func (m *FieldTypeMutation) Ndir() (r http.Dir, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNdir(ctx context.Context) (v *http.Dir, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNdir is only allowed on UpdateOne operations") + return v, errors.New("OldNdir is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNdir requires an ID field in the mutation") + return v, errors.New("OldNdir requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3666,10 +3724,10 @@ func (m *FieldTypeMutation) Str() (r sql.NullString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldStr(ctx context.Context) (v sql.NullString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStr is only allowed on UpdateOne operations") + return v, errors.New("OldStr is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStr requires an ID field in the mutation") + return v, errors.New("OldStr requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3715,10 +3773,10 @@ func (m *FieldTypeMutation) NullStr() (r *sql.NullString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullStr(ctx context.Context) (v *sql.NullString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullStr is only allowed on UpdateOne operations") + return v, errors.New("OldNullStr is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullStr requires an ID field in the mutation") + return v, errors.New("OldNullStr requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3764,10 +3822,10 @@ func (m *FieldTypeMutation) Link() (r schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldLink(ctx context.Context) (v schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLink is only allowed on UpdateOne operations") + return v, errors.New("OldLink is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLink requires an ID field in the mutation") + return v, errors.New("OldLink requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3813,10 +3871,10 @@ func (m *FieldTypeMutation) NullLink() (r *schema.Link, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullLink(ctx context.Context) (v *schema.Link, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullLink is only allowed on UpdateOne operations") + return v, errors.New("OldNullLink is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullLink requires an ID field in the mutation") + return v, errors.New("OldNullLink requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3862,10 +3920,10 @@ func (m *FieldTypeMutation) Active() (r schema.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldActive(ctx context.Context) (v schema.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldActive is only allowed on UpdateOne operations") + return v, errors.New("OldActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldActive requires an ID field in the mutation") + return v, errors.New("OldActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3911,10 +3969,10 @@ func (m *FieldTypeMutation) NullActive() (r schema.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullActive(ctx context.Context) (v *schema.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullActive is only allowed on UpdateOne operations") + return v, errors.New("OldNullActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullActive requires an ID field in the mutation") + return v, errors.New("OldNullActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3960,10 +4018,10 @@ func (m *FieldTypeMutation) Deleted() (r *sql.NullBool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDeleted(ctx context.Context) (v *sql.NullBool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDeleted is only allowed on UpdateOne operations") + return v, errors.New("OldDeleted is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDeleted requires an ID field in the mutation") + return v, errors.New("OldDeleted requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4009,10 +4067,10 @@ func (m *FieldTypeMutation) DeletedAt() (r *sql.NullTime, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldDeletedAt(ctx context.Context) (v *sql.NullTime, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDeletedAt is only allowed on UpdateOne operations") + return v, errors.New("OldDeletedAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDeletedAt requires an ID field in the mutation") + return v, errors.New("OldDeletedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4058,10 +4116,10 @@ func (m *FieldTypeMutation) RawData() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldRawData(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRawData is only allowed on UpdateOne operations") + return v, errors.New("OldRawData is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRawData requires an ID field in the mutation") + return v, errors.New("OldRawData requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4107,10 +4165,10 @@ func (m *FieldTypeMutation) Sensitive() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSensitive(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSensitive is only allowed on UpdateOne operations") + return v, errors.New("OldSensitive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSensitive requires an ID field in the mutation") + return v, errors.New("OldSensitive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4156,10 +4214,10 @@ func (m *FieldTypeMutation) IP() (r net.IP, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldIP(ctx context.Context) (v net.IP, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldIP is only allowed on UpdateOne operations") + return v, errors.New("OldIP is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldIP requires an ID field in the mutation") + return v, errors.New("OldIP requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4205,10 +4263,10 @@ func (m *FieldTypeMutation) NullInt64() (r *sql.NullInt64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullInt64(ctx context.Context) (v *sql.NullInt64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldNullInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullInt64 requires an ID field in the mutation") + return v, errors.New("OldNullInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4255,10 +4313,10 @@ func (m *FieldTypeMutation) SchemaInt() (r schema.Int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt(ctx context.Context) (v schema.Int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt requires an ID field in the mutation") + return v, errors.New("OldSchemaInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4325,10 +4383,10 @@ func (m *FieldTypeMutation) SchemaInt8() (r schema.Int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt8(ctx context.Context) (v schema.Int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt8 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt8 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt8 requires an ID field in the mutation") + return v, errors.New("OldSchemaInt8 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4395,10 +4453,10 @@ func (m *FieldTypeMutation) SchemaInt64() (r schema.Int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaInt64(ctx context.Context) (v schema.Int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaInt64 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaInt64 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaInt64 requires an ID field in the mutation") + return v, errors.New("OldSchemaInt64 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4465,10 +4523,10 @@ func (m *FieldTypeMutation) SchemaFloat() (r schema.Float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaFloat(ctx context.Context) (v schema.Float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaFloat is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaFloat requires an ID field in the mutation") + return v, errors.New("OldSchemaFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4535,10 +4593,10 @@ func (m *FieldTypeMutation) SchemaFloat32() (r schema.Float32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldSchemaFloat32(ctx context.Context) (v schema.Float32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSchemaFloat32 is only allowed on UpdateOne operations") + return v, errors.New("OldSchemaFloat32 is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSchemaFloat32 requires an ID field in the mutation") + return v, errors.New("OldSchemaFloat32 requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4604,10 +4662,10 @@ func (m *FieldTypeMutation) NullFloat() (r *sql.NullFloat64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNullFloat(ctx context.Context) (v *sql.NullFloat64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNullFloat is only allowed on UpdateOne operations") + return v, errors.New("OldNullFloat is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNullFloat requires an ID field in the mutation") + return v, errors.New("OldNullFloat requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4653,10 +4711,10 @@ func (m *FieldTypeMutation) Role() (r role.Role, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldRole(ctx context.Context) (v role.Role, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRole is only allowed on UpdateOne operations") + return v, errors.New("OldRole is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRole requires an ID field in the mutation") + return v, errors.New("OldRole requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4689,10 +4747,10 @@ func (m *FieldTypeMutation) Priority() (r role.Priority, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPriority(ctx context.Context) (v role.Priority, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPriority is only allowed on UpdateOne operations") + return v, errors.New("OldPriority is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPriority requires an ID field in the mutation") + return v, errors.New("OldPriority requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4738,10 +4796,10 @@ func (m *FieldTypeMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4787,10 +4845,10 @@ func (m *FieldTypeMutation) NillableUUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNillableUUID(ctx context.Context) (v *uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNillableUUID is only allowed on UpdateOne operations") + return v, errors.New("OldNillableUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNillableUUID requires an ID field in the mutation") + return v, errors.New("OldNillableUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4836,10 +4894,10 @@ func (m *FieldTypeMutation) Strings() (r []string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) 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 { @@ -4885,10 +4943,10 @@ func (m *FieldTypeMutation) Pair() (r schema.Pair, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPair(ctx context.Context) (v schema.Pair, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPair is only allowed on UpdateOne operations") + return v, errors.New("OldPair is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPair requires an ID field in the mutation") + return v, errors.New("OldPair requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4921,10 +4979,10 @@ func (m *FieldTypeMutation) NilPair() (r *schema.Pair, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldNilPair(ctx context.Context) (v *schema.Pair, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNilPair is only allowed on UpdateOne operations") + return v, errors.New("OldNilPair is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNilPair requires an ID field in the mutation") + return v, errors.New("OldNilPair requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -4970,10 +5028,10 @@ func (m *FieldTypeMutation) Vstring() (r schema.VString, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldVstring(ctx context.Context) (v schema.VString, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldVstring is only allowed on UpdateOne operations") + return v, errors.New("OldVstring is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldVstring requires an ID field in the mutation") + return v, errors.New("OldVstring requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5006,10 +5064,10 @@ func (m *FieldTypeMutation) Triple() (r schema.Triple, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldTriple(ctx context.Context) (v schema.Triple, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldTriple is only allowed on UpdateOne operations") + return v, errors.New("OldTriple is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldTriple requires an ID field in the mutation") + return v, errors.New("OldTriple requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5043,10 +5101,10 @@ func (m *FieldTypeMutation) BigInt() (r schema.BigInt, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldBigInt(ctx context.Context) (v schema.BigInt, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBigInt is only allowed on UpdateOne operations") + return v, errors.New("OldBigInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBigInt requires an ID field in the mutation") + return v, errors.New("OldBigInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -5112,10 +5170,10 @@ func (m *FieldTypeMutation) PasswordOther() (r schema.Password, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FieldTypeMutation) OldPasswordOther(ctx context.Context) (v schema.Password, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPasswordOther is only allowed on UpdateOne operations") + return v, errors.New("OldPasswordOther is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPasswordOther requires an ID field in the mutation") + return v, errors.New("OldPasswordOther requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7134,7 +7192,7 @@ func withFileID(id string) fileOption { m.oldValue = func(ctx context.Context) (*File, 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().File.Get(ctx, id) } @@ -7167,7 +7225,7 @@ func (m FileMutation) Client() *Client { // it returns an error otherwise. func (m FileMutation) 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() @@ -7183,6 +7241,25 @@ func (m *FileMutation) ID() (id string, 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 *FileMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().File.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetSize sets the "size" field. func (m *FileMutation) SetSize(i int) { m.size = &i @@ -7203,10 +7280,10 @@ func (m *FileMutation) Size() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldSize(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSize is only allowed on UpdateOne operations") + return v, errors.New("OldSize is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSize requires an ID field in the mutation") + return v, errors.New("OldSize requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7258,10 +7335,10 @@ func (m *FileMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7294,10 +7371,10 @@ func (m *FileMutation) User() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldUser(ctx context.Context) (v *string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUser is only allowed on UpdateOne operations") + return v, errors.New("OldUser is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUser requires an ID field in the mutation") + return v, errors.New("OldUser requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7343,10 +7420,10 @@ func (m *FileMutation) Group() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldGroup(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldGroup is only allowed on UpdateOne operations") + return v, errors.New("OldGroup is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldGroup requires an ID field in the mutation") + return v, errors.New("OldGroup requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7392,10 +7469,10 @@ func (m *FileMutation) GetOp() (r bool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldOp(ctx context.Context) (v bool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOp is only allowed on UpdateOne operations") + return v, errors.New("OldOp is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOp requires an ID field in the mutation") + return v, errors.New("OldOp requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -7942,7 +8019,7 @@ func withFileTypeID(id string) filetypeOption { m.oldValue = func(ctx context.Context) (*FileType, 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().FileType.Get(ctx, id) } @@ -7975,7 +8052,7 @@ func (m FileTypeMutation) Client() *Client { // it returns an error otherwise. func (m FileTypeMutation) 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() @@ -7991,6 +8068,25 @@ func (m *FileTypeMutation) ID() (id string, 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 *FileTypeMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().FileType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *FileTypeMutation) SetName(s string) { m.name = &s @@ -8010,10 +8106,10 @@ func (m *FileTypeMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8046,10 +8142,10 @@ func (m *FileTypeMutation) GetType() (r filetype.Type, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldType(ctx context.Context) (v filetype.Type, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldType is only allowed on UpdateOne operations") + return v, errors.New("OldType is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldType requires an ID field in the mutation") + return v, errors.New("OldType requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8082,10 +8178,10 @@ func (m *FileTypeMutation) State() (r filetype.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileTypeMutation) OldState(ctx context.Context) (v filetype.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8429,7 +8525,7 @@ func withGoodsID(id string) goodsOption { m.oldValue = func(ctx context.Context) (*Goods, 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().Goods.Get(ctx, id) } @@ -8462,7 +8558,7 @@ func (m GoodsMutation) Client() *Client { // it returns an error otherwise. func (m GoodsMutation) 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() @@ -8478,6 +8574,25 @@ func (m *GoodsMutation) ID() (id string, 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 *GoodsMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Goods.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // Where appends a list predicates to the GoodsMutation builder. func (m *GoodsMutation) Where(ps ...predicate.Goods) { m.predicates = append(m.predicates, ps...) @@ -8676,7 +8791,7 @@ func withGroupID(id string) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -8709,7 +8824,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -8725,6 +8840,25 @@ func (m *GroupMutation) ID() (id string, 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 *GroupMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetActive sets the "active" field. func (m *GroupMutation) SetActive(b bool) { m.active = &b @@ -8744,10 +8878,10 @@ func (m *GroupMutation) Active() (r bool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldActive(ctx context.Context) (v bool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldActive is only allowed on UpdateOne operations") + return v, errors.New("OldActive is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldActive requires an ID field in the mutation") + return v, errors.New("OldActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8780,10 +8914,10 @@ func (m *GroupMutation) Expire() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldExpire(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldExpire is only allowed on UpdateOne operations") + return v, errors.New("OldExpire is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldExpire requires an ID field in the mutation") + return v, errors.New("OldExpire requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8816,10 +8950,10 @@ func (m *GroupMutation) GetType() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldType(ctx context.Context) (v *string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldType is only allowed on UpdateOne operations") + return v, errors.New("OldType is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldType requires an ID field in the mutation") + return v, errors.New("OldType requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8866,10 +9000,10 @@ func (m *GroupMutation) MaxUsers() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldMaxUsers(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMaxUsers is only allowed on UpdateOne operations") + return v, errors.New("OldMaxUsers is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMaxUsers requires an ID field in the mutation") + return v, errors.New("OldMaxUsers requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -8935,10 +9069,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -9569,7 +9703,7 @@ func withGroupInfoID(id string) groupinfoOption { m.oldValue = func(ctx context.Context) (*GroupInfo, 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().GroupInfo.Get(ctx, id) } @@ -9602,7 +9736,7 @@ func (m GroupInfoMutation) Client() *Client { // it returns an error otherwise. func (m GroupInfoMutation) 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() @@ -9618,6 +9752,25 @@ func (m *GroupInfoMutation) ID() (id string, 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 *GroupInfoMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().GroupInfo.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetDesc sets the "desc" field. func (m *GroupInfoMutation) SetDesc(s string) { m.desc = &s @@ -9637,10 +9790,10 @@ func (m *GroupInfoMutation) Desc() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupInfoMutation) OldDesc(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDesc is only allowed on UpdateOne operations") + return v, errors.New("OldDesc is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDesc requires an ID field in the mutation") + return v, errors.New("OldDesc requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -9674,10 +9827,10 @@ func (m *GroupInfoMutation) MaxUsers() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupInfoMutation) OldMaxUsers(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMaxUsers is only allowed on UpdateOne operations") + return v, errors.New("OldMaxUsers is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMaxUsers requires an ID field in the mutation") + return v, errors.New("OldMaxUsers requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10039,7 +10192,7 @@ func withItemID(id string) itemOption { m.oldValue = func(ctx context.Context) (*Item, 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().Item.Get(ctx, id) } @@ -10072,7 +10225,7 @@ func (m ItemMutation) Client() *Client { // it returns an error otherwise. func (m ItemMutation) 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() @@ -10094,6 +10247,25 @@ func (m *ItemMutation) ID() (id string, 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 *ItemMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Item.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetText sets the "text" field. func (m *ItemMutation) SetText(s string) { m.text = &s @@ -10113,10 +10285,10 @@ func (m *ItemMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ItemMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10364,7 +10536,7 @@ func withNodeID(id string) nodeOption { m.oldValue = func(ctx context.Context) (*Node, 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().Node.Get(ctx, id) } @@ -10397,7 +10569,7 @@ func (m NodeMutation) Client() *Client { // it returns an error otherwise. func (m NodeMutation) 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() @@ -10413,6 +10585,25 @@ func (m *NodeMutation) ID() (id string, 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 *NodeMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Node.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetValue sets the "value" field. func (m *NodeMutation) SetValue(i int) { m.value = &i @@ -10433,10 +10624,10 @@ func (m *NodeMutation) Value() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValue is only allowed on UpdateOne operations") + return v, errors.New("OldValue is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValue requires an ID field in the mutation") + return v, errors.New("OldValue requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10846,7 +11037,7 @@ func withPetID(id string) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -10879,7 +11070,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -10895,6 +11086,25 @@ func (m *PetMutation) ID() (id string, 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 *PetMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetAge sets the "age" field. func (m *PetMutation) SetAge(f float64) { m.age = &f @@ -10915,10 +11125,10 @@ func (m *PetMutation) Age() (r float64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldAge(ctx context.Context) (v float64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -10970,10 +11180,10 @@ func (m *PetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11006,10 +11216,10 @@ func (m *PetMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11055,10 +11265,10 @@ func (m *PetMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -11499,7 +11709,7 @@ func withSpecID(id string) specOption { m.oldValue = func(ctx context.Context) (*Spec, 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().Spec.Get(ctx, id) } @@ -11532,7 +11742,7 @@ func (m SpecMutation) Client() *Client { // it returns an error otherwise. func (m SpecMutation) 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() @@ -11548,6 +11758,25 @@ func (m *SpecMutation) ID() (id string, 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 *SpecMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Spec.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // AddCardIDs adds the "card" edge to the Card entity by ids. func (m *SpecMutation) AddCardIDs(ids ...string) { if m.card == nil { @@ -11821,7 +12050,7 @@ func withTaskID(id string) taskOption { m.oldValue = func(ctx context.Context) (*Task, 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().Task.Get(ctx, id) } @@ -11854,7 +12083,7 @@ func (m TaskMutation) Client() *Client { // it returns an error otherwise. func (m TaskMutation) 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() @@ -11870,6 +12099,25 @@ func (m *TaskMutation) ID() (id string, 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 *TaskMutation) IDs(ctx context.Context) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Task.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetPriority sets the "priority" field. func (m *TaskMutation) SetPriority(s schema.Priority) { m.priority = &s @@ -11890,10 +12138,10 @@ func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPriority is only allowed on UpdateOne operations") + return v, errors.New("OldPriority is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPriority requires an ID field in the mutation") + return v, errors.New("OldPriority requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12189,7 +12437,7 @@ func withUserID(id string) 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) } @@ -12222,7 +12470,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() @@ -12238,6 +12486,25 @@ func (m *UserMutation) ID() (id string, 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) ([]string, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []string{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) + } +} + // SetOptionalInt sets the "optional_int" field. func (m *UserMutation) SetOptionalInt(i int) { m.optional_int = &i @@ -12258,10 +12525,10 @@ func (m *UserMutation) OptionalInt() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldOptionalInt(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOptionalInt is only allowed on UpdateOne operations") + return v, errors.New("OldOptionalInt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOptionalInt requires an ID field in the mutation") + return v, errors.New("OldOptionalInt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12328,10 +12595,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12383,10 +12650,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12419,10 +12686,10 @@ func (m *UserMutation) Last() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldLast(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLast is only allowed on UpdateOne operations") + return v, errors.New("OldLast is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLast requires an ID field in the mutation") + return v, errors.New("OldLast requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12455,10 +12722,10 @@ func (m *UserMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12504,10 +12771,10 @@ func (m *UserMutation) Address() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAddress(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAddress is only allowed on UpdateOne operations") + return v, errors.New("OldAddress is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAddress requires an ID field in the mutation") + return v, errors.New("OldAddress requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12553,10 +12820,10 @@ func (m *UserMutation) Phone() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldPhone(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPhone is only allowed on UpdateOne operations") + return v, errors.New("OldPhone is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPhone requires an ID field in the mutation") + return v, errors.New("OldPhone requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12602,10 +12869,10 @@ func (m *UserMutation) Password() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPassword is only allowed on UpdateOne operations") + return v, errors.New("OldPassword is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPassword requires an ID field in the mutation") + return v, errors.New("OldPassword requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12651,10 +12918,10 @@ func (m *UserMutation) Role() (r user.Role, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldRole(ctx context.Context) (v user.Role, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRole is only allowed on UpdateOne operations") + return v, errors.New("OldRole is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRole requires an ID field in the mutation") + return v, errors.New("OldRole requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12687,10 +12954,10 @@ func (m *UserMutation) Employment() (r user.Employment, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldEmployment(ctx context.Context) (v user.Employment, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldEmployment is only allowed on UpdateOne operations") + return v, errors.New("OldEmployment is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldEmployment requires an ID field in the mutation") + return v, errors.New("OldEmployment requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -12723,10 +12990,10 @@ func (m *UserMutation) SSOCert() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldSSOCert(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSSOCert is only allowed on UpdateOne operations") + return v, errors.New("OldSSOCert is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSSOCert requires an ID field in the mutation") + return v, errors.New("OldSSOCert requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/hooks/ent/mutation.go b/entc/integration/hooks/ent/mutation.go index 0b79b74db..6e76a6197 100644 --- a/entc/integration/hooks/ent/mutation.go +++ b/entc/integration/hooks/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" "time" @@ -80,7 +81,7 @@ func withCardID(id int) cardOption { m.oldValue = func(ctx context.Context) (*Card, 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().Card.Get(ctx, id) } @@ -113,7 +114,7 @@ func (m CardMutation) Client() *Client { // it returns an error otherwise. func (m CardMutation) 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() @@ -129,6 +130,25 @@ func (m *CardMutation) 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 *CardMutation) 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().Card.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetNumber sets the "number" field. func (m *CardMutation) SetNumber(s string) { m.number = &s @@ -148,10 +168,10 @@ func (m *CardMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -184,10 +204,10 @@ func (m *CardMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -233,10 +253,10 @@ func (m *CardMutation) CreatedAt() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCreatedAt is only allowed on UpdateOne operations") + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCreatedAt requires an ID field in the mutation") + return v, errors.New("OldCreatedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -269,10 +289,10 @@ func (m *CardMutation) InHook() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldInHook(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInHook is only allowed on UpdateOne operations") + return v, errors.New("OldInHook is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInHook requires an ID field in the mutation") + return v, errors.New("OldInHook requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -632,7 +652,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) } @@ -665,7 +685,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() @@ -681,6 +701,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) + } +} + // SetVersion sets the "version" field. func (m *UserMutation) SetVersion(i int) { m.version = &i @@ -701,10 +740,10 @@ func (m *UserMutation) Version() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldVersion(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldVersion is only allowed on UpdateOne operations") + return v, errors.New("OldVersion is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldVersion requires an ID field in the mutation") + return v, errors.New("OldVersion requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -756,10 +795,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -793,10 +832,10 @@ func (m *UserMutation) Worth() (r uint, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldWorth(ctx context.Context) (v uint, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldWorth is only allowed on UpdateOne operations") + return v, errors.New("OldWorth is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldWorth requires an ID field in the mutation") + return v, errors.New("OldWorth requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/hooks/hooks_test.go b/entc/integration/hooks/hooks_test.go index f7fa2bebd..facba9822 100644 --- a/entc/integration/hooks/hooks_test.go +++ b/entc/integration/hooks/hooks_test.go @@ -152,6 +152,37 @@ func TestDeletion(t *testing.T) { require.Zero(t, client.Card.Query().CountX(ctx)) } +func TestMutationIDs(t *testing.T) { + ctx := context.Background() + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + count := make(map[ent.Op]int) + client.User.Use( + hook.Unless( + func(next ent.Mutator) ent.Mutator { + return hook.UserFunc(func(ctx context.Context, m *ent.UserMutation) (ent.Value, error) { + count[m.Op()]++ + ids, err := m.IDs(ctx) + require.NoError(t, err) + require.Len(t, ids, 1) + require.Equal(t, count[m.Op()], ids[0]) + return next.Mutate(ctx, m) + }) + }, + ent.OpCreate, + ), + ) + for i := 0; i < 5; i++ { + owner := client.User.Create().SetName(fmt.Sprintf("owner-%d", i)).SaveX(ctx) + client.Card.Create().SetNumber(fmt.Sprintf("card-%d", i)).SetOwner(owner).ExecX(ctx) + } + for i := 0; i < 5; i++ { + p := user.And(user.Name(fmt.Sprintf("owner-%d", i)), user.HasCardsWith(card.Number(fmt.Sprintf("card-%d", i)))) + client.User.Update().AddVersion(1).Where(p).ExecX(ctx) + client.User.Delete().Where(p).ExecX(ctx) + } +} + func TestPostCreation(t *testing.T) { ctx := context.Background() client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") diff --git a/entc/integration/idtype/ent/mutation.go b/entc/integration/idtype/ent/mutation.go index a8157aa3a..56d44ba42 100644 --- a/entc/integration/idtype/ent/mutation.go +++ b/entc/integration/idtype/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -80,7 +81,7 @@ func withUserID(id uint64) 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) } @@ -113,7 +114,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() @@ -129,6 +130,25 @@ func (m *UserMutation) ID() (id uint64, 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) ([]uint64, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []uint64{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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -148,10 +168,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index a6e5fecb4..f2cfa9858 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -1631,7 +1631,7 @@ func Mutation(t *testing.T, client *ent.Client) { m.SetAge(30) } } - uu := a8m.Update() + uu := a8m.Update().AddPets(pedro) ub = client.User.Create() setUsers(ub.Mutation(), uu.Mutation()) a8m = uu.SaveX(ctx) @@ -1642,6 +1642,35 @@ func Mutation(t *testing.T, client *ent.Client) { require.Equal(t, []int{usr.ID}, a8m.Update().AddFriends(usr).Mutation().FriendsIDs()) require.Empty(t, a8m.Update().AddFriends(usr).RemoveFriends(usr).Mutation().FriendsIDs()) require.Equal(t, []int{usr.ID}, a8m.Update().AddFriends(usr).RemoveFriends(a8m).Mutation().FriendsIDs()) + a8m.Update().AddFriends(usr).ExecX(ctx) + + t.Run("IDs", func(t *testing.T) { + ids := client.User.Query().IDsX(ctx) + u := client.User.Update().Where(user.IDIn(ids...)).AddAge(1) + mids, err := u.Mutation().IDs(ctx) + require.NoError(t, err) + // Order can change between the 2 queries. + sort.Ints(ids) + sort.Ints(mids) + require.Equal(t, ids, mids) + u.ExecX(ctx) + + u = client.User. + Update(). + AddAge(1). + Where( + user.Name(a8m.Name), + user.HasPets(), + user.HasPetsWith( + pet.Name(pedro.Name), + ), + ) + mids, err = u.Mutation().IDs(ctx) + require.NoError(t, err) + require.Len(t, mids, 1) + require.Equal(t, a8m.ID, mids[0]) + u.ExecX(ctx) + }) } // Test templates codegen. diff --git a/entc/integration/json/ent/mutation.go b/entc/integration/json/ent/mutation.go index 0064b39c2..49300483f 100644 --- a/entc/integration/json/ent/mutation.go +++ b/entc/integration/json/ent/mutation.go @@ -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 { diff --git a/entc/integration/migrate/entv1/mutation.go b/entc/integration/migrate/entv1/mutation.go index 8c4f16b7b..61a2c0ddf 100644 --- a/entc/integration/migrate/entv1/mutation.go +++ b/entc/integration/migrate/entv1/mutation.go @@ -8,6 +8,7 @@ package entv1 import ( "context" + "errors" "fmt" "sync" @@ -79,7 +80,7 @@ func withCarID(id int) carOption { m.oldValue = func(ctx context.Context) (*Car, 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().Car.Get(ctx, id) } @@ -112,7 +113,7 @@ func (m CarMutation) Client() *Client { // it returns an error otherwise. func (m CarMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv1: mutation is not running in a transaction") + return nil, errors.New("entv1: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -128,6 +129,25 @@ func (m *CarMutation) 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 *CarMutation) 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().Car.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *CarMutation) SetOwnerID(id int) { m.owner = &id @@ -393,7 +413,7 @@ func withConversionID(id int) conversionOption { m.oldValue = func(ctx context.Context) (*Conversion, 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().Conversion.Get(ctx, id) } @@ -426,7 +446,7 @@ func (m ConversionMutation) Client() *Client { // it returns an error otherwise. func (m ConversionMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv1: mutation is not running in a transaction") + return nil, errors.New("entv1: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -442,6 +462,25 @@ func (m *ConversionMutation) 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 *ConversionMutation) 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().Conversion.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *ConversionMutation) SetName(s string) { m.name = &s @@ -461,10 +500,10 @@ func (m *ConversionMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -511,10 +550,10 @@ func (m *ConversionMutation) Int8ToString() (r int8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt8ToString(ctx context.Context) (v int8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt8ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt8ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt8ToString requires an ID field in the mutation") + return v, errors.New("OldInt8ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -581,10 +620,10 @@ func (m *ConversionMutation) Uint8ToString() (r uint8, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint8ToString(ctx context.Context) (v uint8, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint8ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint8ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint8ToString requires an ID field in the mutation") + return v, errors.New("OldUint8ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -651,10 +690,10 @@ func (m *ConversionMutation) Int16ToString() (r int16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt16ToString(ctx context.Context) (v int16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt16ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt16ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt16ToString requires an ID field in the mutation") + return v, errors.New("OldInt16ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -721,10 +760,10 @@ func (m *ConversionMutation) Uint16ToString() (r uint16, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint16ToString(ctx context.Context) (v uint16, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint16ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint16ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint16ToString requires an ID field in the mutation") + return v, errors.New("OldUint16ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -791,10 +830,10 @@ func (m *ConversionMutation) Int32ToString() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt32ToString(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt32ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt32ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt32ToString requires an ID field in the mutation") + return v, errors.New("OldInt32ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -861,10 +900,10 @@ func (m *ConversionMutation) Uint32ToString() (r uint32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint32ToString(ctx context.Context) (v uint32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint32ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint32ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint32ToString requires an ID field in the mutation") + return v, errors.New("OldUint32ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -931,10 +970,10 @@ func (m *ConversionMutation) Int64ToString() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt64ToString(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt64ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt64ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt64ToString requires an ID field in the mutation") + return v, errors.New("OldInt64ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1001,10 +1040,10 @@ func (m *ConversionMutation) Uint64ToString() (r uint64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint64ToString(ctx context.Context) (v uint64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint64ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint64ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint64ToString requires an ID field in the mutation") + return v, errors.New("OldUint64ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1550,7 +1589,7 @@ func withCustomTypeID(id int) customtypeOption { m.oldValue = func(ctx context.Context) (*CustomType, 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().CustomType.Get(ctx, id) } @@ -1583,7 +1622,7 @@ func (m CustomTypeMutation) Client() *Client { // it returns an error otherwise. func (m CustomTypeMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv1: mutation is not running in a transaction") + return nil, errors.New("entv1: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -1599,6 +1638,25 @@ func (m *CustomTypeMutation) 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 *CustomTypeMutation) 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().CustomType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetCustom sets the "custom" field. func (m *CustomTypeMutation) SetCustom(s string) { m.custom = &s @@ -1618,10 +1676,10 @@ func (m *CustomTypeMutation) Custom() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CustomTypeMutation) OldCustom(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCustom is only allowed on UpdateOne operations") + return v, errors.New("OldCustom is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCustom requires an ID field in the mutation") + return v, errors.New("OldCustom requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1883,7 +1941,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) } @@ -1916,7 +1974,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("entv1: mutation is not running in a transaction") + return nil, errors.New("entv1: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -1938,6 +1996,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int32) { m.age = &i @@ -1958,10 +2035,10 @@ func (m *UserMutation) Age() (r int32, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int32, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2013,10 +2090,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2049,10 +2126,10 @@ func (m *UserMutation) Description() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldDescription(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDescription is only allowed on UpdateOne operations") + return v, errors.New("OldDescription is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDescription requires an ID field in the mutation") + return v, errors.New("OldDescription requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2098,10 +2175,10 @@ func (m *UserMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2134,10 +2211,10 @@ func (m *UserMutation) Address() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAddress(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAddress is only allowed on UpdateOne operations") + return v, errors.New("OldAddress is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAddress requires an ID field in the mutation") + return v, errors.New("OldAddress requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2183,10 +2260,10 @@ func (m *UserMutation) Renamed() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldRenamed(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRenamed is only allowed on UpdateOne operations") + return v, errors.New("OldRenamed is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRenamed requires an ID field in the mutation") + return v, errors.New("OldRenamed requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2232,10 +2309,10 @@ func (m *UserMutation) Blob() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldBlob(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBlob is only allowed on UpdateOne operations") + return v, errors.New("OldBlob is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBlob requires an ID field in the mutation") + return v, errors.New("OldBlob requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2281,10 +2358,10 @@ func (m *UserMutation) State() (r user.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldState(ctx context.Context) (v user.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2330,10 +2407,10 @@ func (m *UserMutation) Status() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldStatus(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStatus is only allowed on UpdateOne operations") + return v, errors.New("OldStatus is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStatus requires an ID field in the mutation") + return v, errors.New("OldStatus requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2379,10 +2456,10 @@ func (m *UserMutation) Workplace() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldWorkplace(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldWorkplace is only allowed on UpdateOne operations") + return v, errors.New("OldWorkplace is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldWorkplace requires an ID field in the mutation") + return v, errors.New("OldWorkplace requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/migrate/entv2/mutation.go b/entc/integration/migrate/entv2/mutation.go index 79153048b..455727e13 100644 --- a/entc/integration/migrate/entv2/mutation.go +++ b/entc/integration/migrate/entv2/mutation.go @@ -8,6 +8,7 @@ package entv2 import ( "context" + "errors" "fmt" "sync" "time" @@ -85,7 +86,7 @@ func withCarID(id int) carOption { m.oldValue = func(ctx context.Context) (*Car, 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().Car.Get(ctx, id) } @@ -118,7 +119,7 @@ func (m CarMutation) Client() *Client { // it returns an error otherwise. func (m CarMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -134,6 +135,25 @@ func (m *CarMutation) 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 *CarMutation) 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().Car.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *CarMutation) SetOwnerID(id int) { m.owner = &id @@ -391,7 +411,7 @@ func withConversionID(id int) conversionOption { m.oldValue = func(ctx context.Context) (*Conversion, 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().Conversion.Get(ctx, id) } @@ -424,7 +444,7 @@ func (m ConversionMutation) Client() *Client { // it returns an error otherwise. func (m ConversionMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -440,6 +460,25 @@ func (m *ConversionMutation) 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 *ConversionMutation) 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().Conversion.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *ConversionMutation) SetName(s string) { m.name = &s @@ -459,10 +498,10 @@ func (m *ConversionMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -508,10 +547,10 @@ func (m *ConversionMutation) Int8ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt8ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt8ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt8ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt8ToString requires an ID field in the mutation") + return v, errors.New("OldInt8ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -557,10 +596,10 @@ func (m *ConversionMutation) Uint8ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint8ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint8ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint8ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint8ToString requires an ID field in the mutation") + return v, errors.New("OldUint8ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -606,10 +645,10 @@ func (m *ConversionMutation) Int16ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt16ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt16ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt16ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt16ToString requires an ID field in the mutation") + return v, errors.New("OldInt16ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -655,10 +694,10 @@ func (m *ConversionMutation) Uint16ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint16ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint16ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint16ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint16ToString requires an ID field in the mutation") + return v, errors.New("OldUint16ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -704,10 +743,10 @@ func (m *ConversionMutation) Int32ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt32ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt32ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt32ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt32ToString requires an ID field in the mutation") + return v, errors.New("OldInt32ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -753,10 +792,10 @@ func (m *ConversionMutation) Uint32ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint32ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint32ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint32ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint32ToString requires an ID field in the mutation") + return v, errors.New("OldUint32ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -802,10 +841,10 @@ func (m *ConversionMutation) Int64ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldInt64ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldInt64ToString is only allowed on UpdateOne operations") + return v, errors.New("OldInt64ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldInt64ToString requires an ID field in the mutation") + return v, errors.New("OldInt64ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -851,10 +890,10 @@ func (m *ConversionMutation) Uint64ToString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *ConversionMutation) OldUint64ToString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUint64ToString is only allowed on UpdateOne operations") + return v, errors.New("OldUint64ToString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUint64ToString requires an ID field in the mutation") + return v, errors.New("OldUint64ToString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1281,7 +1320,7 @@ func withCustomTypeID(id int) customtypeOption { m.oldValue = func(ctx context.Context) (*CustomType, 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().CustomType.Get(ctx, id) } @@ -1314,7 +1353,7 @@ func (m CustomTypeMutation) Client() *Client { // it returns an error otherwise. func (m CustomTypeMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -1330,6 +1369,25 @@ func (m *CustomTypeMutation) 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 *CustomTypeMutation) 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().CustomType.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetCustom sets the "custom" field. func (m *CustomTypeMutation) SetCustom(s string) { m.custom = &s @@ -1349,10 +1407,10 @@ func (m *CustomTypeMutation) Custom() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CustomTypeMutation) OldCustom(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCustom is only allowed on UpdateOne operations") + return v, errors.New("OldCustom is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCustom requires an ID field in the mutation") + return v, errors.New("OldCustom requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1594,7 +1652,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -1627,7 +1685,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -1643,6 +1701,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // Where appends a list predicates to the GroupMutation builder. func (m *GroupMutation) Where(ps ...predicate.Group) { m.predicates = append(m.predicates, ps...) @@ -1827,7 +1904,7 @@ func withMediaID(id int) mediaOption { m.oldValue = func(ctx context.Context) (*Media, 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().Media.Get(ctx, id) } @@ -1860,7 +1937,7 @@ func (m MediaMutation) Client() *Client { // it returns an error otherwise. func (m MediaMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -1876,6 +1953,25 @@ func (m *MediaMutation) 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 *MediaMutation) 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().Media.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetSource sets the "source" field. func (m *MediaMutation) SetSource(s string) { m.source = &s @@ -1895,10 +1991,10 @@ func (m *MediaMutation) Source() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MediaMutation) OldSource(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSource is only allowed on UpdateOne operations") + return v, errors.New("OldSource is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSource requires an ID field in the mutation") + return v, errors.New("OldSource requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1944,10 +2040,10 @@ func (m *MediaMutation) SourceURI() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MediaMutation) OldSourceURI(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldSourceURI is only allowed on UpdateOne operations") + return v, errors.New("OldSourceURI is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldSourceURI requires an ID field in the mutation") + return v, errors.New("OldSourceURI requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1993,10 +2089,10 @@ func (m *MediaMutation) Text() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *MediaMutation) OldText(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldText is only allowed on UpdateOne operations") + return v, errors.New("OldText is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldText requires an ID field in the mutation") + return v, errors.New("OldText requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2286,7 +2382,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -2319,7 +2415,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { - return nil, fmt.Errorf("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -2335,6 +2431,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetOwnerID sets the "owner" edge to the User entity by id. func (m *PetMutation) SetOwnerID(id int) { m.owner = &id @@ -2607,7 +2722,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) } @@ -2640,7 +2755,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("entv2: mutation is not running in a transaction") + return nil, errors.New("entv2: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() @@ -2662,6 +2777,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) + } +} + // SetMixedString sets the "mixed_string" field. func (m *UserMutation) SetMixedString(s string) { m.mixed_string = &s @@ -2681,10 +2815,10 @@ func (m *UserMutation) MixedString() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldMixedString(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMixedString is only allowed on UpdateOne operations") + return v, errors.New("OldMixedString is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMixedString requires an ID field in the mutation") + return v, errors.New("OldMixedString requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2717,10 +2851,10 @@ func (m *UserMutation) MixedEnum() (r user.MixedEnum, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldMixedEnum(ctx context.Context) (v user.MixedEnum, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMixedEnum is only allowed on UpdateOne operations") + return v, errors.New("OldMixedEnum is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMixedEnum requires an ID field in the mutation") + return v, errors.New("OldMixedEnum requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2754,10 +2888,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2809,10 +2943,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2845,10 +2979,10 @@ func (m *UserMutation) Description() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldDescription(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDescription is only allowed on UpdateOne operations") + return v, errors.New("OldDescription is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDescription requires an ID field in the mutation") + return v, errors.New("OldDescription requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2894,10 +3028,10 @@ func (m *UserMutation) Nickname() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldNickname(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNickname is only allowed on UpdateOne operations") + return v, errors.New("OldNickname is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNickname requires an ID field in the mutation") + return v, errors.New("OldNickname requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2930,10 +3064,10 @@ func (m *UserMutation) Phone() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldPhone(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldPhone is only allowed on UpdateOne operations") + return v, errors.New("OldPhone is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldPhone requires an ID field in the mutation") + return v, errors.New("OldPhone requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -2966,10 +3100,10 @@ func (m *UserMutation) Buffer() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldBuffer(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBuffer is only allowed on UpdateOne operations") + return v, errors.New("OldBuffer is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBuffer requires an ID field in the mutation") + return v, errors.New("OldBuffer requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3015,10 +3149,10 @@ func (m *UserMutation) Title() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldTitle(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldTitle is only allowed on UpdateOne operations") + return v, errors.New("OldTitle is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldTitle requires an ID field in the mutation") + return v, errors.New("OldTitle requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3051,10 +3185,10 @@ func (m *UserMutation) NewName() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldNewName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNewName is only allowed on UpdateOne operations") + return v, errors.New("OldNewName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNewName requires an ID field in the mutation") + return v, errors.New("OldNewName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3100,10 +3234,10 @@ func (m *UserMutation) Blob() (r []byte, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldBlob(ctx context.Context) (v []byte, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldBlob is only allowed on UpdateOne operations") + return v, errors.New("OldBlob is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldBlob requires an ID field in the mutation") + return v, errors.New("OldBlob requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3149,10 +3283,10 @@ func (m *UserMutation) State() (r user.State, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldState(ctx context.Context) (v user.State, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldState is only allowed on UpdateOne operations") + return v, errors.New("OldState is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldState requires an ID field in the mutation") + return v, errors.New("OldState requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3198,10 +3332,10 @@ func (m *UserMutation) Status() (r user.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldStatus(ctx context.Context) (v user.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStatus is only allowed on UpdateOne operations") + return v, errors.New("OldStatus is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStatus requires an ID field in the mutation") + return v, errors.New("OldStatus requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3247,10 +3381,10 @@ func (m *UserMutation) Workplace() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldWorkplace(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldWorkplace is only allowed on UpdateOne operations") + return v, errors.New("OldWorkplace is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldWorkplace requires an ID field in the mutation") + return v, errors.New("OldWorkplace requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -3296,10 +3430,10 @@ func (m *UserMutation) CreatedAt() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldCreatedAt is only allowed on UpdateOne operations") + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldCreatedAt requires an ID field in the mutation") + return v, errors.New("OldCreatedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/multischema/ent/mutation.go b/entc/integration/multischema/ent/mutation.go index 8d55285d5..2ec37d3ea 100644 --- a/entc/integration/multischema/ent/mutation.go +++ b/entc/integration/multischema/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -79,7 +80,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -112,7 +113,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -128,6 +129,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -147,10 +167,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -463,7 +483,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -496,7 +516,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -512,6 +532,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *PetMutation) SetName(s string) { m.name = &s @@ -531,10 +570,10 @@ func (m *PetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -567,10 +606,10 @@ func (m *PetMutation) OwnerID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldOwnerID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldOwnerID is only allowed on UpdateOne operations") + return v, errors.New("OldOwnerID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldOwnerID requires an ID field in the mutation") + return v, errors.New("OldOwnerID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -893,7 +932,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) } @@ -926,7 +965,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() @@ -942,6 +981,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -961,10 +1019,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/privacy/ent/mutation.go b/entc/integration/privacy/ent/mutation.go index eda24b3ed..c128b30e2 100644 --- a/entc/integration/privacy/ent/mutation.go +++ b/entc/integration/privacy/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -85,7 +86,7 @@ func withTaskID(id int) taskOption { m.oldValue = func(ctx context.Context) (*Task, 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().Task.Get(ctx, id) } @@ -118,7 +119,7 @@ func (m TaskMutation) Client() *Client { // it returns an error otherwise. func (m TaskMutation) 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() @@ -134,6 +135,25 @@ func (m *TaskMutation) 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 *TaskMutation) 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().Task.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetTitle sets the "title" field. func (m *TaskMutation) SetTitle(s string) { m.title = &s @@ -153,10 +173,10 @@ func (m *TaskMutation) Title() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldTitle(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldTitle is only allowed on UpdateOne operations") + return v, errors.New("OldTitle is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldTitle requires an ID field in the mutation") + return v, errors.New("OldTitle requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -189,10 +209,10 @@ func (m *TaskMutation) Description() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldDescription(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDescription is only allowed on UpdateOne operations") + return v, errors.New("OldDescription is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDescription requires an ID field in the mutation") + return v, errors.New("OldDescription requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -238,10 +258,10 @@ func (m *TaskMutation) Status() (r task.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldStatus(ctx context.Context) (v task.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStatus is only allowed on UpdateOne operations") + return v, errors.New("OldStatus is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStatus requires an ID field in the mutation") + return v, errors.New("OldStatus requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -274,10 +294,10 @@ func (m *TaskMutation) UUID() (r uuid.UUID, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TaskMutation) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldUUID is only allowed on UpdateOne operations") + return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldUUID requires an ID field in the mutation") + return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -730,7 +750,7 @@ func withTeamID(id int) teamOption { m.oldValue = func(ctx context.Context) (*Team, 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().Team.Get(ctx, id) } @@ -763,7 +783,7 @@ func (m TeamMutation) Client() *Client { // it returns an error otherwise. func (m TeamMutation) 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() @@ -779,6 +799,25 @@ func (m *TeamMutation) 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 *TeamMutation) 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().Team.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *TeamMutation) SetName(s string) { m.name = &s @@ -798,10 +837,10 @@ func (m *TeamMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TeamMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1200,7 +1239,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) } @@ -1233,7 +1272,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() @@ -1249,6 +1288,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -1268,10 +1326,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1305,10 +1363,10 @@ func (m *UserMutation) Age() (r uint, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v uint, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/entc/integration/template/ent/mutation.go b/entc/integration/template/ent/mutation.go index f4584d067..5a5ef99a1 100644 --- a/entc/integration/template/ent/mutation.go +++ b/entc/integration/template/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" "time" @@ -78,7 +79,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -111,7 +112,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -127,6 +128,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetMaxUsers sets the "max_users" field. func (m *GroupMutation) SetMaxUsers(i int) { m.max_users = &i @@ -147,10 +167,10 @@ func (m *GroupMutation) MaxUsers() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldMaxUsers(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldMaxUsers is only allowed on UpdateOne operations") + return v, errors.New("OldMaxUsers is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldMaxUsers requires an ID field in the mutation") + return v, errors.New("OldMaxUsers requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -409,7 +429,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -442,7 +462,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -458,6 +478,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetAge sets the "age" field. func (m *PetMutation) SetAge(i int) { m.age = &i @@ -478,10 +517,10 @@ func (m *PetMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -533,10 +572,10 @@ func (m *PetMutation) LicensedAt() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldLicensedAt(ctx context.Context) (v *time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldLicensedAt is only allowed on UpdateOne operations") + return v, errors.New("OldLicensedAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldLicensedAt requires an ID field in the mutation") + return v, errors.New("OldLicensedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -884,7 +923,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) } @@ -917,7 +956,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() @@ -933,6 +972,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -952,10 +1010,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/edgeindex/ent/mutation.go b/examples/edgeindex/ent/mutation.go index f7b7b409a..203c08ab4 100644 --- a/examples/edgeindex/ent/mutation.go +++ b/examples/edgeindex/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -77,7 +78,7 @@ func withCityID(id int) cityOption { m.oldValue = func(ctx context.Context) (*City, 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().City.Get(ctx, id) } @@ -110,7 +111,7 @@ func (m CityMutation) Client() *Client { // it returns an error otherwise. func (m CityMutation) 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() @@ -126,6 +127,25 @@ func (m *CityMutation) 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 *CityMutation) 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().City.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *CityMutation) SetName(s string) { m.name = &s @@ -145,10 +165,10 @@ func (m *CityMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CityMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -461,7 +481,7 @@ func withStreetID(id int) streetOption { m.oldValue = func(ctx context.Context) (*Street, 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().Street.Get(ctx, id) } @@ -494,7 +514,7 @@ func (m StreetMutation) Client() *Client { // it returns an error otherwise. func (m StreetMutation) 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() @@ -510,6 +530,25 @@ func (m *StreetMutation) 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 *StreetMutation) 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().Street.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *StreetMutation) SetName(s string) { m.name = &s @@ -529,10 +568,10 @@ func (m *StreetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *StreetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/entcpkg/ent/mutation.go b/examples/entcpkg/ent/mutation.go index a7448ea59..27b36aae2 100644 --- a/examples/entcpkg/ent/mutation.go +++ b/examples/entcpkg/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -74,7 +75,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) } @@ -107,7 +108,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() @@ -123,6 +124,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -142,10 +162,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -192,10 +212,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/fs/ent/mutation.go b/examples/fs/ent/mutation.go index 27775f316..6679ca241 100644 --- a/examples/fs/ent/mutation.go +++ b/examples/fs/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -78,7 +79,7 @@ func withFileID(id int) fileOption { m.oldValue = func(ctx context.Context) (*File, 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().File.Get(ctx, id) } @@ -111,7 +112,7 @@ func (m FileMutation) Client() *Client { // it returns an error otherwise. func (m FileMutation) 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() @@ -127,6 +128,25 @@ func (m *FileMutation) 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 *FileMutation) 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().File.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *FileMutation) SetName(s string) { m.name = &s @@ -146,10 +166,10 @@ func (m *FileMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -182,10 +202,10 @@ func (m *FileMutation) Deleted() (r bool, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldDeleted(ctx context.Context) (v bool, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldDeleted is only allowed on UpdateOne operations") + return v, errors.New("OldDeleted is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldDeleted requires an ID field in the mutation") + return v, errors.New("OldDeleted requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -218,10 +238,10 @@ func (m *FileMutation) ParentID() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *FileMutation) OldParentID(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldParentID is only allowed on UpdateOne operations") + return v, errors.New("OldParentID is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldParentID requires an ID field in the mutation") + return v, errors.New("OldParentID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/m2m2types/ent/mutation.go b/examples/m2m2types/ent/mutation.go index cd40afd68..6de4cfdc2 100644 --- a/examples/m2m2types/ent/mutation.go +++ b/examples/m2m2types/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -77,7 +78,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -110,7 +111,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -126,6 +127,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -145,10 +165,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -464,7 +484,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) } @@ -497,7 +517,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() @@ -513,6 +533,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -533,10 +572,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -588,10 +627,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/m2mbidi/ent/mutation.go b/examples/m2mbidi/ent/mutation.go index 65ffc12fd..e77cc2b1c 100644 --- a/examples/m2mbidi/ent/mutation.go +++ b/examples/m2mbidi/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -77,7 +78,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) } @@ -110,7 +111,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() @@ -126,6 +127,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -146,10 +166,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -201,10 +221,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/m2mrecur/ent/mutation.go b/examples/m2mrecur/ent/mutation.go index f34b8b6e8..f6d19233f 100644 --- a/examples/m2mrecur/ent/mutation.go +++ b/examples/m2mrecur/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -80,7 +81,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) } @@ -113,7 +114,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() @@ -129,6 +130,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -149,10 +169,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -204,10 +224,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/o2m2types/ent/mutation.go b/examples/o2m2types/ent/mutation.go index d7d007f9d..5b404e3cf 100644 --- a/examples/o2m2types/ent/mutation.go +++ b/examples/o2m2types/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -76,7 +77,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -109,7 +110,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -125,6 +126,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *PetMutation) SetName(s string) { m.name = &s @@ -144,10 +164,10 @@ func (m *PetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -440,7 +460,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) } @@ -473,7 +493,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() @@ -489,6 +509,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -509,10 +548,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -564,10 +603,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/o2mrecur/ent/mutation.go b/examples/o2mrecur/ent/mutation.go index 0838bfa65..a4e9db4a7 100644 --- a/examples/o2mrecur/ent/mutation.go +++ b/examples/o2mrecur/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -78,7 +79,7 @@ func withNodeID(id int) nodeOption { m.oldValue = func(ctx context.Context) (*Node, 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().Node.Get(ctx, id) } @@ -111,7 +112,7 @@ func (m NodeMutation) Client() *Client { // it returns an error otherwise. func (m NodeMutation) 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() @@ -127,6 +128,25 @@ func (m *NodeMutation) 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 *NodeMutation) 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().Node.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetValue sets the "value" field. func (m *NodeMutation) SetValue(i int) { m.value = &i @@ -147,10 +167,10 @@ func (m *NodeMutation) Value() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValue is only allowed on UpdateOne operations") + return v, errors.New("OldValue is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValue requires an ID field in the mutation") + return v, errors.New("OldValue requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/o2o2types/ent/mutation.go b/examples/o2o2types/ent/mutation.go index cad77e8b0..7073e94d6 100644 --- a/examples/o2o2types/ent/mutation.go +++ b/examples/o2o2types/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" "time" @@ -78,7 +79,7 @@ func withCardID(id int) cardOption { m.oldValue = func(ctx context.Context) (*Card, 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().Card.Get(ctx, id) } @@ -111,7 +112,7 @@ func (m CardMutation) Client() *Client { // it returns an error otherwise. func (m CardMutation) 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() @@ -127,6 +128,25 @@ func (m *CardMutation) 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 *CardMutation) 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().Card.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetExpired sets the "expired" field. func (m *CardMutation) SetExpired(t time.Time) { m.expired = &t @@ -146,10 +166,10 @@ func (m *CardMutation) Expired() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldExpired(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldExpired is only allowed on UpdateOne operations") + return v, errors.New("OldExpired is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldExpired requires an ID field in the mutation") + return v, errors.New("OldExpired requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -182,10 +202,10 @@ func (m *CardMutation) Number() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldNumber is only allowed on UpdateOne operations") + return v, errors.New("OldNumber is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldNumber requires an ID field in the mutation") + return v, errors.New("OldNumber requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -494,7 +514,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) } @@ -527,7 +547,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() @@ -543,6 +563,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -563,10 +602,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -618,10 +657,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/o2obidi/ent/mutation.go b/examples/o2obidi/ent/mutation.go index bd4dd4d2d..9b731a396 100644 --- a/examples/o2obidi/ent/mutation.go +++ b/examples/o2obidi/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -76,7 +77,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) } @@ -109,7 +110,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() @@ -125,6 +126,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -145,10 +165,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -200,10 +220,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/o2orecur/ent/mutation.go b/examples/o2orecur/ent/mutation.go index 5d16171de..3c4555402 100644 --- a/examples/o2orecur/ent/mutation.go +++ b/examples/o2orecur/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -77,7 +78,7 @@ func withNodeID(id int) nodeOption { m.oldValue = func(ctx context.Context) (*Node, 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().Node.Get(ctx, id) } @@ -110,7 +111,7 @@ func (m NodeMutation) Client() *Client { // it returns an error otherwise. func (m NodeMutation) 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() @@ -126,6 +127,25 @@ func (m *NodeMutation) 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 *NodeMutation) 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().Node.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetValue sets the "value" field. func (m *NodeMutation) SetValue(i int) { m.value = &i @@ -146,10 +166,10 @@ func (m *NodeMutation) Value() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldValue is only allowed on UpdateOne operations") + return v, errors.New("OldValue is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldValue requires an ID field in the mutation") + return v, errors.New("OldValue requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/privacyadmin/ent/mutation.go b/examples/privacyadmin/ent/mutation.go index c12ec52fb..cb4500516 100644 --- a/examples/privacyadmin/ent/mutation.go +++ b/examples/privacyadmin/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -72,7 +73,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) } @@ -105,7 +106,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() @@ -121,6 +122,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -140,10 +160,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/privacytenant/ent/mutation.go b/examples/privacytenant/ent/mutation.go index 70ea6d4b7..c36b49748 100644 --- a/examples/privacytenant/ent/mutation.go +++ b/examples/privacytenant/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -81,7 +82,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -114,7 +115,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -130,6 +131,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -149,10 +169,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -520,7 +540,7 @@ func withTenantID(id int) tenantOption { m.oldValue = func(ctx context.Context) (*Tenant, 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().Tenant.Get(ctx, id) } @@ -553,7 +573,7 @@ func (m TenantMutation) Client() *Client { // it returns an error otherwise. func (m TenantMutation) 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() @@ -569,6 +589,25 @@ func (m *TenantMutation) 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 *TenantMutation) 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().Tenant.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *TenantMutation) SetName(s string) { m.name = &s @@ -588,10 +627,10 @@ func (m *TenantMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *TenantMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -818,7 +857,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) } @@ -851,7 +890,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() @@ -867,6 +906,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) + } +} + // SetName sets the "name" field. func (m *UserMutation) SetName(s string) { m.name = &s @@ -886,10 +944,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -922,10 +980,10 @@ func (m *UserMutation) Foods() (r []string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldFoods(ctx context.Context) (v []string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldFoods is only allowed on UpdateOne operations") + return v, errors.New("OldFoods is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldFoods requires an ID field in the mutation") + return v, errors.New("OldFoods requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/start/ent/mutation.go b/examples/start/ent/mutation.go index c55e2d243..9f8daeb7b 100644 --- a/examples/start/ent/mutation.go +++ b/examples/start/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" "time" @@ -80,7 +81,7 @@ func withCarID(id int) carOption { m.oldValue = func(ctx context.Context) (*Car, 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().Car.Get(ctx, id) } @@ -113,7 +114,7 @@ func (m CarMutation) Client() *Client { // it returns an error otherwise. func (m CarMutation) 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() @@ -129,6 +130,25 @@ func (m *CarMutation) 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 *CarMutation) 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().Car.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetModel sets the "model" field. func (m *CarMutation) SetModel(s string) { m.model = &s @@ -148,10 +168,10 @@ func (m *CarMutation) Model() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldModel(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldModel is only allowed on UpdateOne operations") + return v, errors.New("OldModel is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldModel requires an ID field in the mutation") + return v, errors.New("OldModel requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -184,10 +204,10 @@ func (m *CarMutation) RegisteredAt() (r time.Time, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *CarMutation) OldRegisteredAt(ctx context.Context) (v time.Time, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldRegisteredAt is only allowed on UpdateOne operations") + return v, errors.New("OldRegisteredAt is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldRegisteredAt requires an ID field in the mutation") + return v, errors.New("OldRegisteredAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -495,7 +515,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -528,7 +548,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -544,6 +564,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -563,10 +602,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -885,7 +924,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) } @@ -918,7 +957,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() @@ -934,6 +973,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -954,10 +1012,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1009,10 +1067,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/traversal/ent/mutation.go b/examples/traversal/ent/mutation.go index ebbceadbf..b3a11e4ba 100644 --- a/examples/traversal/ent/mutation.go +++ b/examples/traversal/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -81,7 +82,7 @@ func withGroupID(id int) groupOption { m.oldValue = func(ctx context.Context) (*Group, 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().Group.Get(ctx, id) } @@ -114,7 +115,7 @@ func (m GroupMutation) Client() *Client { // it returns an error otherwise. func (m GroupMutation) 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() @@ -130,6 +131,25 @@ func (m *GroupMutation) 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 *GroupMutation) 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().Group.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -149,10 +169,10 @@ func (m *GroupMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *GroupMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -525,7 +545,7 @@ func withPetID(id int) petOption { m.oldValue = func(ctx context.Context) (*Pet, 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().Pet.Get(ctx, id) } @@ -558,7 +578,7 @@ func (m PetMutation) Client() *Client { // it returns an error otherwise. func (m PetMutation) 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() @@ -574,6 +594,25 @@ func (m *PetMutation) 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 *PetMutation) 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().Pet.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + // SetName sets the "name" field. func (m *PetMutation) SetName(s string) { m.name = &s @@ -593,10 +632,10 @@ func (m *PetMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *PetMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -978,7 +1017,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) } @@ -1011,7 +1050,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() @@ -1027,6 +1066,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) + } +} + // SetAge sets the "age" field. func (m *UserMutation) SetAge(i int) { m.age = &i @@ -1047,10 +1105,10 @@ func (m *UserMutation) Age() (r int, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations") + return v, errors.New("OldAge is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldAge requires an ID field in the mutation") + return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -1102,10 +1160,10 @@ func (m *UserMutation) Name() (r string, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldName is only allowed on UpdateOne operations") + return v, errors.New("OldName is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldName requires an ID field in the mutation") + return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { diff --git a/examples/version/ent/mutation.go b/examples/version/ent/mutation.go index 883c6b548..6f95cadd8 100644 --- a/examples/version/ent/mutation.go +++ b/examples/version/ent/mutation.go @@ -8,6 +8,7 @@ package ent import ( "context" + "errors" "fmt" "sync" @@ -74,7 +75,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) } @@ -107,7 +108,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() @@ -123,6 +124,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) + } +} + // SetVersion sets the "version" field. func (m *UserMutation) SetVersion(i int64) { m.version = &i @@ -143,10 +163,10 @@ func (m *UserMutation) Version() (r int64, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldVersion(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldVersion is only allowed on UpdateOne operations") + return v, errors.New("OldVersion is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldVersion requires an ID field in the mutation") + return v, errors.New("OldVersion requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { @@ -198,10 +218,10 @@ func (m *UserMutation) Status() (r user.Status, exists bool) { // An error is returned if the mutation operation is not UpdateOne, or the database query fails. func (m *UserMutation) OldStatus(ctx context.Context) (v user.Status, err error) { if !m.op.Is(OpUpdateOne) { - return v, fmt.Errorf("OldStatus is only allowed on UpdateOne operations") + return v, errors.New("OldStatus is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, fmt.Errorf("OldStatus requires an ID field in the mutation") + return v, errors.New("OldStatus requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil {