From 50a065dc5dce0e38c5f98869bccbfe678b97097a Mon Sep 17 00:00:00 2001 From: bui Date: Fri, 8 Oct 2021 15:48:36 +0200 Subject: [PATCH] update machine schema to have last_push field --- pkg/database/ent/machine.go | 12 ++++- pkg/database/ent/machine/machine.go | 5 ++ pkg/database/ent/machine/where.go | 83 +++++++++++++++++++++++++++++ pkg/database/ent/machine_create.go | 29 ++++++++++ pkg/database/ent/machine_update.go | 42 +++++++++++++++ pkg/database/ent/migrate/schema.go | 1 + pkg/database/ent/mutation.go | 56 ++++++++++++++++++- pkg/database/ent/runtime.go | 8 ++- pkg/database/ent/schema/machine.go | 2 + 9 files changed, 234 insertions(+), 4 deletions(-) diff --git a/pkg/database/ent/machine.go b/pkg/database/ent/machine.go index f84b2d4b8..73466f1f8 100644 --- a/pkg/database/ent/machine.go +++ b/pkg/database/ent/machine.go @@ -20,6 +20,8 @@ type Machine struct { CreatedAt time.Time `json:"created_at,omitempty"` // UpdatedAt holds the value of the "updated_at" field. UpdatedAt time.Time `json:"updated_at,omitempty"` + // LastPush holds the value of the "last_push" field. + LastPush time.Time `json:"last_push,omitempty"` // MachineId holds the value of the "machineId" field. MachineId string `json:"machineId,omitempty"` // Password holds the value of the "password" field. @@ -68,7 +70,7 @@ func (*Machine) scanValues(columns []string) ([]interface{}, error) { values[i] = new(sql.NullInt64) case machine.FieldMachineId, machine.FieldPassword, machine.FieldIpAddress, machine.FieldScenarios, machine.FieldVersion, machine.FieldStatus: values[i] = new(sql.NullString) - case machine.FieldCreatedAt, machine.FieldUpdatedAt: + case machine.FieldCreatedAt, machine.FieldUpdatedAt, machine.FieldLastPush: values[i] = new(sql.NullTime) default: return nil, fmt.Errorf("unexpected column %q for type Machine", columns[i]) @@ -103,6 +105,12 @@ func (m *Machine) assignValues(columns []string, values []interface{}) error { } else if value.Valid { m.UpdatedAt = value.Time } + case machine.FieldLastPush: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field last_push", values[i]) + } else if value.Valid { + m.LastPush = value.Time + } case machine.FieldMachineId: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field machineId", values[i]) @@ -182,6 +190,8 @@ func (m *Machine) String() string { builder.WriteString(m.CreatedAt.Format(time.ANSIC)) builder.WriteString(", updated_at=") builder.WriteString(m.UpdatedAt.Format(time.ANSIC)) + builder.WriteString(", last_push=") + builder.WriteString(m.LastPush.Format(time.ANSIC)) builder.WriteString(", machineId=") builder.WriteString(m.MachineId) builder.WriteString(", password=") diff --git a/pkg/database/ent/machine/machine.go b/pkg/database/ent/machine/machine.go index dbd6d0b90..bca842a73 100644 --- a/pkg/database/ent/machine/machine.go +++ b/pkg/database/ent/machine/machine.go @@ -15,6 +15,8 @@ const ( FieldCreatedAt = "created_at" // FieldUpdatedAt holds the string denoting the updated_at field in the database. FieldUpdatedAt = "updated_at" + // FieldLastPush holds the string denoting the last_push field in the database. + FieldLastPush = "last_push" // FieldMachineId holds the string denoting the machineid field in the database. FieldMachineId = "machine_id" // FieldPassword holds the string denoting the password field in the database. @@ -47,6 +49,7 @@ var Columns = []string{ FieldID, FieldCreatedAt, FieldUpdatedAt, + FieldLastPush, FieldMachineId, FieldPassword, FieldIpAddress, @@ -71,6 +74,8 @@ var ( DefaultCreatedAt func() time.Time // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. DefaultUpdatedAt func() time.Time + // DefaultLastPush holds the default value on creation for the "last_push" field. + DefaultLastPush func() time.Time // ScenariosValidator is a validator for the "scenarios" field. It is called by the builders before save. ScenariosValidator func(string) error // DefaultIsValidated holds the default value on creation for the "isValidated" field. diff --git a/pkg/database/ent/machine/where.go b/pkg/database/ent/machine/where.go index 8fa0f8039..c4a05d0b7 100644 --- a/pkg/database/ent/machine/where.go +++ b/pkg/database/ent/machine/where.go @@ -107,6 +107,13 @@ func UpdatedAt(v time.Time) predicate.Machine { }) } +// LastPush applies equality check predicate on the "last_push" field. It's identical to LastPushEQ. +func LastPush(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldLastPush), v)) + }) +} + // MachineId applies equality check predicate on the "machineId" field. It's identical to MachineIdEQ. func MachineId(v string) predicate.Machine { return predicate.Machine(func(s *sql.Selector) { @@ -308,6 +315,82 @@ func UpdatedAtLTE(v time.Time) predicate.Machine { }) } +// LastPushEQ applies the EQ predicate on the "last_push" field. +func LastPushEQ(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldLastPush), v)) + }) +} + +// LastPushNEQ applies the NEQ predicate on the "last_push" field. +func LastPushNEQ(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldLastPush), v)) + }) +} + +// LastPushIn applies the In predicate on the "last_push" field. +func LastPushIn(vs ...time.Time) predicate.Machine { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.Machine(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(FieldLastPush), v...)) + }) +} + +// LastPushNotIn applies the NotIn predicate on the "last_push" field. +func LastPushNotIn(vs ...time.Time) predicate.Machine { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.Machine(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(FieldLastPush), v...)) + }) +} + +// LastPushGT applies the GT predicate on the "last_push" field. +func LastPushGT(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldLastPush), v)) + }) +} + +// LastPushGTE applies the GTE predicate on the "last_push" field. +func LastPushGTE(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldLastPush), v)) + }) +} + +// LastPushLT applies the LT predicate on the "last_push" field. +func LastPushLT(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldLastPush), v)) + }) +} + +// LastPushLTE applies the LTE predicate on the "last_push" field. +func LastPushLTE(v time.Time) predicate.Machine { + return predicate.Machine(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldLastPush), v)) + }) +} + // MachineIdEQ applies the EQ predicate on the "machineId" field. func MachineIdEQ(v string) predicate.Machine { return predicate.Machine(func(s *sql.Selector) { diff --git a/pkg/database/ent/machine_create.go b/pkg/database/ent/machine_create.go index d545091f6..33925b389 100644 --- a/pkg/database/ent/machine_create.go +++ b/pkg/database/ent/machine_create.go @@ -49,6 +49,20 @@ func (mc *MachineCreate) SetNillableUpdatedAt(t *time.Time) *MachineCreate { return mc } +// SetLastPush sets the "last_push" field. +func (mc *MachineCreate) SetLastPush(t time.Time) *MachineCreate { + mc.mutation.SetLastPush(t) + return mc +} + +// SetNillableLastPush sets the "last_push" field if the given value is not nil. +func (mc *MachineCreate) SetNillableLastPush(t *time.Time) *MachineCreate { + if t != nil { + mc.SetLastPush(*t) + } + return mc +} + // SetMachineId sets the "machineId" field. func (mc *MachineCreate) SetMachineId(s string) *MachineCreate { mc.mutation.SetMachineId(s) @@ -217,6 +231,10 @@ func (mc *MachineCreate) defaults() { v := machine.DefaultUpdatedAt() mc.mutation.SetUpdatedAt(v) } + if _, ok := mc.mutation.LastPush(); !ok { + v := machine.DefaultLastPush() + mc.mutation.SetLastPush(v) + } if _, ok := mc.mutation.IsValidated(); !ok { v := machine.DefaultIsValidated mc.mutation.SetIsValidated(v) @@ -231,6 +249,9 @@ func (mc *MachineCreate) check() error { if _, ok := mc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "updated_at"`)} } + if _, ok := mc.mutation.LastPush(); !ok { + return &ValidationError{Name: "last_push", err: errors.New("ent: missing required field \"last_push\"")} + } if _, ok := mc.mutation.MachineId(); !ok { return &ValidationError{Name: "machineId", err: errors.New(`ent: missing required field "machineId"`)} } @@ -291,6 +312,14 @@ func (mc *MachineCreate) createSpec() (*Machine, *sqlgraph.CreateSpec) { }) _node.UpdatedAt = value } + if value, ok := mc.mutation.LastPush(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeTime, + Value: value, + Column: machine.FieldLastPush, + }) + _node.LastPush = value + } if value, ok := mc.mutation.MachineId(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, diff --git a/pkg/database/ent/machine_update.go b/pkg/database/ent/machine_update.go index 4cc103719..54113d4b9 100644 --- a/pkg/database/ent/machine_update.go +++ b/pkg/database/ent/machine_update.go @@ -56,6 +56,20 @@ func (mu *MachineUpdate) SetNillableUpdatedAt(t *time.Time) *MachineUpdate { return mu } +// SetLastPush sets the "last_push" field. +func (mu *MachineUpdate) SetLastPush(t time.Time) *MachineUpdate { + mu.mutation.SetLastPush(t) + return mu +} + +// SetNillableLastPush sets the "last_push" field if the given value is not nil. +func (mu *MachineUpdate) SetNillableLastPush(t *time.Time) *MachineUpdate { + if t != nil { + mu.SetLastPush(*t) + } + return mu +} + // SetMachineId sets the "machineId" field. func (mu *MachineUpdate) SetMachineId(s string) *MachineUpdate { mu.mutation.SetMachineId(s) @@ -291,6 +305,13 @@ func (mu *MachineUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: machine.FieldUpdatedAt, }) } + if value, ok := mu.mutation.LastPush(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeTime, + Value: value, + Column: machine.FieldLastPush, + }) + } if value, ok := mu.mutation.MachineId(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeString, @@ -459,6 +480,20 @@ func (muo *MachineUpdateOne) SetNillableUpdatedAt(t *time.Time) *MachineUpdateOn return muo } +// SetLastPush sets the "last_push" field. +func (muo *MachineUpdateOne) SetLastPush(t time.Time) *MachineUpdateOne { + muo.mutation.SetLastPush(t) + return muo +} + +// SetNillableLastPush sets the "last_push" field if the given value is not nil. +func (muo *MachineUpdateOne) SetNillableLastPush(t *time.Time) *MachineUpdateOne { + if t != nil { + muo.SetLastPush(*t) + } + return muo +} + // SetMachineId sets the "machineId" field. func (muo *MachineUpdateOne) SetMachineId(s string) *MachineUpdateOne { muo.mutation.SetMachineId(s) @@ -718,6 +753,13 @@ func (muo *MachineUpdateOne) sqlSave(ctx context.Context) (_node *Machine, err e Column: machine.FieldUpdatedAt, }) } + if value, ok := muo.mutation.LastPush(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeTime, + Value: value, + Column: machine.FieldLastPush, + }) + } if value, ok := muo.mutation.MachineId(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeString, diff --git a/pkg/database/ent/migrate/schema.go b/pkg/database/ent/migrate/schema.go index 94cad488e..5355909b9 100644 --- a/pkg/database/ent/migrate/schema.go +++ b/pkg/database/ent/migrate/schema.go @@ -137,6 +137,7 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, + {Name: "last_push", Type: field.TypeTime}, {Name: "machine_id", Type: field.TypeString, Unique: true}, {Name: "password", Type: field.TypeString}, {Name: "ip_address", Type: field.TypeString}, diff --git a/pkg/database/ent/mutation.go b/pkg/database/ent/mutation.go index b4804d5f0..7966db171 100644 --- a/pkg/database/ent/mutation.go +++ b/pkg/database/ent/mutation.go @@ -4986,6 +4986,7 @@ type MachineMutation struct { id *int created_at *time.Time updated_at *time.Time + last_push *time.Time machineId *string password *string ipAddress *string @@ -5153,6 +5154,42 @@ func (m *MachineMutation) ResetUpdatedAt() { m.updated_at = nil } +// SetLastPush sets the "last_push" field. +func (m *MachineMutation) SetLastPush(t time.Time) { + m.last_push = &t +} + +// LastPush returns the value of the "last_push" field in the mutation. +func (m *MachineMutation) LastPush() (r time.Time, exists bool) { + v := m.last_push + if v == nil { + return + } + return *v, true +} + +// OldLastPush returns the old "last_push" field's value of the Machine entity. +// If the Machine 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 *MachineMutation) OldLastPush(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldLastPush is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldLastPush requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldLastPush: %w", err) + } + return oldValue.LastPush, nil +} + +// ResetLastPush resets all changes to the "last_push" field. +func (m *MachineMutation) ResetLastPush() { + m.last_push = nil +} + // SetMachineId sets the "machineId" field. func (m *MachineMutation) SetMachineId(s string) { m.machineId = &s @@ -5517,13 +5554,16 @@ func (m *MachineMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *MachineMutation) Fields() []string { - fields := make([]string, 0, 9) + fields := make([]string, 0, 10) if m.created_at != nil { fields = append(fields, machine.FieldCreatedAt) } if m.updated_at != nil { fields = append(fields, machine.FieldUpdatedAt) } + if m.last_push != nil { + fields = append(fields, machine.FieldLastPush) + } if m.machineId != nil { fields = append(fields, machine.FieldMachineId) } @@ -5557,6 +5597,8 @@ func (m *MachineMutation) Field(name string) (ent.Value, bool) { return m.CreatedAt() case machine.FieldUpdatedAt: return m.UpdatedAt() + case machine.FieldLastPush: + return m.LastPush() case machine.FieldMachineId: return m.MachineId() case machine.FieldPassword: @@ -5584,6 +5626,8 @@ func (m *MachineMutation) OldField(ctx context.Context, name string) (ent.Value, return m.OldCreatedAt(ctx) case machine.FieldUpdatedAt: return m.OldUpdatedAt(ctx) + case machine.FieldLastPush: + return m.OldLastPush(ctx) case machine.FieldMachineId: return m.OldMachineId(ctx) case machine.FieldPassword: @@ -5621,6 +5665,13 @@ func (m *MachineMutation) SetField(name string, value ent.Value) error { } m.SetUpdatedAt(v) return nil + case machine.FieldLastPush: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetLastPush(v) + return nil case machine.FieldMachineId: v, ok := value.(string) if !ok { @@ -5746,6 +5797,9 @@ func (m *MachineMutation) ResetField(name string) error { case machine.FieldUpdatedAt: m.ResetUpdatedAt() return nil + case machine.FieldLastPush: + m.ResetLastPush() + return nil case machine.FieldMachineId: m.ResetMachineId() return nil diff --git a/pkg/database/ent/runtime.go b/pkg/database/ent/runtime.go index f6be19afa..dc7594caa 100644 --- a/pkg/database/ent/runtime.go +++ b/pkg/database/ent/runtime.go @@ -112,12 +112,16 @@ func init() { machineDescUpdatedAt := machineFields[1].Descriptor() // machine.DefaultUpdatedAt holds the default value on creation for the updated_at field. machine.DefaultUpdatedAt = machineDescUpdatedAt.Default.(func() time.Time) + // machineDescLastPush is the schema descriptor for last_push field. + machineDescLastPush := machineFields[2].Descriptor() + // machine.DefaultLastPush holds the default value on creation for the last_push field. + machine.DefaultLastPush = machineDescLastPush.Default.(func() time.Time) // machineDescScenarios is the schema descriptor for scenarios field. - machineDescScenarios := machineFields[5].Descriptor() + machineDescScenarios := machineFields[6].Descriptor() // machine.ScenariosValidator is a validator for the "scenarios" field. It is called by the builders before save. machine.ScenariosValidator = machineDescScenarios.Validators[0].(func(string) error) // machineDescIsValidated is the schema descriptor for isValidated field. - machineDescIsValidated := machineFields[7].Descriptor() + machineDescIsValidated := machineFields[8].Descriptor() // machine.DefaultIsValidated holds the default value on creation for the isValidated field. machine.DefaultIsValidated = machineDescIsValidated.Default.(bool) metaFields := schema.Meta{}.Fields() diff --git a/pkg/database/ent/schema/machine.go b/pkg/database/ent/schema/machine.go index 00b731f6d..f301613c7 100644 --- a/pkg/database/ent/schema/machine.go +++ b/pkg/database/ent/schema/machine.go @@ -20,6 +20,8 @@ func (Machine) Fields() []ent.Field { Default(time.Now), field.Time("updated_at"). Default(time.Now), + field.Time("last_push"). + Default(time.Now), field.String("machineId").Unique(), field.String("password").Sensitive(), field.String("ipAddress"),