// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/schema"
)

// MachineUpdate is the builder for updating Machine entities.
type MachineUpdate struct {
	config
	hooks    []Hook
	mutation *MachineMutation
}

// Where appends a list predicates to the MachineUpdate builder.
func (_u *MachineUpdate) Where(ps ...predicate.Machine) *MachineUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *MachineUpdate) SetUpdatedAt(v time.Time) *MachineUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetLastPush sets the "last_push" field.
func (_u *MachineUpdate) SetLastPush(v time.Time) *MachineUpdate {
	_u.mutation.SetLastPush(v)
	return _u
}

// SetNillableLastPush sets the "last_push" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableLastPush(v *time.Time) *MachineUpdate {
	if v != nil {
		_u.SetLastPush(*v)
	}
	return _u
}

// ClearLastPush clears the value of the "last_push" field.
func (_u *MachineUpdate) ClearLastPush() *MachineUpdate {
	_u.mutation.ClearLastPush()
	return _u
}

// SetLastHeartbeat sets the "last_heartbeat" field.
func (_u *MachineUpdate) SetLastHeartbeat(v time.Time) *MachineUpdate {
	_u.mutation.SetLastHeartbeat(v)
	return _u
}

// SetNillableLastHeartbeat sets the "last_heartbeat" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableLastHeartbeat(v *time.Time) *MachineUpdate {
	if v != nil {
		_u.SetLastHeartbeat(*v)
	}
	return _u
}

// ClearLastHeartbeat clears the value of the "last_heartbeat" field.
func (_u *MachineUpdate) ClearLastHeartbeat() *MachineUpdate {
	_u.mutation.ClearLastHeartbeat()
	return _u
}

// SetPassword sets the "password" field.
func (_u *MachineUpdate) SetPassword(v string) *MachineUpdate {
	_u.mutation.SetPassword(v)
	return _u
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *MachineUpdate) SetNillablePassword(v *string) *MachineUpdate {
	if v != nil {
		_u.SetPassword(*v)
	}
	return _u
}

// SetIpAddress sets the "ipAddress" field.
func (_u *MachineUpdate) SetIpAddress(v string) *MachineUpdate {
	_u.mutation.SetIpAddress(v)
	return _u
}

// SetNillableIpAddress sets the "ipAddress" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableIpAddress(v *string) *MachineUpdate {
	if v != nil {
		_u.SetIpAddress(*v)
	}
	return _u
}

// SetScenarios sets the "scenarios" field.
func (_u *MachineUpdate) SetScenarios(v string) *MachineUpdate {
	_u.mutation.SetScenarios(v)
	return _u
}

// SetNillableScenarios sets the "scenarios" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableScenarios(v *string) *MachineUpdate {
	if v != nil {
		_u.SetScenarios(*v)
	}
	return _u
}

// ClearScenarios clears the value of the "scenarios" field.
func (_u *MachineUpdate) ClearScenarios() *MachineUpdate {
	_u.mutation.ClearScenarios()
	return _u
}

// SetVersion sets the "version" field.
func (_u *MachineUpdate) SetVersion(v string) *MachineUpdate {
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableVersion(v *string) *MachineUpdate {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// ClearVersion clears the value of the "version" field.
func (_u *MachineUpdate) ClearVersion() *MachineUpdate {
	_u.mutation.ClearVersion()
	return _u
}

// SetIsValidated sets the "isValidated" field.
func (_u *MachineUpdate) SetIsValidated(v bool) *MachineUpdate {
	_u.mutation.SetIsValidated(v)
	return _u
}

// SetNillableIsValidated sets the "isValidated" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableIsValidated(v *bool) *MachineUpdate {
	if v != nil {
		_u.SetIsValidated(*v)
	}
	return _u
}

// SetAuthType sets the "auth_type" field.
func (_u *MachineUpdate) SetAuthType(v string) *MachineUpdate {
	_u.mutation.SetAuthType(v)
	return _u
}

// SetNillableAuthType sets the "auth_type" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableAuthType(v *string) *MachineUpdate {
	if v != nil {
		_u.SetAuthType(*v)
	}
	return _u
}

