// 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/decision"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/event"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)

// AlertUpdate is the builder for updating Alert entities.
type AlertUpdate struct {
	config
	hooks    []Hook
	mutation *AlertMutation
}

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

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

// SetOwnerID sets the "owner" edge to the Machine entity by ID.
func (_u *AlertUpdate) SetOwnerID(id int) *AlertUpdate {
	_u.mutation.SetOwnerID(id)
	return _u
}

// SetNillableOwnerID sets the "owner" edge to the Machine entity by ID if the given value is not nil.
func (_u *AlertUpdate) SetNillableOwnerID(id *int) *AlertUpdate {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

// SetOwner sets the "owner" edge to the Machine entity.
func (_u *AlertUpdate) SetOwner(v *Machine) *AlertUpdate {
	return _u.SetOwnerID(v.ID)
}

// AddDecisionIDs adds the "decisions" edge to the Decision entity by IDs.
func (_u *AlertUpdate) AddDecisionIDs(ids ...int) *AlertUpdate {
	_u.mutation.AddDecisionIDs(ids...)
	return _u
}

// AddDecisions adds the "decisions" edges to the Decision entity.
func (_u *AlertUpdate) AddDecisions(v ...*Decision) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddDecisionIDs(ids...)
}

// AddEventIDs adds the "events" edge to the Event entity by IDs.
func (_u *AlertUpdate) AddEventIDs(ids ...int) *AlertUpdate {
	_u.mutation.AddEventIDs(ids...)
	return _u
}

// AddEvents adds the "events" edges to the Event entity.
func (_u *AlertUpdate) AddEvents(v ...*Event) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEventIDs(ids...)
}

// AddMetaIDs adds the "metas" edge to the Meta entity by IDs.
func (_u *AlertUpdate) AddMetaIDs(ids ...int) *AlertUpdate {
	_u.mutation.AddMetaIDs(ids...)
	return _u
}

// AddMetas adds the "metas" edges to the Meta entity.
func (_u *AlertUpdate) AddMetas(v ...*Meta) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddMetaIDs(ids...)
}

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

// ClearOwner clears the "owner" edge to the Machine entity.
func (_u *AlertUpdate) ClearOwner() *AlertUpdate {
	_u.mutation.ClearOwner()
	return _u
}

// ClearDecisions clears all "decisions" edges to the Decision entity.
func (_u *AlertUpdate) ClearDecisions() *AlertUpdate {
	_u.mutation.ClearDecisions()
	return _u
}

// RemoveDecisionIDs removes the "decisions" edge to Decision entities by IDs.
func (_u *AlertUpdate) RemoveDecisionIDs(ids ...int) *AlertUpdate {
	_u.mutation.RemoveDecisionIDs(ids...)
	return _u
}

// RemoveDecisions removes "decisions" edges to Decision entities.
func (_u *AlertUpdate) RemoveDecisions(v ...*Decision) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveDecisionIDs(ids...)
}

// ClearEvents clears all "events" edges to the Event entity.
func (_u *AlertUpdate) ClearEvents() *AlertUpdate {
	_u.mutation.ClearEvents()
	return _u
}

// RemoveEventIDs removes the "events" edge to Event entities by IDs.
func (_u *AlertUpdate) RemoveEventIDs(ids ...int) *AlertUpdate {
	_u.mutation.RemoveEventIDs(ids...)
	return _u
}

// RemoveEvents removes "events" edges to Event entities.
func (_u *AlertUpdate) RemoveEvents(v ...*Event) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEventIDs(ids...)
}

// ClearMetas clears all "metas" edges to the Meta entity.
func (_u *AlertUpdate) ClearMetas() *AlertUpdate {
	_u.mutation.ClearMetas()
	return _u
}

// RemoveMetaIDs removes the "metas" edge to Meta entities by IDs.
func (_u *AlertUpdate) RemoveMetaIDs(ids ...int) *AlertUpdate {
	_u.mutation.RemoveMetaIDs(ids...)
	return _u
}

