From 2128d0baeec13927ff1d332b912a39614ad80a46 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Thu, 15 Oct 2020 14:17:32 +0300 Subject: [PATCH] dialect/sql/schema: apply size-check only if it is defined in schema (#855) --- dialect/sql/schema/schema.go | 6 +- .../migrate/entv1/migrate/schema.go | 5 +- entc/integration/migrate/entv1/mutation.go | 76 +++++++++- entc/integration/migrate/entv1/runtime.go | 4 + entc/integration/migrate/entv1/schema/user.go | 3 + entc/integration/migrate/entv1/user.go | 12 +- entc/integration/migrate/entv1/user/user.go | 5 + entc/integration/migrate/entv1/user/where.go | 132 ++++++++++++++++++ entc/integration/migrate/entv1/user_create.go | 27 ++++ entc/integration/migrate/entv1/user_update.go | 76 ++++++++++ .../migrate/entv2/migrate/schema.go | 1 + entc/integration/migrate/entv2/mutation.go | 76 +++++++++- entc/integration/migrate/entv2/schema/user.go | 3 + entc/integration/migrate/entv2/user.go | 10 ++ entc/integration/migrate/entv2/user/user.go | 3 + entc/integration/migrate/entv2/user/where.go | 132 ++++++++++++++++++ entc/integration/migrate/entv2/user_create.go | 22 +++ entc/integration/migrate/entv2/user_update.go | 66 +++++++++ 18 files changed, 653 insertions(+), 6 deletions(-) diff --git a/dialect/sql/schema/schema.go b/dialect/sql/schema/schema.go index e7767cecc..6096200e8 100644 --- a/dialect/sql/schema/schema.go +++ b/dialect/sql/schema/schema.go @@ -165,7 +165,11 @@ func (c *Column) PrimaryKey() bool { return c.Key == PrimaryKey } func (c *Column) ConvertibleTo(d *Column) bool { switch { case c.Type == d.Type: - return c.Size <= d.Size + if c.Size != 0 && d.Size != 0 { + // Types match and have a size constraint. + return c.Size <= d.Size + } + return true case c.IntType() && d.IntType() || c.UintType() && d.UintType(): return c.Type <= d.Type case c.UintType() && d.IntType(): diff --git a/entc/integration/migrate/entv1/migrate/schema.go b/entc/integration/migrate/entv1/migrate/schema.go index 559b0e982..6f198d973 100644 --- a/entc/integration/migrate/entv1/migrate/schema.go +++ b/entc/integration/migrate/entv1/migrate/schema.go @@ -43,6 +43,7 @@ var ( {Name: "blob", Type: field.TypeBytes, Nullable: true, Size: 255}, {Name: "state", Type: field.TypeEnum, Nullable: true, Enums: []string{"logged_in", "logged_out"}}, {Name: "status", Type: field.TypeString, Nullable: true}, + {Name: "workplace", Type: field.TypeString, Nullable: true, Size: 30}, {Name: "user_children", Type: field.TypeInt, Nullable: true}, {Name: "user_spouse", Type: field.TypeInt, Unique: true, Nullable: true}, } @@ -54,14 +55,14 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "users_users_children", - Columns: []*schema.Column{UsersColumns[9]}, + Columns: []*schema.Column{UsersColumns[10]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "users_users_spouse", - Columns: []*schema.Column{UsersColumns[10]}, + Columns: []*schema.Column{UsersColumns[11]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, diff --git a/entc/integration/migrate/entv1/mutation.go b/entc/integration/migrate/entv1/mutation.go index 2ab6bf9d3..cd1e63baf 100644 --- a/entc/integration/migrate/entv1/mutation.go +++ b/entc/integration/migrate/entv1/mutation.go @@ -347,6 +347,7 @@ type UserMutation struct { blob *[]byte state *user.State status *string + workplace *string clearedFields map[string]struct{} parent *int clearedparent bool @@ -828,6 +829,56 @@ func (m *UserMutation) ResetStatus() { delete(m.clearedFields, user.FieldStatus) } +// SetWorkplace sets the workplace field. +func (m *UserMutation) SetWorkplace(s string) { + m.workplace = &s +} + +// Workplace returns the workplace value in the mutation. +func (m *UserMutation) Workplace() (r string, exists bool) { + v := m.workplace + if v == nil { + return + } + return *v, true +} + +// OldWorkplace returns the old workplace value of the User. +// 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 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 allowed only on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldWorkplace requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldWorkplace: %w", err) + } + return oldValue.Workplace, nil +} + +// ClearWorkplace clears the value of workplace. +func (m *UserMutation) ClearWorkplace() { + m.workplace = nil + m.clearedFields[user.FieldWorkplace] = struct{}{} +} + +// WorkplaceCleared returns if the field workplace was cleared in this mutation. +func (m *UserMutation) WorkplaceCleared() bool { + _, ok := m.clearedFields[user.FieldWorkplace] + return ok +} + +// ResetWorkplace reset all changes of the "workplace" field. +func (m *UserMutation) ResetWorkplace() { + m.workplace = nil + delete(m.clearedFields, user.FieldWorkplace) +} + // SetParentID sets the parent edge to User by id. func (m *UserMutation) SetParentID(id int) { m.parent = &id @@ -1012,7 +1063,7 @@ func (m *UserMutation) Type() string { // this mutation. Note that, in order to get all numeric // fields that were in/decremented, call AddedFields(). func (m *UserMutation) Fields() []string { - fields := make([]string, 0, 8) + fields := make([]string, 0, 9) if m.age != nil { fields = append(fields, user.FieldAge) } @@ -1037,6 +1088,9 @@ func (m *UserMutation) Fields() []string { if m.status != nil { fields = append(fields, user.FieldStatus) } + if m.workplace != nil { + fields = append(fields, user.FieldWorkplace) + } return fields } @@ -1061,6 +1115,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { return m.State() case user.FieldStatus: return m.Status() + case user.FieldWorkplace: + return m.Workplace() } return nil, false } @@ -1086,6 +1142,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldState(ctx) case user.FieldStatus: return m.OldStatus(ctx) + case user.FieldWorkplace: + return m.OldWorkplace(ctx) } return nil, fmt.Errorf("unknown User field %s", name) } @@ -1151,6 +1209,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetStatus(v) return nil + case user.FieldWorkplace: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetWorkplace(v) + return nil } return fmt.Errorf("unknown User field %s", name) } @@ -1211,6 +1276,9 @@ func (m *UserMutation) ClearedFields() []string { if m.FieldCleared(user.FieldStatus) { fields = append(fields, user.FieldStatus) } + if m.FieldCleared(user.FieldWorkplace) { + fields = append(fields, user.FieldWorkplace) + } return fields } @@ -1240,6 +1308,9 @@ func (m *UserMutation) ClearField(name string) error { case user.FieldStatus: m.ClearStatus() return nil + case user.FieldWorkplace: + m.ClearWorkplace() + return nil } return fmt.Errorf("unknown User nullable field %s", name) } @@ -1273,6 +1344,9 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldStatus: m.ResetStatus() return nil + case user.FieldWorkplace: + m.ResetWorkplace() + return nil } return fmt.Errorf("unknown User field %s", name) } diff --git a/entc/integration/migrate/entv1/runtime.go b/entc/integration/migrate/entv1/runtime.go index aaad94e05..888d2f42b 100644 --- a/entc/integration/migrate/entv1/runtime.go +++ b/entc/integration/migrate/entv1/runtime.go @@ -21,4 +21,8 @@ func init() { userDescName := userFields[2].Descriptor() // user.NameValidator is a validator for the "name" field. It is called by the builders before save. user.NameValidator = userDescName.Validators[0].(func(string) error) + // userDescWorkplace is the schema descriptor for workplace field. + userDescWorkplace := userFields[9].Descriptor() + // user.WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save. + user.WorkplaceValidator = userDescWorkplace.Validators[0].(func(string) error) } diff --git a/entc/integration/migrate/entv1/schema/user.go b/entc/integration/migrate/entv1/schema/user.go index a363424e5..3212ce9b8 100644 --- a/entc/integration/migrate/entv1/schema/user.go +++ b/entc/integration/migrate/entv1/schema/user.go @@ -38,6 +38,9 @@ func (User) Fields() []ent.Field { Values("logged_in", "logged_out"), field.String("status"). Optional(), + field.String("workplace"). + MaxLen(30). + Optional(), } } diff --git a/entc/integration/migrate/entv1/user.go b/entc/integration/migrate/entv1/user.go index f7a5c25f8..b639a408e 100644 --- a/entc/integration/migrate/entv1/user.go +++ b/entc/integration/migrate/entv1/user.go @@ -36,6 +36,8 @@ type User struct { State user.State `json:"state,omitempty"` // Status holds the value of the "status" field. Status string `json:"status,omitempty"` + // Workplace holds the value of the "workplace" field. + Workplace string `json:"workplace,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"` @@ -121,6 +123,7 @@ func (*User) scanValues() []interface{} { &[]byte{}, // blob &sql.NullString{}, // state &sql.NullString{}, // status + &sql.NullString{}, // workplace } } @@ -184,7 +187,12 @@ func (u *User) assignValues(values ...interface{}) error { } else if value.Valid { u.Status = value.String } - values = values[8:] + if value, ok := values[8].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field workplace", values[8]) + } else if value.Valid { + u.Workplace = value.String + } + values = values[9:] if len(values) == len(user.ForeignKeys) { if value, ok := values[0].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for edge-field user_children", value) @@ -261,6 +269,8 @@ func (u *User) String() string { builder.WriteString(fmt.Sprintf("%v", u.State)) builder.WriteString(", status=") builder.WriteString(u.Status) + builder.WriteString(", workplace=") + builder.WriteString(u.Workplace) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/migrate/entv1/user/user.go b/entc/integration/migrate/entv1/user/user.go index f1fd592b0..1057b95ce 100644 --- a/entc/integration/migrate/entv1/user/user.go +++ b/entc/integration/migrate/entv1/user/user.go @@ -31,6 +31,8 @@ const ( FieldState = "state" // FieldStatus holds the string denoting the status field in the database. FieldStatus = "status" + // FieldWorkplace holds the string denoting the workplace field in the database. + FieldWorkplace = "workplace" // EdgeParent holds the string denoting the parent edge name in mutations. EdgeParent = "parent" @@ -77,6 +79,7 @@ var Columns = []string{ FieldBlob, FieldState, FieldStatus, + FieldWorkplace, } // ForeignKeys holds the SQL foreign-keys that are owned by the User type. @@ -103,6 +106,8 @@ func ValidColumn(column string) bool { var ( // NameValidator is a validator for the "name" field. It is called by the builders before save. NameValidator func(string) error + // WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save. + WorkplaceValidator func(string) error ) // State defines the type for the state enum field. diff --git a/entc/integration/migrate/entv1/user/where.go b/entc/integration/migrate/entv1/user/where.go index c1b94df4a..7ad2c5284 100644 --- a/entc/integration/migrate/entv1/user/where.go +++ b/entc/integration/migrate/entv1/user/where.go @@ -144,6 +144,13 @@ func Status(v string) predicate.User { }) } +// Workplace applies equality check predicate on the "workplace" field. It's identical to WorkplaceEQ. +func Workplace(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldWorkplace), v)) + }) +} + // AgeEQ applies the EQ predicate on the "age" field. func AgeEQ(v int32) predicate.User { return predicate.User(func(s *sql.Selector) { @@ -969,6 +976,131 @@ func StatusContainsFold(v string) predicate.User { }) } +// WorkplaceEQ applies the EQ predicate on the "workplace" field. +func WorkplaceEQ(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceNEQ applies the NEQ predicate on the "workplace" field. +func WorkplaceNEQ(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceIn applies the In predicate on the "workplace" field. +func WorkplaceIn(vs ...string) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.In(s.C(FieldWorkplace), v...)) + }) +} + +// WorkplaceNotIn applies the NotIn predicate on the "workplace" field. +func WorkplaceNotIn(vs ...string) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.NotIn(s.C(FieldWorkplace), v...)) + }) +} + +// WorkplaceGT applies the GT predicate on the "workplace" field. +func WorkplaceGT(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceGTE applies the GTE predicate on the "workplace" field. +func WorkplaceGTE(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceLT applies the LT predicate on the "workplace" field. +func WorkplaceLT(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceLTE applies the LTE predicate on the "workplace" field. +func WorkplaceLTE(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceContains applies the Contains predicate on the "workplace" field. +func WorkplaceContains(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.Contains(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceHasPrefix applies the HasPrefix predicate on the "workplace" field. +func WorkplaceHasPrefix(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.HasPrefix(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceHasSuffix applies the HasSuffix predicate on the "workplace" field. +func WorkplaceHasSuffix(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.HasSuffix(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceIsNil applies the IsNil predicate on the "workplace" field. +func WorkplaceIsNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldWorkplace))) + }) +} + +// WorkplaceNotNil applies the NotNil predicate on the "workplace" field. +func WorkplaceNotNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldWorkplace))) + }) +} + +// WorkplaceEqualFold applies the EqualFold predicate on the "workplace" field. +func WorkplaceEqualFold(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EqualFold(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceContainsFold applies the ContainsFold predicate on the "workplace" field. +func WorkplaceContainsFold(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.ContainsFold(s.C(FieldWorkplace), v)) + }) +} + // HasParent applies the HasEdge predicate on the "parent" edge. func HasParent() predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/entc/integration/migrate/entv1/user_create.go b/entc/integration/migrate/entv1/user_create.go index 01a4a1a4e..153996f61 100644 --- a/entc/integration/migrate/entv1/user_create.go +++ b/entc/integration/migrate/entv1/user_create.go @@ -104,6 +104,20 @@ func (uc *UserCreate) SetNillableStatus(s *string) *UserCreate { return uc } +// SetWorkplace sets the workplace field. +func (uc *UserCreate) SetWorkplace(s string) *UserCreate { + uc.mutation.SetWorkplace(s) + return uc +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uc *UserCreate) SetNillableWorkplace(s *string) *UserCreate { + if s != nil { + uc.SetWorkplace(*s) + } + return uc +} + // SetID sets the id field. func (uc *UserCreate) SetID(i int) *UserCreate { uc.mutation.SetID(i) @@ -252,6 +266,11 @@ func (uc *UserCreate) check() error { return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)} } } + if v, ok := uc.mutation.Workplace(); ok { + if err := user.WorkplaceValidator(v); err != nil { + return &ValidationError{Name: "workplace", err: fmt.Errorf("entv1: validator failed for field \"workplace\": %w", err)} + } + } return nil } @@ -349,6 +368,14 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { }) _node.Status = value } + if value, ok := uc.mutation.Workplace(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + _node.Workplace = value + } if nodes := uc.mutation.ParentIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, diff --git a/entc/integration/migrate/entv1/user_update.go b/entc/integration/migrate/entv1/user_update.go index 91db5866d..566b079fa 100644 --- a/entc/integration/migrate/entv1/user_update.go +++ b/entc/integration/migrate/entv1/user_update.go @@ -148,6 +148,26 @@ func (uu *UserUpdate) ClearStatus() *UserUpdate { return uu } +// SetWorkplace sets the workplace field. +func (uu *UserUpdate) SetWorkplace(s string) *UserUpdate { + uu.mutation.SetWorkplace(s) + return uu +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uu *UserUpdate) SetNillableWorkplace(s *string) *UserUpdate { + if s != nil { + uu.SetWorkplace(*s) + } + return uu +} + +// ClearWorkplace clears the value of workplace. +func (uu *UserUpdate) ClearWorkplace() *UserUpdate { + uu.mutation.ClearWorkplace() + return uu +} + // SetParentID sets the parent edge to User by id. func (uu *UserUpdate) SetParentID(id int) *UserUpdate { uu.mutation.SetParentID(id) @@ -333,6 +353,11 @@ func (uu *UserUpdate) check() error { return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)} } } + if v, ok := uu.mutation.Workplace(); ok { + if err := user.WorkplaceValidator(v); err != nil { + return &ValidationError{Name: "workplace", err: fmt.Errorf("entv1: validator failed for field \"workplace\": %w", err)} + } + } return nil } @@ -447,6 +472,19 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: user.FieldStatus, }) } + if value, ok := uu.mutation.Workplace(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + } + if uu.mutation.WorkplaceCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: user.FieldWorkplace, + }) + } if uu.mutation.ParentCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, @@ -741,6 +779,26 @@ func (uuo *UserUpdateOne) ClearStatus() *UserUpdateOne { return uuo } +// SetWorkplace sets the workplace field. +func (uuo *UserUpdateOne) SetWorkplace(s string) *UserUpdateOne { + uuo.mutation.SetWorkplace(s) + return uuo +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableWorkplace(s *string) *UserUpdateOne { + if s != nil { + uuo.SetWorkplace(*s) + } + return uuo +} + +// ClearWorkplace clears the value of workplace. +func (uuo *UserUpdateOne) ClearWorkplace() *UserUpdateOne { + uuo.mutation.ClearWorkplace() + return uuo +} + // SetParentID sets the parent edge to User by id. func (uuo *UserUpdateOne) SetParentID(id int) *UserUpdateOne { uuo.mutation.SetParentID(id) @@ -926,6 +984,11 @@ func (uuo *UserUpdateOne) check() error { return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)} } } + if v, ok := uuo.mutation.Workplace(); ok { + if err := user.WorkplaceValidator(v); err != nil { + return &ValidationError{Name: "workplace", err: fmt.Errorf("entv1: validator failed for field \"workplace\": %w", err)} + } + } return nil } @@ -1038,6 +1101,19 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Column: user.FieldStatus, }) } + if value, ok := uuo.mutation.Workplace(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + } + if uuo.mutation.WorkplaceCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: user.FieldWorkplace, + }) + } if uuo.mutation.ParentCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, diff --git a/entc/integration/migrate/entv2/migrate/schema.go b/entc/integration/migrate/entv2/migrate/schema.go index 052f3de99..49c85cdf5 100644 --- a/entc/integration/migrate/entv2/migrate/schema.go +++ b/entc/integration/migrate/entv2/migrate/schema.go @@ -98,6 +98,7 @@ var ( {Name: "blob", Type: field.TypeBytes, Nullable: true, Size: 1000}, {Name: "state", Type: field.TypeEnum, Nullable: true, Enums: []string{"logged_in", "logged_out", "online"}}, {Name: "status", Type: field.TypeEnum, Nullable: true, Enums: []string{"done", "pending"}}, + {Name: "workplace", Type: field.TypeString, Nullable: true}, } // UsersTable holds the schema information for the "users" table. UsersTable = &schema.Table{ diff --git a/entc/integration/migrate/entv2/mutation.go b/entc/integration/migrate/entv2/mutation.go index b6cc6dab6..f3f59f49b 100644 --- a/entc/integration/migrate/entv2/mutation.go +++ b/entc/integration/migrate/entv2/mutation.go @@ -1277,6 +1277,7 @@ type UserMutation struct { blob *[]byte state *user.State status *user.Status + workplace *string clearedFields map[string]struct{} car map[int]struct{} removedcar map[int]struct{} @@ -1905,6 +1906,56 @@ func (m *UserMutation) ResetStatus() { delete(m.clearedFields, user.FieldStatus) } +// SetWorkplace sets the workplace field. +func (m *UserMutation) SetWorkplace(s string) { + m.workplace = &s +} + +// Workplace returns the workplace value in the mutation. +func (m *UserMutation) Workplace() (r string, exists bool) { + v := m.workplace + if v == nil { + return + } + return *v, true +} + +// OldWorkplace returns the old workplace value of the User. +// 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 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 allowed only on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldWorkplace requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldWorkplace: %w", err) + } + return oldValue.Workplace, nil +} + +// ClearWorkplace clears the value of workplace. +func (m *UserMutation) ClearWorkplace() { + m.workplace = nil + m.clearedFields[user.FieldWorkplace] = struct{}{} +} + +// WorkplaceCleared returns if the field workplace was cleared in this mutation. +func (m *UserMutation) WorkplaceCleared() bool { + _, ok := m.clearedFields[user.FieldWorkplace] + return ok +} + +// ResetWorkplace reset all changes of the "workplace" field. +func (m *UserMutation) ResetWorkplace() { + m.workplace = nil + delete(m.clearedFields, user.FieldWorkplace) +} + // AddCarIDs adds the car edge to Car by ids. func (m *UserMutation) AddCarIDs(ids ...int) { if m.car == nil { @@ -2064,7 +2115,7 @@ func (m *UserMutation) Type() string { // this mutation. Note that, in order to get all numeric // fields that were in/decremented, call AddedFields(). func (m *UserMutation) Fields() []string { - fields := make([]string, 0, 12) + fields := make([]string, 0, 13) if m.mixed_string != nil { fields = append(fields, user.FieldMixedString) } @@ -2101,6 +2152,9 @@ func (m *UserMutation) Fields() []string { if m.status != nil { fields = append(fields, user.FieldStatus) } + if m.workplace != nil { + fields = append(fields, user.FieldWorkplace) + } return fields } @@ -2133,6 +2187,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { return m.State() case user.FieldStatus: return m.Status() + case user.FieldWorkplace: + return m.Workplace() } return nil, false } @@ -2166,6 +2222,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldState(ctx) case user.FieldStatus: return m.OldStatus(ctx) + case user.FieldWorkplace: + return m.OldWorkplace(ctx) } return nil, fmt.Errorf("unknown User field %s", name) } @@ -2259,6 +2317,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetStatus(v) return nil + case user.FieldWorkplace: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetWorkplace(v) + return nil } return fmt.Errorf("unknown User field %s", name) } @@ -2319,6 +2384,9 @@ func (m *UserMutation) ClearedFields() []string { if m.FieldCleared(user.FieldStatus) { fields = append(fields, user.FieldStatus) } + if m.FieldCleared(user.FieldWorkplace) { + fields = append(fields, user.FieldWorkplace) + } return fields } @@ -2348,6 +2416,9 @@ func (m *UserMutation) ClearField(name string) error { case user.FieldStatus: m.ClearStatus() return nil + case user.FieldWorkplace: + m.ClearWorkplace() + return nil } return fmt.Errorf("unknown User nullable field %s", name) } @@ -2393,6 +2464,9 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldStatus: m.ResetStatus() return nil + case user.FieldWorkplace: + m.ResetWorkplace() + return nil } return fmt.Errorf("unknown User field %s", name) } diff --git a/entc/integration/migrate/entv2/schema/user.go b/entc/integration/migrate/entv2/schema/user.go index 118fa6a85..e2a5a9458 100644 --- a/entc/integration/migrate/entv2/schema/user.go +++ b/entc/integration/migrate/entv2/schema/user.go @@ -76,6 +76,9 @@ func (User) Fields() []ent.Field { field.Enum("status"). Optional(). Values("done", "pending"), + // remove the max-length constraint from varchar. + field.String("workplace"). + Optional(), // deleting the `address` column. } } diff --git a/entc/integration/migrate/entv2/user.go b/entc/integration/migrate/entv2/user.go index d4f19bb7e..6b5db930d 100644 --- a/entc/integration/migrate/entv2/user.go +++ b/entc/integration/migrate/entv2/user.go @@ -44,6 +44,8 @@ type User struct { State user.State `json:"state,omitempty"` // Status holds the value of the "status" field. Status user.Status `json:"status,omitempty"` + // Workplace holds the value of the "workplace" field. + Workplace string `json:"workplace,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"` @@ -110,6 +112,7 @@ func (*User) scanValues() []interface{} { &[]byte{}, // blob &sql.NullString{}, // state &sql.NullString{}, // status + &sql.NullString{}, // workplace } } @@ -185,6 +188,11 @@ func (u *User) assignValues(values ...interface{}) error { } else if value.Valid { u.Status = user.Status(value.String) } + if value, ok := values[12].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field workplace", values[12]) + } else if value.Valid { + u.Workplace = value.String + } return nil } @@ -250,6 +258,8 @@ func (u *User) String() string { builder.WriteString(fmt.Sprintf("%v", u.State)) builder.WriteString(", status=") builder.WriteString(fmt.Sprintf("%v", u.Status)) + builder.WriteString(", workplace=") + builder.WriteString(u.Workplace) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/migrate/entv2/user/user.go b/entc/integration/migrate/entv2/user/user.go index ee6a53fbd..666f1ec2b 100644 --- a/entc/integration/migrate/entv2/user/user.go +++ b/entc/integration/migrate/entv2/user/user.go @@ -39,6 +39,8 @@ const ( FieldState = "state" // FieldStatus holds the string denoting the status field in the database. FieldStatus = "status" + // FieldWorkplace holds the string denoting the workplace field in the database. + FieldWorkplace = "workplace" // EdgeCar holds the string denoting the car edge name in mutations. EdgeCar = "car" @@ -86,6 +88,7 @@ var Columns = []string{ FieldBlob, FieldState, FieldStatus, + FieldWorkplace, } var ( diff --git a/entc/integration/migrate/entv2/user/where.go b/entc/integration/migrate/entv2/user/where.go index dfba4ef47..2d117f6bf 100644 --- a/entc/integration/migrate/entv2/user/where.go +++ b/entc/integration/migrate/entv2/user/where.go @@ -158,6 +158,13 @@ func Blob(v []byte) predicate.User { }) } +// Workplace applies equality check predicate on the "workplace" field. It's identical to WorkplaceEQ. +func Workplace(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldWorkplace), v)) + }) +} + // MixedStringEQ applies the EQ predicate on the "mixed_string" field. func MixedStringEQ(v string) predicate.User { return predicate.User(func(s *sql.Selector) { @@ -1266,6 +1273,131 @@ func StatusNotNil() predicate.User { }) } +// WorkplaceEQ applies the EQ predicate on the "workplace" field. +func WorkplaceEQ(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceNEQ applies the NEQ predicate on the "workplace" field. +func WorkplaceNEQ(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceIn applies the In predicate on the "workplace" field. +func WorkplaceIn(vs ...string) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.In(s.C(FieldWorkplace), v...)) + }) +} + +// WorkplaceNotIn applies the NotIn predicate on the "workplace" field. +func WorkplaceNotIn(vs ...string) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.NotIn(s.C(FieldWorkplace), v...)) + }) +} + +// WorkplaceGT applies the GT predicate on the "workplace" field. +func WorkplaceGT(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceGTE applies the GTE predicate on the "workplace" field. +func WorkplaceGTE(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceLT applies the LT predicate on the "workplace" field. +func WorkplaceLT(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceLTE applies the LTE predicate on the "workplace" field. +func WorkplaceLTE(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceContains applies the Contains predicate on the "workplace" field. +func WorkplaceContains(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.Contains(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceHasPrefix applies the HasPrefix predicate on the "workplace" field. +func WorkplaceHasPrefix(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.HasPrefix(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceHasSuffix applies the HasSuffix predicate on the "workplace" field. +func WorkplaceHasSuffix(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.HasSuffix(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceIsNil applies the IsNil predicate on the "workplace" field. +func WorkplaceIsNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldWorkplace))) + }) +} + +// WorkplaceNotNil applies the NotNil predicate on the "workplace" field. +func WorkplaceNotNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldWorkplace))) + }) +} + +// WorkplaceEqualFold applies the EqualFold predicate on the "workplace" field. +func WorkplaceEqualFold(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EqualFold(s.C(FieldWorkplace), v)) + }) +} + +// WorkplaceContainsFold applies the ContainsFold predicate on the "workplace" field. +func WorkplaceContainsFold(v string) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.ContainsFold(s.C(FieldWorkplace), v)) + }) +} + // HasCar applies the HasEdge predicate on the "car" edge. func HasCar() predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/entc/integration/migrate/entv2/user_create.go b/entc/integration/migrate/entv2/user_create.go index c82f304d6..d111e0ca3 100644 --- a/entc/integration/migrate/entv2/user_create.go +++ b/entc/integration/migrate/entv2/user_create.go @@ -153,6 +153,20 @@ func (uc *UserCreate) SetNillableStatus(u *user.Status) *UserCreate { return uc } +// SetWorkplace sets the workplace field. +func (uc *UserCreate) SetWorkplace(s string) *UserCreate { + uc.mutation.SetWorkplace(s) + return uc +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uc *UserCreate) SetNillableWorkplace(s *string) *UserCreate { + if s != nil { + uc.SetWorkplace(*s) + } + return uc +} + // SetID sets the id field. func (uc *UserCreate) SetID(i int) *UserCreate { uc.mutation.SetID(i) @@ -450,6 +464,14 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { }) _node.Status = value } + if value, ok := uc.mutation.Workplace(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + _node.Workplace = value + } if nodes := uc.mutation.CarIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, diff --git a/entc/integration/migrate/entv2/user_update.go b/entc/integration/migrate/entv2/user_update.go index 761d86fe6..44a751776 100644 --- a/entc/integration/migrate/entv2/user_update.go +++ b/entc/integration/migrate/entv2/user_update.go @@ -197,6 +197,26 @@ func (uu *UserUpdate) ClearStatus() *UserUpdate { return uu } +// SetWorkplace sets the workplace field. +func (uu *UserUpdate) SetWorkplace(s string) *UserUpdate { + uu.mutation.SetWorkplace(s) + return uu +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uu *UserUpdate) SetNillableWorkplace(s *string) *UserUpdate { + if s != nil { + uu.SetWorkplace(*s) + } + return uu +} + +// ClearWorkplace clears the value of workplace. +func (uu *UserUpdate) ClearWorkplace() *UserUpdate { + uu.mutation.ClearWorkplace() + return uu +} + // AddCarIDs adds the car edge to Car by ids. func (uu *UserUpdate) AddCarIDs(ids ...int) *UserUpdate { uu.mutation.AddCarIDs(ids...) @@ -520,6 +540,19 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: user.FieldStatus, }) } + if value, ok := uu.mutation.Workplace(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + } + if uu.mutation.WorkplaceCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: user.FieldWorkplace, + }) + } if uu.mutation.CarCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, @@ -846,6 +879,26 @@ func (uuo *UserUpdateOne) ClearStatus() *UserUpdateOne { return uuo } +// SetWorkplace sets the workplace field. +func (uuo *UserUpdateOne) SetWorkplace(s string) *UserUpdateOne { + uuo.mutation.SetWorkplace(s) + return uuo +} + +// SetNillableWorkplace sets the workplace field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableWorkplace(s *string) *UserUpdateOne { + if s != nil { + uuo.SetWorkplace(*s) + } + return uuo +} + +// ClearWorkplace clears the value of workplace. +func (uuo *UserUpdateOne) ClearWorkplace() *UserUpdateOne { + uuo.mutation.ClearWorkplace() + return uuo +} + // AddCarIDs adds the car edge to Car by ids. func (uuo *UserUpdateOne) AddCarIDs(ids ...int) *UserUpdateOne { uuo.mutation.AddCarIDs(ids...) @@ -1167,6 +1220,19 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Column: user.FieldStatus, }) } + if value, ok := uuo.mutation.Workplace(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: user.FieldWorkplace, + }) + } + if uuo.mutation.WorkplaceCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Column: user.FieldWorkplace, + }) + } if uuo.mutation.CarCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M,