// SetOsname sets the "osname" field.
func (_u *MachineUpdate) SetOsname(v string) *MachineUpdate {
	_u.mutation.SetOsname(v)
	return _u
}

// SetNillableOsname sets the "osname" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableOsname(v *string) *MachineUpdate {
	if v != nil {
		_u.SetOsname(*v)
	}
	return _u
}

// ClearOsname clears the value of the "osname" field.
func (_u *MachineUpdate) ClearOsname() *MachineUpdate {
	_u.mutation.ClearOsname()
	return _u
}

// SetOsfamily sets the "osfamily" field.
func (_u *MachineUpdate) SetOsfamily(v string) *MachineUpdate {
	_u.mutation.SetOsfamily(v)
	return _u
}

// SetNillableOsfamily sets the "osfamily" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableOsfamily(v *string) *MachineUpdate {
	if v != nil {
		_u.SetOsfamily(*v)
	}
	return _u
}

// ClearOsfamily clears the value of the "osfamily" field.
func (_u *MachineUpdate) ClearOsfamily() *MachineUpdate {
	_u.mutation.ClearOsfamily()
	return _u
}

// SetOsversion sets the "osversion" field.
func (_u *MachineUpdate) SetOsversion(v string) *MachineUpdate {
	_u.mutation.SetOsversion(v)
	return _u
}

// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableOsversion(v *string) *MachineUpdate {
	if v != nil {
		_u.SetOsversion(*v)
	}
	return _u
}

// ClearOsversion clears the value of the "osversion" field.
func (_u *MachineUpdate) ClearOsversion() *MachineUpdate {
	_u.mutation.ClearOsversion()
	return _u
}

// SetFeatureflags sets the "featureflags" field.
func (_u *MachineUpdate) SetFeatureflags(v string) *MachineUpdate {
	_u.mutation.SetFeatureflags(v)
	return _u
}

// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (_u *MachineUpdate) SetNillableFeatureflags(v *string) *MachineUpdate {
	if v != nil {
		_u.SetFeatureflags(*v)
	}
	return _u
}

// ClearFeatureflags clears the value of the "featureflags" field.
func (_u *MachineUpdate) ClearFeatureflags() *MachineUpdate {
	_u.mutation.ClearFeatureflags()
	return _u
}

// SetHubstate sets the "hubstate" field.
func (_u *MachineUpdate) SetHubstate(v map[string][]schema.ItemState) *MachineUpdate {
	_u.mutation.SetHubstate(v)
	return _u
}

// ClearHubstate clears the value of the "hubstate" field.
func (_u *MachineUpdate) ClearHubstate() *MachineUpdate {
	_u.mutation.ClearHubstate()
	return _u
}

// SetDatasources sets the "datasources" field.
func (_u *MachineUpdate) SetDatasources(v map[string]int64) *MachineUpdate {
	_u.mutation.SetDatasources(v)
	return _u
}

// ClearDatasources clears the value of the "datasources" field.
func (_u *MachineUpdate) ClearDatasources() *MachineUpdate {
	_u.mutation.ClearDatasources()
	return _u
}

// AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
func (_u *MachineUpdate) AddAlertIDs(ids ...int) *MachineUpdate {
	_u.mutation.AddAlertIDs(ids...)
	return _u
}

// AddAlerts adds the "alerts" edges to the Alert entity.
func (_u *MachineUpdate) AddAlerts(v ...*Alert) *MachineUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAlertIDs(ids...)
}

// Mutation returns the MachineMutation object of the builder.
func (_u *MachineUpdate) Mutation() *MachineMutation {
	return _u.mutation
}

// ClearAlerts clears all "alerts" edges to the Alert entity.
func (_u *MachineUpdate) ClearAlerts() *MachineUpdate {
	_u.mutation.ClearAlerts()
	return _u
}

// RemoveAlertIDs removes the "alerts" edge to Alert entities by IDs.
func (_u *MachineUpdate) RemoveAlertIDs(ids ...int) *MachineUpdate {
	_u.mutation.RemoveAlertIDs(ids...)
	return _u
}