// RemoveMetas removes "metas" edges to Meta entities.
func (_u *AlertUpdate) RemoveMetas(v ...*Meta) *AlertUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveMetaIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *AlertUpdate) 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 *AlertUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AlertUpdate) 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 *AlertUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := alert.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *AlertUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(alert.Table, alert.Columns, sqlgraph.NewFieldSpec(alert.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(alert.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.BucketIdCleared() {
		_spec.ClearField(alert.FieldBucketId, field.TypeString)
	}
	if _u.mutation.MessageCleared() {
		_spec.ClearField(alert.FieldMessage, field.TypeString)
	}
	if _u.mutation.EventsCountCleared() {
		_spec.ClearField(alert.FieldEventsCount, field.TypeInt32)
	}
	if _u.mutation.StartedAtCleared() {
		_spec.ClearField(alert.FieldStartedAt, field.TypeTime)
	}
	if _u.mutation.StoppedAtCleared() {
		_spec.ClearField(alert.FieldStoppedAt, field.TypeTime)
	}
	if _u.mutation.SourceIpCleared() {
		_spec.ClearField(alert.FieldSourceIp, field.TypeString)
	}
	if _u.mutation.SourceRangeCleared() {
		_spec.ClearField(alert.FieldSourceRange, field.TypeString)
	}
	if _u.mutation.SourceAsNumberCleared() {
		_spec.ClearField(alert.FieldSourceAsNumber, field.TypeString)
	}
	if _u.mutation.SourceAsNameCleared() {
		_spec.ClearField(alert.FieldSourceAsName, field.TypeString)
	}
	if _u.mutation.SourceCountryCleared() {
		_spec.ClearField(alert.FieldSourceCountry, field.TypeString)
	}
	if _u.mutation.SourceLatitudeCleared() {
		_spec.ClearField(alert.FieldSourceLatitude, field.TypeFloat32)
	}
	if _u.mutation.SourceLongitudeCleared() {
		_spec.ClearField(alert.FieldSourceLongitude, field.TypeFloat32)
	}
	if _u.mutation.SourceScopeCleared() {
		_spec.ClearField(alert.FieldSourceScope, field.TypeString)
	}
	if _u.mutation.SourceValueCleared() {
		_spec.ClearField(alert.FieldSourceValue, field.TypeString)
	}
	if _u.mutation.CapacityCleared() {
		_spec.ClearField(alert.FieldCapacity, field.TypeInt32)
	}
	if _u.mutation.LeakSpeedCleared() {
		_spec.ClearField(alert.FieldLeakSpeed, field.TypeString)
	}
	if _u.mutation.ScenarioVersionCleared() {
		_spec.ClearField(alert.FieldScenarioVersion, field.TypeString)
	}
	if _u.mutation.ScenarioHashCleared() {
		_spec.ClearField(alert.FieldScenarioHash, field.TypeString)
	}
	if _u.mutation.UUIDCleared() {
		_spec.ClearField(alert.FieldUUID, field.TypeString)
	}
	if _u.mutation.RemediationCleared() {
		_spec.ClearField(alert.FieldRemediation, field.TypeBool)
	}
	if _u.mutation.KindCleared() {
		_spec.ClearField(alert.FieldKind, field.TypeString)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alert.OwnerTable,
			Columns: []string{alert.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alert.OwnerTable,
			Columns: []string{alert.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.DecisionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedDecisionsIDs(); len(nodes) > 0 && !_u.mutation.DecisionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.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.DecisionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEventsIDs(); len(nodes) > 0 && !_u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.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.EventsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.MetasCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedMetasIDs(); len(nodes) > 0 && !_u.mutation.MetasCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.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.MetasIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.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{alert.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AlertUpdateOne is the builder for updating a single Alert entity.
type AlertUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AlertMutation
}

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

// SetOwnerID sets the "owner" edge to the Machine entity by ID.
func (_u *AlertUpdateOne) SetOwnerID(id int) *AlertUpdateOne {
	_u.mutation.SetOwnerID(id)
	return _u
}

// SetNillableOwnerID sets the "owner" edge to the Machine entity by ID if the given value is not nil.
func (_u *AlertUpdateOne) SetNillableOwnerID(id *int) *AlertUpdateOne {
	if id != nil {
		_u = _u.SetOwnerID(*id)
	}
	return _u
}

// SetOwner sets the "owner" edge to the Machine entity.
func (_u *AlertUpdateOne) SetOwner(v *Machine) *AlertUpdateOne {
	return _u.SetOwnerID(v.ID)
}

// AddDecisionIDs adds the "decisions" edge to the Decision entity by IDs.
func (_u *AlertUpdateOne) AddDecisionIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.AddDecisionIDs(ids...)
	return _u
}

// AddDecisions adds the "decisions" edges to the Decision entity.
func (_u *AlertUpdateOne) AddDecisions(v ...*Decision) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddDecisionIDs(ids...)
}

// AddEventIDs adds the "events" edge to the Event entity by IDs.
func (_u *AlertUpdateOne) AddEventIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.AddEventIDs(ids...)
	return _u
}

// AddEvents adds the "events" edges to the Event entity.
func (_u *AlertUpdateOne) AddEvents(v ...*Event) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEventIDs(ids...)
}

// AddMetaIDs adds the "metas" edge to the Meta entity by IDs.
func (_u *AlertUpdateOne) AddMetaIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.AddMetaIDs(ids...)
	return _u
}

// AddMetas adds the "metas" edges to the Meta entity.
func (_u *AlertUpdateOne) AddMetas(v ...*Meta) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddMetaIDs(ids...)
}

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

