From 36553bbd8a9bdc82a8b927b9eae08a5cbe120374 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Fri, 12 May 2023 10:56:26 +0300 Subject: [PATCH] entc/gen: avoid conflict between order by edge-count and fields end with _count (#3534) --- entc/gen/type.go | 15 ++- entc/integration/ent/entql.go | 6 ++ entc/integration/ent/migrate/schema.go | 7 +- entc/integration/ent/mutation.go | 109 +++++++++++++++++++- entc/integration/ent/schema/user.go | 4 + entc/integration/ent/user.go | 13 ++- entc/integration/ent/user/user.go | 8 ++ entc/integration/ent/user/where.go | 55 ++++++++++ entc/integration/ent/user_create.go | 98 ++++++++++++++++++ entc/integration/ent/user_update.go | 72 +++++++++++++ entc/integration/gremlin/ent/mutation.go | 109 +++++++++++++++++++- entc/integration/gremlin/ent/user.go | 9 ++ entc/integration/gremlin/ent/user/user.go | 2 + entc/integration/gremlin/ent/user/where.go | 77 ++++++++++++++ entc/integration/gremlin/ent/user_create.go | 17 +++ entc/integration/gremlin/ent/user_update.go | 72 +++++++++++++ 16 files changed, 666 insertions(+), 7 deletions(-) diff --git a/entc/gen/type.go b/entc/gen/type.go index f2b5121684..c70fe8101b 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -1067,7 +1067,20 @@ func (f Field) DefaultValue() any { return f.def.DefaultValue } func (f Field) DefaultFunc() bool { return f.def.DefaultKind == reflect.Func } // OrderName returns the function/option name for ordering by this field. -func (f Field) OrderName() string { return "By" + pascal(f.Name) } +func (f Field) OrderName() string { + name := "By" + pascal(f.Name) + // Some users store associations count as a separate field. + // In this case, we suffix the order name with "Field". + if f.typ == nil || !strings.HasSuffix(name, "Count") { + return name + } + for _, e := range f.typ.Edges { + if nameE, err := e.OrderCountName(); err == nil && nameE == name { + return name + "Field" + } + } + return name +} // BuilderField returns the struct member of the field in the builder. func (f Field) BuilderField() string { diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 15535ee439..8a25aed456 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -378,6 +378,7 @@ var schemaGraph = func() *sqlgraph.Schema { user.FieldRole: {Type: field.TypeEnum, Column: user.FieldRole}, user.FieldEmployment: {Type: field.TypeEnum, Column: user.FieldEmployment}, user.FieldSSOCert: {Type: field.TypeString, Column: user.FieldSSOCert}, + user.FieldFilesCount: {Type: field.TypeInt, Column: user.FieldFilesCount}, }, } graph.MustAddE( @@ -2252,6 +2253,11 @@ func (f *UserFilter) WhereSSOCert(p entql.StringP) { f.Where(p.Field(user.FieldSSOCert)) } +// WhereFilesCount applies the entql int predicate on the files_count field. +func (f *UserFilter) WhereFilesCount(p entql.IntP) { + f.Where(p.Field(user.FieldFilesCount)) +} + // WhereHasCard applies a predicate to check if query has an edge card. func (f *UserFilter) WhereHasCard() { f.Where(entql.HasEdge("card")) diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index 2063b8225a..bfafc02ad1 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -441,6 +441,7 @@ var ( {Name: "role", Type: field.TypeEnum, Enums: []string{"user", "admin", "free-user", "test user"}, Default: "user"}, {Name: "employment", Type: field.TypeEnum, Enums: []string{"Full-Time", "Part-Time", "Contract"}, Default: "Full-Time"}, {Name: "sso_cert", Type: field.TypeString, Nullable: true}, + {Name: "files_count", Type: field.TypeInt, Nullable: true}, {Name: "group_blocked", Type: field.TypeInt, Nullable: true}, {Name: "user_spouse", Type: field.TypeInt, Unique: true, Nullable: true}, {Name: "user_parent", Type: field.TypeInt, Nullable: true}, @@ -453,19 +454,19 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "users_groups_blocked", - Columns: []*schema.Column{UsersColumns[12]}, + Columns: []*schema.Column{UsersColumns[13]}, RefColumns: []*schema.Column{GroupsColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "users_users_spouse", - Columns: []*schema.Column{UsersColumns[13]}, + Columns: []*schema.Column{UsersColumns[14]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "users_users_parent", - Columns: []*schema.Column{UsersColumns[14]}, + Columns: []*schema.Column{UsersColumns[15]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, }, diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 21cb1a85e8..e0aaa845c8 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -14964,6 +14964,8 @@ type UserMutation struct { role *user.Role employment *user.Employment _SSOCert *string + files_count *int + addfiles_count *int clearedFields map[string]struct{} card *int clearedcard bool @@ -15612,6 +15614,76 @@ func (m *UserMutation) ResetSSOCert() { delete(m.clearedFields, user.FieldSSOCert) } +// SetFilesCount sets the "files_count" field. +func (m *UserMutation) SetFilesCount(i int) { + m.files_count = &i + m.addfiles_count = nil +} + +// FilesCount returns the value of the "files_count" field in the mutation. +func (m *UserMutation) FilesCount() (r int, exists bool) { + v := m.files_count + if v == nil { + return + } + return *v, true +} + +// OldFilesCount returns the old "files_count" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldFilesCount(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldFilesCount is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldFilesCount requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldFilesCount: %w", err) + } + return oldValue.FilesCount, nil +} + +// AddFilesCount adds i to the "files_count" field. +func (m *UserMutation) AddFilesCount(i int) { + if m.addfiles_count != nil { + *m.addfiles_count += i + } else { + m.addfiles_count = &i + } +} + +// AddedFilesCount returns the value that was added to the "files_count" field in this mutation. +func (m *UserMutation) AddedFilesCount() (r int, exists bool) { + v := m.addfiles_count + if v == nil { + return + } + return *v, true +} + +// ClearFilesCount clears the value of the "files_count" field. +func (m *UserMutation) ClearFilesCount() { + m.files_count = nil + m.addfiles_count = nil + m.clearedFields[user.FieldFilesCount] = struct{}{} +} + +// FilesCountCleared returns if the "files_count" field was cleared in this mutation. +func (m *UserMutation) FilesCountCleared() bool { + _, ok := m.clearedFields[user.FieldFilesCount] + return ok +} + +// ResetFilesCount resets all changes to the "files_count" field. +func (m *UserMutation) ResetFilesCount() { + m.files_count = nil + m.addfiles_count = nil + delete(m.clearedFields, user.FieldFilesCount) +} + // SetCardID sets the "card" edge to the Card entity by id. func (m *UserMutation) SetCardID(id int) { m.card = &id @@ -16180,7 +16252,7 @@ func (m *UserMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *UserMutation) Fields() []string { - fields := make([]string, 0, 11) + fields := make([]string, 0, 12) if m.optional_int != nil { fields = append(fields, user.FieldOptionalInt) } @@ -16214,6 +16286,9 @@ func (m *UserMutation) Fields() []string { if m._SSOCert != nil { fields = append(fields, user.FieldSSOCert) } + if m.files_count != nil { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16244,6 +16319,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { return m.Employment() case user.FieldSSOCert: return m.SSOCert() + case user.FieldFilesCount: + return m.FilesCount() } return nil, false } @@ -16275,6 +16352,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldEmployment(ctx) case user.FieldSSOCert: return m.OldSSOCert(ctx) + case user.FieldFilesCount: + return m.OldFilesCount(ctx) } return nil, fmt.Errorf("unknown User field %s", name) } @@ -16361,6 +16440,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetSSOCert(v) return nil + case user.FieldFilesCount: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetFilesCount(v) + return nil } return fmt.Errorf("unknown User field %s", name) } @@ -16375,6 +16461,9 @@ func (m *UserMutation) AddedFields() []string { if m.addage != nil { fields = append(fields, user.FieldAge) } + if m.addfiles_count != nil { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16387,6 +16476,8 @@ func (m *UserMutation) AddedField(name string) (ent.Value, bool) { return m.AddedOptionalInt() case user.FieldAge: return m.AddedAge() + case user.FieldFilesCount: + return m.AddedFilesCount() } return nil, false } @@ -16410,6 +16501,13 @@ func (m *UserMutation) AddField(name string, value ent.Value) error { } m.AddAge(v) return nil + case user.FieldFilesCount: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddFilesCount(v) + return nil } return fmt.Errorf("unknown User numeric field %s", name) } @@ -16436,6 +16534,9 @@ func (m *UserMutation) ClearedFields() []string { if m.FieldCleared(user.FieldSSOCert) { fields = append(fields, user.FieldSSOCert) } + if m.FieldCleared(user.FieldFilesCount) { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16468,6 +16569,9 @@ func (m *UserMutation) ClearField(name string) error { case user.FieldSSOCert: m.ClearSSOCert() return nil + case user.FieldFilesCount: + m.ClearFilesCount() + return nil } return fmt.Errorf("unknown User nullable field %s", name) } @@ -16509,6 +16613,9 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldSSOCert: m.ResetSSOCert() return nil + case user.FieldFilesCount: + m.ResetFilesCount() + return nil } return fmt.Errorf("unknown User field %s", name) } diff --git a/entc/integration/ent/schema/user.go b/entc/integration/ent/schema/user.go index 559b65e940..4640eda511 100644 --- a/entc/integration/ent/schema/user.go +++ b/entc/integration/ent/schema/user.go @@ -51,6 +51,10 @@ func (User) Fields() []ent.Field { Default("Full-Time"), field.String("SSOCert"). Optional(), + // Some users store the associations + // count as a separate field. + field.Int("files_count"). + Optional(), } } diff --git a/entc/integration/ent/user.go b/entc/integration/ent/user.go index 5ab28bb5c3..11e627d40b 100644 --- a/entc/integration/ent/user.go +++ b/entc/integration/ent/user.go @@ -44,6 +44,8 @@ type User struct { Employment user.Employment `json:"employment,omitempty"` // SSOCert holds the value of the "SSOCert" field. SSOCert string `json:"SSOCert,omitempty"` + // FilesCount holds the value of the "files_count" field. + FilesCount int `json:"files_count,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the UserQuery when eager-loading is set. Edges UserEdges `json:"edges"` @@ -209,7 +211,7 @@ func (*User) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case user.FieldID, user.FieldOptionalInt, user.FieldAge: + case user.FieldID, user.FieldOptionalInt, user.FieldAge, user.FieldFilesCount: values[i] = new(sql.NullInt64) case user.FieldName, user.FieldLast, user.FieldNickname, user.FieldAddress, user.FieldPhone, user.FieldPassword, user.FieldRole, user.FieldEmployment, user.FieldSSOCert: values[i] = new(sql.NullString) @@ -306,6 +308,12 @@ func (u *User) assignValues(columns []string, values []any) error { } else if value.Valid { u.SSOCert = value.String } + case user.FieldFilesCount: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field files_count", values[i]) + } else if value.Valid { + u.FilesCount = int(value.Int64) + } case user.ForeignKeys[0]: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for edge-field group_blocked", value) @@ -449,6 +457,9 @@ func (u *User) String() string { builder.WriteString(", ") builder.WriteString("SSOCert=") builder.WriteString(u.SSOCert) + builder.WriteString(", ") + builder.WriteString("files_count=") + builder.WriteString(fmt.Sprintf("%v", u.FilesCount)) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/ent/user/user.go b/entc/integration/ent/user/user.go index b54944b32b..3e64fe6141 100644 --- a/entc/integration/ent/user/user.go +++ b/entc/integration/ent/user/user.go @@ -40,6 +40,8 @@ const ( FieldEmployment = "employment" // FieldSSOCert holds the string denoting the ssocert field in the database. FieldSSOCert = "sso_cert" + // FieldFilesCount holds the string denoting the files_count field in the database. + FieldFilesCount = "files_count" // EdgeCard holds the string denoting the card edge name in mutations. EdgeCard = "card" // EdgePets holds the string denoting the pets edge name in mutations. @@ -131,6 +133,7 @@ var Columns = []string{ FieldRole, FieldEmployment, FieldSSOCert, + FieldFilesCount, } // ForeignKeys holds the SQL foreign-keys that are owned by the "users" @@ -298,6 +301,11 @@ func BySSOCert(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldSSOCert, opts...).ToFunc() } +// ByFilesCountField orders the results by the files_count field. +func ByFilesCountField(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldFilesCount, opts...).ToFunc() +} + // ByCardField orders the results by card field. func ByCardField(field string, opts ...sql.OrderTermOption) OrderOption { return func(s *sql.Selector) { diff --git a/entc/integration/ent/user/where.go b/entc/integration/ent/user/where.go index 9b8abfb36c..73c28b1cf7 100644 --- a/entc/integration/ent/user/where.go +++ b/entc/integration/ent/user/where.go @@ -102,6 +102,11 @@ func SSOCert(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldSSOCert, v)) } +// FilesCount applies equality check predicate on the "files_count" field. It's identical to FilesCountEQ. +func FilesCount(v int) predicate.User { + return predicate.User(sql.FieldEQ(FieldFilesCount, v)) +} + // OptionalIntEQ applies the EQ predicate on the "optional_int" field. func OptionalIntEQ(v int) predicate.User { return predicate.User(sql.FieldEQ(FieldOptionalInt, v)) @@ -737,6 +742,56 @@ func SSOCertContainsFold(v string) predicate.User { return predicate.User(sql.FieldContainsFold(FieldSSOCert, v)) } +// FilesCountEQ applies the EQ predicate on the "files_count" field. +func FilesCountEQ(v int) predicate.User { + return predicate.User(sql.FieldEQ(FieldFilesCount, v)) +} + +// FilesCountNEQ applies the NEQ predicate on the "files_count" field. +func FilesCountNEQ(v int) predicate.User { + return predicate.User(sql.FieldNEQ(FieldFilesCount, v)) +} + +// FilesCountIn applies the In predicate on the "files_count" field. +func FilesCountIn(vs ...int) predicate.User { + return predicate.User(sql.FieldIn(FieldFilesCount, vs...)) +} + +// FilesCountNotIn applies the NotIn predicate on the "files_count" field. +func FilesCountNotIn(vs ...int) predicate.User { + return predicate.User(sql.FieldNotIn(FieldFilesCount, vs...)) +} + +// FilesCountGT applies the GT predicate on the "files_count" field. +func FilesCountGT(v int) predicate.User { + return predicate.User(sql.FieldGT(FieldFilesCount, v)) +} + +// FilesCountGTE applies the GTE predicate on the "files_count" field. +func FilesCountGTE(v int) predicate.User { + return predicate.User(sql.FieldGTE(FieldFilesCount, v)) +} + +// FilesCountLT applies the LT predicate on the "files_count" field. +func FilesCountLT(v int) predicate.User { + return predicate.User(sql.FieldLT(FieldFilesCount, v)) +} + +// FilesCountLTE applies the LTE predicate on the "files_count" field. +func FilesCountLTE(v int) predicate.User { + return predicate.User(sql.FieldLTE(FieldFilesCount, v)) +} + +// FilesCountIsNil applies the IsNil predicate on the "files_count" field. +func FilesCountIsNil() predicate.User { + return predicate.User(sql.FieldIsNull(FieldFilesCount)) +} + +// FilesCountNotNil applies the NotNil predicate on the "files_count" field. +func FilesCountNotNil() predicate.User { + return predicate.User(sql.FieldNotNull(FieldFilesCount)) +} + // HasCard applies the HasEdge predicate on the "card" edge. func HasCard() predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/entc/integration/ent/user_create.go b/entc/integration/ent/user_create.go index e5db8c62a9..7ee77fee2e 100644 --- a/entc/integration/ent/user_create.go +++ b/entc/integration/ent/user_create.go @@ -167,6 +167,20 @@ func (uc *UserCreate) SetNillableSSOCert(s *string) *UserCreate { return uc } +// SetFilesCount sets the "files_count" field. +func (uc *UserCreate) SetFilesCount(i int) *UserCreate { + uc.mutation.SetFilesCount(i) + return uc +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uc *UserCreate) SetNillableFilesCount(i *int) *UserCreate { + if i != nil { + uc.SetFilesCount(*i) + } + return uc +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uc *UserCreate) SetCardID(id int) *UserCreate { uc.mutation.SetCardID(id) @@ -504,6 +518,10 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { _spec.SetField(user.FieldSSOCert, field.TypeString, value) _node.SSOCert = value } + if value, ok := uc.mutation.FilesCount(); ok { + _spec.SetField(user.FieldFilesCount, field.TypeInt, value) + _node.FilesCount = value + } if nodes := uc.mutation.CardIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, @@ -914,6 +932,30 @@ func (u *UserUpsert) ClearSSOCert() *UserUpsert { return u } +// SetFilesCount sets the "files_count" field. +func (u *UserUpsert) SetFilesCount(v int) *UserUpsert { + u.Set(user.FieldFilesCount, v) + return u +} + +// UpdateFilesCount sets the "files_count" field to the value that was provided on create. +func (u *UserUpsert) UpdateFilesCount() *UserUpsert { + u.SetExcluded(user.FieldFilesCount) + return u +} + +// AddFilesCount adds v to the "files_count" field. +func (u *UserUpsert) AddFilesCount(v int) *UserUpsert { + u.Add(user.FieldFilesCount, v) + return u +} + +// ClearFilesCount clears the value of the "files_count" field. +func (u *UserUpsert) ClearFilesCount() *UserUpsert { + u.SetNull(user.FieldFilesCount) + return u +} + // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // @@ -1164,6 +1206,34 @@ func (u *UserUpsertOne) ClearSSOCert() *UserUpsertOne { }) } +// SetFilesCount sets the "files_count" field. +func (u *UserUpsertOne) SetFilesCount(v int) *UserUpsertOne { + return u.Update(func(s *UserUpsert) { + s.SetFilesCount(v) + }) +} + +// AddFilesCount adds v to the "files_count" field. +func (u *UserUpsertOne) AddFilesCount(v int) *UserUpsertOne { + return u.Update(func(s *UserUpsert) { + s.AddFilesCount(v) + }) +} + +// UpdateFilesCount sets the "files_count" field to the value that was provided on create. +func (u *UserUpsertOne) UpdateFilesCount() *UserUpsertOne { + return u.Update(func(s *UserUpsert) { + s.UpdateFilesCount() + }) +} + +// ClearFilesCount clears the value of the "files_count" field. +func (u *UserUpsertOne) ClearFilesCount() *UserUpsertOne { + return u.Update(func(s *UserUpsert) { + s.ClearFilesCount() + }) +} + // Exec executes the query. func (u *UserUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { @@ -1574,6 +1644,34 @@ func (u *UserUpsertBulk) ClearSSOCert() *UserUpsertBulk { }) } +// SetFilesCount sets the "files_count" field. +func (u *UserUpsertBulk) SetFilesCount(v int) *UserUpsertBulk { + return u.Update(func(s *UserUpsert) { + s.SetFilesCount(v) + }) +} + +// AddFilesCount adds v to the "files_count" field. +func (u *UserUpsertBulk) AddFilesCount(v int) *UserUpsertBulk { + return u.Update(func(s *UserUpsert) { + s.AddFilesCount(v) + }) +} + +// UpdateFilesCount sets the "files_count" field to the value that was provided on create. +func (u *UserUpsertBulk) UpdateFilesCount() *UserUpsertBulk { + return u.Update(func(s *UserUpsert) { + s.UpdateFilesCount() + }) +} + +// ClearFilesCount clears the value of the "files_count" field. +func (u *UserUpsertBulk) ClearFilesCount() *UserUpsertBulk { + return u.Update(func(s *UserUpsert) { + s.ClearFilesCount() + }) +} + // Exec executes the query. func (u *UserUpsertBulk) Exec(ctx context.Context) error { for i, b := range u.create.builders { diff --git a/entc/integration/ent/user_update.go b/entc/integration/ent/user_update.go index a6c7cb0502..d8ad856c15 100644 --- a/entc/integration/ent/user_update.go +++ b/entc/integration/ent/user_update.go @@ -224,6 +224,33 @@ func (uu *UserUpdate) ClearSSOCert() *UserUpdate { return uu } +// SetFilesCount sets the "files_count" field. +func (uu *UserUpdate) SetFilesCount(i int) *UserUpdate { + uu.mutation.ResetFilesCount() + uu.mutation.SetFilesCount(i) + return uu +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uu *UserUpdate) SetNillableFilesCount(i *int) *UserUpdate { + if i != nil { + uu.SetFilesCount(*i) + } + return uu +} + +// AddFilesCount adds i to the "files_count" field. +func (uu *UserUpdate) AddFilesCount(i int) *UserUpdate { + uu.mutation.AddFilesCount(i) + return uu +} + +// ClearFilesCount clears the value of the "files_count" field. +func (uu *UserUpdate) ClearFilesCount() *UserUpdate { + uu.mutation.ClearFilesCount() + return uu +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uu *UserUpdate) SetCardID(id int) *UserUpdate { uu.mutation.SetCardID(id) @@ -703,6 +730,15 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { if uu.mutation.SSOCertCleared() { _spec.ClearField(user.FieldSSOCert, field.TypeString) } + if value, ok := uu.mutation.FilesCount(); ok { + _spec.SetField(user.FieldFilesCount, field.TypeInt, value) + } + if value, ok := uu.mutation.AddedFilesCount(); ok { + _spec.AddField(user.FieldFilesCount, field.TypeInt, value) + } + if uu.mutation.FilesCountCleared() { + _spec.ClearField(user.FieldFilesCount, field.TypeInt) + } if uu.mutation.CardCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, @@ -1344,6 +1380,33 @@ func (uuo *UserUpdateOne) ClearSSOCert() *UserUpdateOne { return uuo } +// SetFilesCount sets the "files_count" field. +func (uuo *UserUpdateOne) SetFilesCount(i int) *UserUpdateOne { + uuo.mutation.ResetFilesCount() + uuo.mutation.SetFilesCount(i) + return uuo +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableFilesCount(i *int) *UserUpdateOne { + if i != nil { + uuo.SetFilesCount(*i) + } + return uuo +} + +// AddFilesCount adds i to the "files_count" field. +func (uuo *UserUpdateOne) AddFilesCount(i int) *UserUpdateOne { + uuo.mutation.AddFilesCount(i) + return uuo +} + +// ClearFilesCount clears the value of the "files_count" field. +func (uuo *UserUpdateOne) ClearFilesCount() *UserUpdateOne { + uuo.mutation.ClearFilesCount() + return uuo +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uuo *UserUpdateOne) SetCardID(id int) *UserUpdateOne { uuo.mutation.SetCardID(id) @@ -1853,6 +1916,15 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) if uuo.mutation.SSOCertCleared() { _spec.ClearField(user.FieldSSOCert, field.TypeString) } + if value, ok := uuo.mutation.FilesCount(); ok { + _spec.SetField(user.FieldFilesCount, field.TypeInt, value) + } + if value, ok := uuo.mutation.AddedFilesCount(); ok { + _spec.AddField(user.FieldFilesCount, field.TypeInt, value) + } + if uuo.mutation.FilesCountCleared() { + _spec.ClearField(user.FieldFilesCount, field.TypeInt) + } if uuo.mutation.CardCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index f0c9147ad1..a710b2cf40 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -14965,6 +14965,8 @@ type UserMutation struct { role *user.Role employment *user.Employment _SSOCert *string + files_count *int + addfiles_count *int clearedFields map[string]struct{} card *string clearedcard bool @@ -15613,6 +15615,76 @@ func (m *UserMutation) ResetSSOCert() { delete(m.clearedFields, user.FieldSSOCert) } +// SetFilesCount sets the "files_count" field. +func (m *UserMutation) SetFilesCount(i int) { + m.files_count = &i + m.addfiles_count = nil +} + +// FilesCount returns the value of the "files_count" field in the mutation. +func (m *UserMutation) FilesCount() (r int, exists bool) { + v := m.files_count + if v == nil { + return + } + return *v, true +} + +// OldFilesCount returns the old "files_count" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldFilesCount(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldFilesCount is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldFilesCount requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldFilesCount: %w", err) + } + return oldValue.FilesCount, nil +} + +// AddFilesCount adds i to the "files_count" field. +func (m *UserMutation) AddFilesCount(i int) { + if m.addfiles_count != nil { + *m.addfiles_count += i + } else { + m.addfiles_count = &i + } +} + +// AddedFilesCount returns the value that was added to the "files_count" field in this mutation. +func (m *UserMutation) AddedFilesCount() (r int, exists bool) { + v := m.addfiles_count + if v == nil { + return + } + return *v, true +} + +// ClearFilesCount clears the value of the "files_count" field. +func (m *UserMutation) ClearFilesCount() { + m.files_count = nil + m.addfiles_count = nil + m.clearedFields[user.FieldFilesCount] = struct{}{} +} + +// FilesCountCleared returns if the "files_count" field was cleared in this mutation. +func (m *UserMutation) FilesCountCleared() bool { + _, ok := m.clearedFields[user.FieldFilesCount] + return ok +} + +// ResetFilesCount resets all changes to the "files_count" field. +func (m *UserMutation) ResetFilesCount() { + m.files_count = nil + m.addfiles_count = nil + delete(m.clearedFields, user.FieldFilesCount) +} + // SetCardID sets the "card" edge to the Card entity by id. func (m *UserMutation) SetCardID(id string) { m.card = &id @@ -16181,7 +16253,7 @@ func (m *UserMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *UserMutation) Fields() []string { - fields := make([]string, 0, 11) + fields := make([]string, 0, 12) if m.optional_int != nil { fields = append(fields, user.FieldOptionalInt) } @@ -16215,6 +16287,9 @@ func (m *UserMutation) Fields() []string { if m._SSOCert != nil { fields = append(fields, user.FieldSSOCert) } + if m.files_count != nil { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16245,6 +16320,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { return m.Employment() case user.FieldSSOCert: return m.SSOCert() + case user.FieldFilesCount: + return m.FilesCount() } return nil, false } @@ -16276,6 +16353,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldEmployment(ctx) case user.FieldSSOCert: return m.OldSSOCert(ctx) + case user.FieldFilesCount: + return m.OldFilesCount(ctx) } return nil, fmt.Errorf("unknown User field %s", name) } @@ -16362,6 +16441,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetSSOCert(v) return nil + case user.FieldFilesCount: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetFilesCount(v) + return nil } return fmt.Errorf("unknown User field %s", name) } @@ -16376,6 +16462,9 @@ func (m *UserMutation) AddedFields() []string { if m.addage != nil { fields = append(fields, user.FieldAge) } + if m.addfiles_count != nil { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16388,6 +16477,8 @@ func (m *UserMutation) AddedField(name string) (ent.Value, bool) { return m.AddedOptionalInt() case user.FieldAge: return m.AddedAge() + case user.FieldFilesCount: + return m.AddedFilesCount() } return nil, false } @@ -16411,6 +16502,13 @@ func (m *UserMutation) AddField(name string, value ent.Value) error { } m.AddAge(v) return nil + case user.FieldFilesCount: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddFilesCount(v) + return nil } return fmt.Errorf("unknown User numeric field %s", name) } @@ -16437,6 +16535,9 @@ func (m *UserMutation) ClearedFields() []string { if m.FieldCleared(user.FieldSSOCert) { fields = append(fields, user.FieldSSOCert) } + if m.FieldCleared(user.FieldFilesCount) { + fields = append(fields, user.FieldFilesCount) + } return fields } @@ -16469,6 +16570,9 @@ func (m *UserMutation) ClearField(name string) error { case user.FieldSSOCert: m.ClearSSOCert() return nil + case user.FieldFilesCount: + m.ClearFilesCount() + return nil } return fmt.Errorf("unknown User nullable field %s", name) } @@ -16510,6 +16614,9 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldSSOCert: m.ResetSSOCert() return nil + case user.FieldFilesCount: + m.ResetFilesCount() + return nil } return fmt.Errorf("unknown User field %s", name) } diff --git a/entc/integration/gremlin/ent/user.go b/entc/integration/gremlin/ent/user.go index 275ed322d3..e3c0339645 100644 --- a/entc/integration/gremlin/ent/user.go +++ b/entc/integration/gremlin/ent/user.go @@ -43,6 +43,8 @@ type User struct { Employment user.Employment `json:"employment,omitempty"` // SSOCert holds the value of the "SSOCert" field. SSOCert string `json:"SSOCert,omitempty"` + // FilesCount holds the value of the "files_count" field. + FilesCount int `json:"files_count,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the UserQuery when eager-loading is set. Edges UserEdges `json:"edges"` @@ -211,6 +213,7 @@ func (u *User) FromResponse(res *gremlin.Response) error { Role user.Role `json:"role,omitempty"` Employment user.Employment `json:"employment,omitempty"` SSOCert string `json:"sso_cert,omitempty"` + FilesCount int `json:"files_count,omitempty"` } if err := vmap.Decode(&scanu); err != nil { return err @@ -227,6 +230,7 @@ func (u *User) FromResponse(res *gremlin.Response) error { u.Role = scanu.Role u.Employment = scanu.Employment u.SSOCert = scanu.SSOCert + u.FilesCount = scanu.FilesCount return nil } @@ -339,6 +343,9 @@ func (u *User) String() string { builder.WriteString(", ") builder.WriteString("SSOCert=") builder.WriteString(u.SSOCert) + builder.WriteString(", ") + builder.WriteString("files_count=") + builder.WriteString(fmt.Sprintf("%v", u.FilesCount)) builder.WriteByte(')') return builder.String() } @@ -365,6 +372,7 @@ func (u *Users) FromResponse(res *gremlin.Response) error { Role user.Role `json:"role,omitempty"` Employment user.Employment `json:"employment,omitempty"` SSOCert string `json:"sso_cert,omitempty"` + FilesCount int `json:"files_count,omitempty"` } if err := vmap.Decode(&scanu); err != nil { return err @@ -382,6 +390,7 @@ func (u *Users) FromResponse(res *gremlin.Response) error { node.Role = v.Role node.Employment = v.Employment node.SSOCert = v.SSOCert + node.FilesCount = v.FilesCount *u = append(*u, node) } return nil diff --git a/entc/integration/gremlin/ent/user/user.go b/entc/integration/gremlin/ent/user/user.go index e73a99b01c..a1b65d73d3 100644 --- a/entc/integration/gremlin/ent/user/user.go +++ b/entc/integration/gremlin/ent/user/user.go @@ -39,6 +39,8 @@ const ( FieldEmployment = "employment" // FieldSSOCert holds the string denoting the ssocert field in the database. FieldSSOCert = "sso_cert" + // FieldFilesCount holds the string denoting the files_count field in the database. + FieldFilesCount = "files_count" // EdgeCard holds the string denoting the card edge name in mutations. EdgeCard = "card" // EdgePets holds the string denoting the pets edge name in mutations. diff --git a/entc/integration/gremlin/ent/user/where.go b/entc/integration/gremlin/ent/user/where.go index 394800f7d5..aa6740bd2a 100644 --- a/entc/integration/gremlin/ent/user/where.go +++ b/entc/integration/gremlin/ent/user/where.go @@ -147,6 +147,13 @@ func SSOCert(v string) predicate.User { }) } +// FilesCount applies equality check predicate on the "files_count" field. It's identical to FilesCountEQ. +func FilesCount(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.EQ(v)) + }) +} + // OptionalIntEQ applies the EQ predicate on the "optional_int" field. func OptionalIntEQ(v int) predicate.User { return predicate.User(func(t *dsl.Traversal) { @@ -938,6 +945,76 @@ func SSOCertNotNil() predicate.User { }) } +// FilesCountEQ applies the EQ predicate on the "files_count" field. +func FilesCountEQ(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.EQ(v)) + }) +} + +// FilesCountNEQ applies the NEQ predicate on the "files_count" field. +func FilesCountNEQ(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.NEQ(v)) + }) +} + +// FilesCountIn applies the In predicate on the "files_count" field. +func FilesCountIn(vs ...int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.Within(vs...)) + }) +} + +// FilesCountNotIn applies the NotIn predicate on the "files_count" field. +func FilesCountNotIn(vs ...int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.Without(vs...)) + }) +} + +// FilesCountGT applies the GT predicate on the "files_count" field. +func FilesCountGT(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.GT(v)) + }) +} + +// FilesCountGTE applies the GTE predicate on the "files_count" field. +func FilesCountGTE(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.GTE(v)) + }) +} + +// FilesCountLT applies the LT predicate on the "files_count" field. +func FilesCountLT(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.LT(v)) + }) +} + +// FilesCountLTE applies the LTE predicate on the "files_count" field. +func FilesCountLTE(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldFilesCount, p.LTE(v)) + }) +} + +// FilesCountIsNil applies the IsNil predicate on the "files_count" field. +func FilesCountIsNil() predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldFilesCount) + }) +} + +// FilesCountNotNil applies the NotNil predicate on the "files_count" field. +func FilesCountNotNil() predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldFilesCount) + }) +} + // HasCard applies the HasEdge predicate on the "card" edge. func HasCard() predicate.User { return predicate.User(func(t *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/user_create.go b/entc/integration/gremlin/ent/user_create.go index e8ee1ea149..d6dd66a432 100644 --- a/entc/integration/gremlin/ent/user_create.go +++ b/entc/integration/gremlin/ent/user_create.go @@ -164,6 +164,20 @@ func (uc *UserCreate) SetNillableSSOCert(s *string) *UserCreate { return uc } +// SetFilesCount sets the "files_count" field. +func (uc *UserCreate) SetFilesCount(i int) *UserCreate { + uc.mutation.SetFilesCount(i) + return uc +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uc *UserCreate) SetNillableFilesCount(i *int) *UserCreate { + if i != nil { + uc.SetFilesCount(*i) + } + return uc +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uc *UserCreate) SetCardID(id string) *UserCreate { uc.mutation.SetCardID(id) @@ -502,6 +516,9 @@ func (uc *UserCreate) gremlin() *dsl.Traversal { if value, ok := uc.mutation.SSOCert(); ok { v.Property(dsl.Single, user.FieldSSOCert, value) } + if value, ok := uc.mutation.FilesCount(); ok { + v.Property(dsl.Single, user.FieldFilesCount, value) + } for _, id := range uc.mutation.CardIDs() { v.AddE(user.CardLabel).To(g.V(id)).OutV() constraints = append(constraints, &constraint{ diff --git a/entc/integration/gremlin/ent/user_update.go b/entc/integration/gremlin/ent/user_update.go index 72fafb44fb..5b857427f5 100644 --- a/entc/integration/gremlin/ent/user_update.go +++ b/entc/integration/gremlin/ent/user_update.go @@ -221,6 +221,33 @@ func (uu *UserUpdate) ClearSSOCert() *UserUpdate { return uu } +// SetFilesCount sets the "files_count" field. +func (uu *UserUpdate) SetFilesCount(i int) *UserUpdate { + uu.mutation.ResetFilesCount() + uu.mutation.SetFilesCount(i) + return uu +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uu *UserUpdate) SetNillableFilesCount(i *int) *UserUpdate { + if i != nil { + uu.SetFilesCount(*i) + } + return uu +} + +// AddFilesCount adds i to the "files_count" field. +func (uu *UserUpdate) AddFilesCount(i int) *UserUpdate { + uu.mutation.AddFilesCount(i) + return uu +} + +// ClearFilesCount clears the value of the "files_count" field. +func (uu *UserUpdate) ClearFilesCount() *UserUpdate { + uu.mutation.ClearFilesCount() + return uu +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uu *UserUpdate) SetCardID(id string) *UserUpdate { uu.mutation.SetCardID(id) @@ -704,6 +731,12 @@ func (uu *UserUpdate) gremlin() *dsl.Traversal { if value, ok := uu.mutation.SSOCert(); ok { v.Property(dsl.Single, user.FieldSSOCert, value) } + if value, ok := uu.mutation.FilesCount(); ok { + v.Property(dsl.Single, user.FieldFilesCount, value) + } + if value, ok := uu.mutation.AddedFilesCount(); ok { + v.Property(dsl.Single, user.FieldFilesCount, __.Union(__.Values(user.FieldFilesCount), __.Constant(value)).Sum()) + } var properties []any if uu.mutation.OptionalIntCleared() { properties = append(properties, user.FieldOptionalInt) @@ -723,6 +756,9 @@ func (uu *UserUpdate) gremlin() *dsl.Traversal { if uu.mutation.SSOCertCleared() { properties = append(properties, user.FieldSSOCert) } + if uu.mutation.FilesCountCleared() { + properties = append(properties, user.FieldFilesCount) + } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } @@ -1042,6 +1078,33 @@ func (uuo *UserUpdateOne) ClearSSOCert() *UserUpdateOne { return uuo } +// SetFilesCount sets the "files_count" field. +func (uuo *UserUpdateOne) SetFilesCount(i int) *UserUpdateOne { + uuo.mutation.ResetFilesCount() + uuo.mutation.SetFilesCount(i) + return uuo +} + +// SetNillableFilesCount sets the "files_count" field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableFilesCount(i *int) *UserUpdateOne { + if i != nil { + uuo.SetFilesCount(*i) + } + return uuo +} + +// AddFilesCount adds i to the "files_count" field. +func (uuo *UserUpdateOne) AddFilesCount(i int) *UserUpdateOne { + uuo.mutation.AddFilesCount(i) + return uuo +} + +// ClearFilesCount clears the value of the "files_count" field. +func (uuo *UserUpdateOne) ClearFilesCount() *UserUpdateOne { + uuo.mutation.ClearFilesCount() + return uuo +} + // SetCardID sets the "card" edge to the Card entity by ID. func (uuo *UserUpdateOne) SetCardID(id string) *UserUpdateOne { uuo.mutation.SetCardID(id) @@ -1543,6 +1606,12 @@ func (uuo *UserUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := uuo.mutation.SSOCert(); ok { v.Property(dsl.Single, user.FieldSSOCert, value) } + if value, ok := uuo.mutation.FilesCount(); ok { + v.Property(dsl.Single, user.FieldFilesCount, value) + } + if value, ok := uuo.mutation.AddedFilesCount(); ok { + v.Property(dsl.Single, user.FieldFilesCount, __.Union(__.Values(user.FieldFilesCount), __.Constant(value)).Sum()) + } var properties []any if uuo.mutation.OptionalIntCleared() { properties = append(properties, user.FieldOptionalInt) @@ -1562,6 +1631,9 @@ func (uuo *UserUpdateOne) gremlin(id string) *dsl.Traversal { if uuo.mutation.SSOCertCleared() { properties = append(properties, user.FieldSSOCert) } + if uuo.mutation.FilesCountCleared() { + properties = append(properties, user.FieldFilesCount) + } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) }