// RemoveAlerts removes "alerts" edges to Alert entities.
func (_u *MachineUpdate) RemoveAlerts(v ...*Alert) *MachineUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAlertIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *MachineUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *MachineUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *MachineUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *MachineUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *MachineUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := machine.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *MachineUpdate) check() error {
	if v, ok := _u.mutation.Scenarios(); ok {
		if err := machine.ScenariosValidator(v); err != nil {
			return &ValidationError{Name: "scenarios", err: fmt.Errorf(`ent: validator failed for field "Machine.scenarios": %w`, err)}
		}
	}
	return nil
}

func (_u *MachineUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(machine.Table, machine.Columns, sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(machine.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.LastPush(); ok {
		_spec.SetField(machine.FieldLastPush, field.TypeTime, value)
	}
	if _u.mutation.LastPushCleared() {
		_spec.ClearField(machine.FieldLastPush, field.TypeTime)
	}
	if value, ok := _u.mutation.LastHeartbeat(); ok {
		_spec.SetField(machine.FieldLastHeartbeat, field.TypeTime, value)
	}
	if _u.mutation.LastHeartbeatCleared() {
		_spec.ClearField(machine.FieldLastHeartbeat, field.TypeTime)
	}
	if value, ok := _u.mutation.Password(); ok {
		_spec.SetField(machine.FieldPassword, field.TypeString, value)
	}
	if value, ok := _u.mutation.IpAddress(); ok {
		_spec.SetField(machine.FieldIpAddress, field.TypeString, value)
	}
	if value, ok := _u.mutation.Scenarios(); ok {
		_spec.SetField(machine.FieldScenarios, field.TypeString, value)
	}
	if _u.mutation.ScenariosCleared() {
		_spec.ClearField(machine.FieldScenarios, field.TypeString)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(machine.FieldVersion, field.TypeString, value)
	}
	if _u.mutation.VersionCleared() {
		_spec.ClearField(machine.FieldVersion, field.TypeString)
	}
	if value, ok := _u.mutation.IsValidated(); ok {
		_spec.SetField(machine.FieldIsValidated, field.TypeBool, value)
	}
	if value, ok := _u.mutation.AuthType(); ok {
		_spec.SetField(machine.FieldAuthType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Osname(); ok {
		_spec.SetField(machine.FieldOsname, field.TypeString, value)
	}
	if _u.mutation.OsnameCleared() {
		_spec.ClearField(machine.FieldOsname, field.TypeString)
	}
	if value, ok := _u.mutation.Osfamily(); ok {
		_spec.SetField(machine.FieldOsfamily, field.TypeString, value)
	}
	if _u.mutation.OsfamilyCleared() {
		_spec.ClearField(machine.FieldOsfamily, field.TypeString)
	}
	if value, ok := _u.mutation.Osversion(); ok {
		_spec.SetField(machine.FieldOsversion, field.TypeString, value)
	}
	if _u.mutation.OsversionCleared() {
		_spec.ClearField(machine.FieldOsversion, field.TypeString)
	}
	if value, ok := _u.mutation.Featureflags(); ok {
		_spec.SetField(machine.FieldFeatureflags, field.TypeString, value)
	}
	if _u.mutation.FeatureflagsCleared() {
		_spec.ClearField(machine.FieldFeatureflags, field.TypeString)
	}
	if value, ok := _u.mutation.Hubstate(); ok {
		_spec.SetField(machine.FieldHubstate, field.TypeJSON, value)
	}
	if _u.mutation.HubstateCleared() {
		_spec.ClearField(machine.FieldHubstate, field.TypeJSON)
	}
	if value, ok := _u.mutation.Datasources(); ok {
		_spec.SetField(machine.FieldDatasources, field.TypeJSON, value)
	}
	if _u.mutation.DatasourcesCleared() {
		_spec.ClearField(machine.FieldDatasources, field.TypeJSON)
	}
	if _u.mutation.AlertsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAlertsIDs(); len(nodes) > 0 && !_u.mutation.AlertsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AlertsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{machine.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// MachineUpdateOne is the builder for updating a single Machine entity.
type MachineUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MachineMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *MachineUpdateOne) SetUpdatedAt(v time.Time) *MachineUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetLastPush sets the "last_push" field.
func (_u *MachineUpdateOne) SetLastPush(v time.Time) *MachineUpdateOne {
	_u.mutation.SetLastPush(v)
	return _u
}

// SetNillableLastPush sets the "last_push" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableLastPush(v *time.Time) *MachineUpdateOne {
	if v != nil {
		_u.SetLastPush(*v)
	}
	return _u
}

// ClearLastPush clears the value of the "last_push" field.
func (_u *MachineUpdateOne) ClearLastPush() *MachineUpdateOne {
	_u.mutation.ClearLastPush()
	return _u
}

// SetLastHeartbeat sets the "last_heartbeat" field.
func (_u *MachineUpdateOne) SetLastHeartbeat(v time.Time) *MachineUpdateOne {
	_u.mutation.SetLastHeartbeat(v)
	return _u
}

// SetNillableLastHeartbeat sets the "last_heartbeat" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableLastHeartbeat(v *time.Time) *MachineUpdateOne {
	if v != nil {
		_u.SetLastHeartbeat(*v)
	}
	return _u
}

// ClearLastHeartbeat clears the value of the "last_heartbeat" field.
func (_u *MachineUpdateOne) ClearLastHeartbeat() *MachineUpdateOne {
	_u.mutation.ClearLastHeartbeat()
	return _u
}

// SetPassword sets the "password" field.
func (_u *MachineUpdateOne) SetPassword(v string) *MachineUpdateOne {
	_u.mutation.SetPassword(v)
	return _u
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillablePassword(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetPassword(*v)
	}
	return _u
}

// SetIpAddress sets the "ipAddress" field.
func (_u *MachineUpdateOne) SetIpAddress(v string) *MachineUpdateOne {
	_u.mutation.SetIpAddress(v)
	return _u
}

// SetNillableIpAddress sets the "ipAddress" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableIpAddress(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetIpAddress(*v)
	}
	return _u
}

// SetScenarios sets the "scenarios" field.
func (_u *MachineUpdateOne) SetScenarios(v string) *MachineUpdateOne {
	_u.mutation.SetScenarios(v)
	return _u
}

// SetNillableScenarios sets the "scenarios" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableScenarios(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetScenarios(*v)
	}
	return _u
}