// ClearOwner clears the "owner" edge to the Machine entity.
func (_u *AlertUpdateOne) ClearOwner() *AlertUpdateOne {
	_u.mutation.ClearOwner()
	return _u
}

// ClearDecisions clears all "decisions" edges to the Decision entity.
func (_u *AlertUpdateOne) ClearDecisions() *AlertUpdateOne {
	_u.mutation.ClearDecisions()
	return _u
}

// RemoveDecisionIDs removes the "decisions" edge to Decision entities by IDs.
func (_u *AlertUpdateOne) RemoveDecisionIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.RemoveDecisionIDs(ids...)
	return _u
}

// RemoveDecisions removes "decisions" edges to Decision entities.
func (_u *AlertUpdateOne) RemoveDecisions(v ...*Decision) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveDecisionIDs(ids...)
}

// ClearEvents clears all "events" edges to the Event entity.
func (_u *AlertUpdateOne) ClearEvents() *AlertUpdateOne {
	_u.mutation.ClearEvents()
	return _u
}

// RemoveEventIDs removes the "events" edge to Event entities by IDs.
func (_u *AlertUpdateOne) RemoveEventIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.RemoveEventIDs(ids...)
	return _u
}

// RemoveEvents removes "events" edges to Event entities.
func (_u *AlertUpdateOne) RemoveEvents(v ...*Event) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEventIDs(ids...)
}

// ClearMetas clears all "metas" edges to the Meta entity.
func (_u *AlertUpdateOne) ClearMetas() *AlertUpdateOne {
	_u.mutation.ClearMetas()
	return _u
}

// RemoveMetaIDs removes the "metas" edge to Meta entities by IDs.
func (_u *AlertUpdateOne) RemoveMetaIDs(ids ...int) *AlertUpdateOne {
	_u.mutation.RemoveMetaIDs(ids...)
	return _u
}

// RemoveMetas removes "metas" edges to Meta entities.
func (_u *AlertUpdateOne) RemoveMetas(v ...*Meta) *AlertUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveMetaIDs(ids...)
}