// ClearScenarios clears the value of the "scenarios" field.
func (_u *MachineUpdateOne) ClearScenarios() *MachineUpdateOne {
	_u.mutation.ClearScenarios()
	return _u
}

// SetVersion sets the "version" field.
func (_u *MachineUpdateOne) SetVersion(v string) *MachineUpdateOne {
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableVersion(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// ClearVersion clears the value of the "version" field.
func (_u *MachineUpdateOne) ClearVersion() *MachineUpdateOne {
	_u.mutation.ClearVersion()
	return _u
}

// SetIsValidated sets the "isValidated" field.
func (_u *MachineUpdateOne) SetIsValidated(v bool) *MachineUpdateOne {
	_u.mutation.SetIsValidated(v)
	return _u
}

// SetNillableIsValidated sets the "isValidated" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableIsValidated(v *bool) *MachineUpdateOne {
	if v != nil {
		_u.SetIsValidated(*v)
	}
	return _u
}

// SetAuthType sets the "auth_type" field.
func (_u *MachineUpdateOne) SetAuthType(v string) *MachineUpdateOne {
	_u.mutation.SetAuthType(v)
	return _u
}

// SetNillableAuthType sets the "auth_type" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableAuthType(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetAuthType(*v)
	}
	return _u
}

// SetOsname sets the "osname" field.
func (_u *MachineUpdateOne) SetOsname(v string) *MachineUpdateOne {
	_u.mutation.SetOsname(v)
	return _u
}

// SetNillableOsname sets the "osname" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableOsname(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetOsname(*v)
	}
	return _u
}

// ClearOsname clears the value of the "osname" field.
func (_u *MachineUpdateOne) ClearOsname() *MachineUpdateOne {
	_u.mutation.ClearOsname()
	return _u
}

// SetOsfamily sets the "osfamily" field.
func (_u *MachineUpdateOne) SetOsfamily(v string) *MachineUpdateOne {
	_u.mutation.SetOsfamily(v)
	return _u
}

// SetNillableOsfamily sets the "osfamily" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableOsfamily(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetOsfamily(*v)
	}
	return _u
}

// ClearOsfamily clears the value of the "osfamily" field.
func (_u *MachineUpdateOne) ClearOsfamily() *MachineUpdateOne {
	_u.mutation.ClearOsfamily()
	return _u
}

// SetOsversion sets the "osversion" field.
func (_u *MachineUpdateOne) SetOsversion(v string) *MachineUpdateOne {
	_u.mutation.SetOsversion(v)
	return _u
}

// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableOsversion(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetOsversion(*v)
	}
	return _u
}

// ClearOsversion clears the value of the "osversion" field.
func (_u *MachineUpdateOne) ClearOsversion() *MachineUpdateOne {
	_u.mutation.ClearOsversion()
	return _u
}

// SetFeatureflags sets the "featureflags" field.
func (_u *MachineUpdateOne) SetFeatureflags(v string) *MachineUpdateOne {
	_u.mutation.SetFeatureflags(v)
	return _u
}

// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (_u *MachineUpdateOne) SetNillableFeatureflags(v *string) *MachineUpdateOne {
	if v != nil {
		_u.SetFeatureflags(*v)
	}
	return _u
}

// ClearFeatureflags clears the value of the "featureflags" field.
func (_u *MachineUpdateOne) ClearFeatureflags() *MachineUpdateOne {
	_u.mutation.ClearFeatureflags()
	return _u
}

// SetHubstate sets the "hubstate" field.
func (_u *MachineUpdateOne) SetHubstate(v map[string][]schema.ItemState) *MachineUpdateOne {
	_u.mutation.SetHubstate(v)
	return _u
}

// ClearHubstate clears the value of the "hubstate" field.
func (_u *MachineUpdateOne) ClearHubstate() *MachineUpdateOne {
	_u.mutation.ClearHubstate()
	return _u
}

// SetDatasources sets the "datasources" field.
func (_u *MachineUpdateOne) SetDatasources(v map[string]int64) *MachineUpdateOne {
	_u.mutation.SetDatasources(v)
	return _u
}

// ClearDatasources clears the value of the "datasources" field.
func (_u *MachineUpdateOne) ClearDatasources() *MachineUpdateOne {
	_u.mutation.ClearDatasources()
	return _u
}

// AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
func (_u *MachineUpdateOne) AddAlertIDs(ids ...int) *MachineUpdateOne {
	_u.mutation.AddAlertIDs(ids...)
	return _u
}

// AddAlerts adds the "alerts" edges to the Alert entity.
func (_u *MachineUpdateOne) AddAlerts(v ...*Alert) *MachineUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAlertIDs(ids...)
}

// Mutation returns the MachineMutation object of the builder.
func (_u *MachineUpdateOne) Mutation() *MachineMutation {
	return _u.mutation
}

// ClearAlerts clears all "alerts" edges to the Alert entity.
func (_u *MachineUpdateOne) ClearAlerts() *MachineUpdateOne {
	_u.mutation.ClearAlerts()
	return _u
}

// RemoveAlertIDs removes the "alerts" edge to Alert entities by IDs.
func (_u *MachineUpdateOne) RemoveAlertIDs(ids ...int) *MachineUpdateOne {
	_u.mutation.RemoveAlertIDs(ids...)
	return _u
}

// RemoveAlerts removes "alerts" edges to Alert entities.
func (_u *MachineUpdateOne) RemoveAlerts(v ...*Alert) *MachineUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAlertIDs(ids...)
}