// Where appends a list predicates to the AlertUpdate builder.
func (_u *AlertUpdateOne) Where(ps ...predicate.Alert) *AlertUpdateOne {
	_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 *AlertUpdateOne) Select(field string, fields ...string) *AlertUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AlertUpdateOne) 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 *AlertUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := alert.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

func (_u *AlertUpdateOne) sqlSave(ctx context.Context) (_node *Alert, err error) {
	_spec := sqlgraph.NewUpdateSpec(alert.Table, alert.Columns, sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Alert.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, alert.FieldID)
		for _, f := range fields {
			if !alert.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != alert.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(alert.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.BucketIdCleared() {
		_spec.ClearField(alert.FieldBucketId, field.TypeString)
	}
	if _u.mutation.MessageCleared() {
		_spec.ClearField(alert.FieldMessage, field.TypeString)
	}
	if _u.mutation.EventsCountCleared() {
		_spec.ClearField(alert.FieldEventsCount, field.TypeInt32)
	}
	if _u.mutation.StartedAtCleared() {
		_spec.ClearField(alert.FieldStartedAt, field.TypeTime)
	}
	if _u.mutation.StoppedAtCleared() {
		_spec.ClearField(alert.FieldStoppedAt, field.TypeTime)
	}
	if _u.mutation.SourceIpCleared() {
		_spec.ClearField(alert.FieldSourceIp, field.TypeString)
	}
	if _u.mutation.SourceRangeCleared() {
		_spec.ClearField(alert.FieldSourceRange, field.TypeString)
	}
	if _u.mutation.SourceAsNumberCleared() {
		_spec.ClearField(alert.FieldSourceAsNumber, field.TypeString)
	}
	if _u.mutation.SourceAsNameCleared() {
		_spec.ClearField(alert.FieldSourceAsName, field.TypeString)
	}
	if _u.mutation.SourceCountryCleared() {
		_spec.ClearField(alert.FieldSourceCountry, field.TypeString)
	}
	if _u.mutation.SourceLatitudeCleared() {
		_spec.ClearField(alert.FieldSourceLatitude, field.TypeFloat32)
	}
	if _u.mutation.SourceLongitudeCleared() {
		_spec.ClearField(alert.FieldSourceLongitude, field.TypeFloat32)
	}
	if _u.mutation.SourceScopeCleared() {
		_spec.ClearField(alert.FieldSourceScope, field.TypeString)
	}
	if _u.mutation.SourceValueCleared() {
		_spec.ClearField(alert.FieldSourceValue, field.TypeString)
	}
	if _u.mutation.CapacityCleared() {
		_spec.ClearField(alert.FieldCapacity, field.TypeInt32)
	}
	if _u.mutation.LeakSpeedCleared() {
		_spec.ClearField(alert.FieldLeakSpeed, field.TypeString)
	}
	if _u.mutation.ScenarioVersionCleared() {
		_spec.ClearField(alert.FieldScenarioVersion, field.TypeString)
	}
	if _u.mutation.ScenarioHashCleared() {
		_spec.ClearField(alert.FieldScenarioHash, field.TypeString)
	}
	if _u.mutation.UUIDCleared() {
		_spec.ClearField(alert.FieldUUID, field.TypeString)
	}
	if _u.mutation.RemediationCleared() {
		_spec.ClearField(alert.FieldRemediation, field.TypeBool)
	}
	if _u.mutation.KindCleared() {
		_spec.ClearField(alert.FieldKind, field.TypeString)
	}
	if _u.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alert.OwnerTable,
			Columns: []string{alert.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alert.OwnerTable,
			Columns: []string{alert.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.DecisionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedDecisionsIDs(); len(nodes) > 0 && !_u.mutation.DecisionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.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.DecisionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEventsIDs(); len(nodes) > 0 && !_u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.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.EventsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.MetasCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedMetasIDs(); len(nodes) > 0 && !_u.mutation.MetasCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.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.MetasIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Alert{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{alert.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