// Where appends a list predicates to the MachineUpdate builder.
func (_u *MachineUpdateOne) Where(ps ...predicate.Machine) *MachineUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *MachineUpdateOne) Select(field string, fields ...string) *MachineUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated Machine entity.
func (_u *MachineUpdateOne) Save(ctx context.Context) (*Machine, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *MachineUpdateOne) SaveX(ctx context.Context) *Machine {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *MachineUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *MachineUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *MachineUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := machine.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *MachineUpdateOne) check() error {
	if v, ok := _u.mutation.Scenarios(); ok {
		if err := machine.ScenariosValidator(v); err != nil {
			return &ValidationError{Name: "scenarios", err: fmt.Errorf(`ent: validator failed for field "Machine.scenarios": %w`, err)}
		}
	}
	return nil
}

func (_u *MachineUpdateOne) sqlSave(ctx context.Context) (_node *Machine, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(machine.Table, machine.Columns, sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Machine.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, machine.FieldID)
		for _, f := range fields {
			if !machine.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != machine.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(machine.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.LastPush(); ok {
		_spec.SetField(machine.FieldLastPush, field.TypeTime, value)
	}
	if _u.mutation.LastPushCleared() {
		_spec.ClearField(machine.FieldLastPush, field.TypeTime)
	}
	if value, ok := _u.mutation.LastHeartbeat(); ok {
		_spec.SetField(machine.FieldLastHeartbeat, field.TypeTime, value)
	}
	if _u.mutation.LastHeartbeatCleared() {
		_spec.ClearField(machine.FieldLastHeartbeat, field.TypeTime)
	}
	if value, ok := _u.mutation.Password(); ok {
		_spec.SetField(machine.FieldPassword, field.TypeString, value)
	}
	if value, ok := _u.mutation.IpAddress(); ok {
		_spec.SetField(machine.FieldIpAddress, field.TypeString, value)
	}
	if value, ok := _u.mutation.Scenarios(); ok {
		_spec.SetField(machine.FieldScenarios, field.TypeString, value)
	}
	if _u.mutation.ScenariosCleared() {
		_spec.ClearField(machine.FieldScenarios, field.TypeString)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(machine.FieldVersion, field.TypeString, value)
	}
	if _u.mutation.VersionCleared() {
		_spec.ClearField(machine.FieldVersion, field.TypeString)
	}
	if value, ok := _u.mutation.IsValidated(); ok {
		_spec.SetField(machine.FieldIsValidated, field.TypeBool, value)
	}
	if value, ok := _u.mutation.AuthType(); ok {
		_spec.SetField(machine.FieldAuthType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Osname(); ok {
		_spec.SetField(machine.FieldOsname, field.TypeString, value)
	}
	if _u.mutation.OsnameCleared() {
		_spec.ClearField(machine.FieldOsname, field.TypeString)
	}
	if value, ok := _u.mutation.Osfamily(); ok {
		_spec.SetField(machine.FieldOsfamily, field.TypeString, value)
	}
	if _u.mutation.OsfamilyCleared() {
		_spec.ClearField(machine.FieldOsfamily, field.TypeString)
	}
	if value, ok := _u.mutation.Osversion(); ok {
		_spec.SetField(machine.FieldOsversion, field.TypeString, value)
	}
	if _u.mutation.OsversionCleared() {
		_spec.ClearField(machine.FieldOsversion, field.TypeString)
	}
	if value, ok := _u.mutation.Featureflags(); ok {
		_spec.SetField(machine.FieldFeatureflags, field.TypeString, value)
	}
	if _u.mutation.FeatureflagsCleared() {
		_spec.ClearField(machine.FieldFeatureflags, field.TypeString)
	}
	if value, ok := _u.mutation.Hubstate(); ok {
		_spec.SetField(machine.FieldHubstate, field.TypeJSON, value)
	}
	if _u.mutation.HubstateCleared() {
		_spec.ClearField(machine.FieldHubstate, field.TypeJSON)
	}
	if value, ok := _u.mutation.Datasources(); ok {
		_spec.SetField(machine.FieldDatasources, field.TypeJSON, value)
	}
	if _u.mutation.DatasourcesCleared() {
		_spec.ClearField(machine.FieldDatasources, field.TypeJSON)
	}
	if _u.mutation.AlertsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAlertsIDs(); len(nodes) > 0 && !_u.mutation.AlertsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AlertsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   machine.AlertsTable,
			Columns: []string{machine.AlertsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Machine{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{machine.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
