// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

// Code generated by entc, DO NOT EDIT.

package ent

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

	"ariga.io/atlas/cmd/atlas/internal/migrate/ent/internal"
	"ariga.io/atlas/cmd/atlas/internal/migrate/ent/predicate"
	"ariga.io/atlas/cmd/atlas/internal/migrate/ent/revision"
	"ariga.io/atlas/sql/migrate"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
)

// RevisionUpdate is the builder for updating Revision entities.
type RevisionUpdate struct {
	config
	hooks    []Hook
	mutation *RevisionMutation
}

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

// SetType sets the "type" field.
func (_u *RevisionUpdate) SetType(v migrate.RevisionType) *RevisionUpdate {
	_u.mutation.ResetType()
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableType(v *migrate.RevisionType) *RevisionUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// AddType adds value to the "type" field.
func (_u *RevisionUpdate) AddType(v migrate.RevisionType) *RevisionUpdate {
	_u.mutation.AddType(v)
	return _u
}

// SetApplied sets the "applied" field.
func (_u *RevisionUpdate) SetApplied(v int) *RevisionUpdate {
	_u.mutation.ResetApplied()
	_u.mutation.SetApplied(v)
	return _u
}

// SetNillableApplied sets the "applied" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableApplied(v *int) *RevisionUpdate {
	if v != nil {
		_u.SetApplied(*v)
	}
	return _u
}

// AddApplied adds value to the "applied" field.
func (_u *RevisionUpdate) AddApplied(v int) *RevisionUpdate {
	_u.mutation.AddApplied(v)
	return _u
}

// SetTotal sets the "total" field.
func (_u *RevisionUpdate) SetTotal(v int) *RevisionUpdate {
	_u.mutation.ResetTotal()
	_u.mutation.SetTotal(v)
	return _u
}

// SetNillableTotal sets the "total" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableTotal(v *int) *RevisionUpdate {
	if v != nil {
		_u.SetTotal(*v)
	}
	return _u
}

// AddTotal adds value to the "total" field.
func (_u *RevisionUpdate) AddTotal(v int) *RevisionUpdate {
	_u.mutation.AddTotal(v)
	return _u
}

// SetExecutionTime sets the "execution_time" field.
func (_u *RevisionUpdate) SetExecutionTime(v time.Duration) *RevisionUpdate {
	_u.mutation.ResetExecutionTime()
	_u.mutation.SetExecutionTime(v)
	return _u
}

// SetNillableExecutionTime sets the "execution_time" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableExecutionTime(v *time.Duration) *RevisionUpdate {
	if v != nil {
		_u.SetExecutionTime(*v)
	}
	return _u
}

// AddExecutionTime adds value to the "execution_time" field.
func (_u *RevisionUpdate) AddExecutionTime(v time.Duration) *RevisionUpdate {
	_u.mutation.AddExecutionTime(v)
	return _u
}

// SetError sets the "error" field.
func (_u *RevisionUpdate) SetError(v string) *RevisionUpdate {
	_u.mutation.SetError(v)
	return _u
}

// SetNillableError sets the "error" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableError(v *string) *RevisionUpdate {
	if v != nil {
		_u.SetError(*v)
	}
	return _u
}

// ClearError clears the value of the "error" field.
func (_u *RevisionUpdate) ClearError() *RevisionUpdate {
	_u.mutation.ClearError()
	return _u
}

// SetErrorStmt sets the "error_stmt" field.
func (_u *RevisionUpdate) SetErrorStmt(v string) *RevisionUpdate {
	_u.mutation.SetErrorStmt(v)
	return _u
}

// SetNillableErrorStmt sets the "error_stmt" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableErrorStmt(v *string) *RevisionUpdate {
	if v != nil {
		_u.SetErrorStmt(*v)
	}
	return _u
}

// ClearErrorStmt clears the value of the "error_stmt" field.
func (_u *RevisionUpdate) ClearErrorStmt() *RevisionUpdate {
	_u.mutation.ClearErrorStmt()
	return _u
}

// SetHash sets the "hash" field.
func (_u *RevisionUpdate) SetHash(v string) *RevisionUpdate {
	_u.mutation.SetHash(v)
	return _u
}

// SetNillableHash sets the "hash" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableHash(v *string) *RevisionUpdate {
	if v != nil {
		_u.SetHash(*v)
	}
	return _u
}

// SetPartialHashes sets the "partial_hashes" field.
func (_u *RevisionUpdate) SetPartialHashes(v []string) *RevisionUpdate {
	_u.mutation.SetPartialHashes(v)
	return _u
}

// AppendPartialHashes appends value to the "partial_hashes" field.
func (_u *RevisionUpdate) AppendPartialHashes(v []string) *RevisionUpdate {
	_u.mutation.AppendPartialHashes(v)
	return _u
}

// ClearPartialHashes clears the value of the "partial_hashes" field.
func (_u *RevisionUpdate) ClearPartialHashes() *RevisionUpdate {
	_u.mutation.ClearPartialHashes()
	return _u
}

// SetOperatorVersion sets the "operator_version" field.
func (_u *RevisionUpdate) SetOperatorVersion(v string) *RevisionUpdate {
	_u.mutation.SetOperatorVersion(v)
	return _u
}

// SetNillableOperatorVersion sets the "operator_version" field if the given value is not nil.
func (_u *RevisionUpdate) SetNillableOperatorVersion(v *string) *RevisionUpdate {
	if v != nil {
		_u.SetOperatorVersion(*v)
	}
	return _u
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *RevisionUpdate) check() error {
	if v, ok := _u.mutation.Applied(); ok {
		if err := revision.AppliedValidator(v); err != nil {
			return &ValidationError{Name: "applied", err: fmt.Errorf(`ent: validator failed for field "Revision.applied": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Total(); ok {
		if err := revision.TotalValidator(v); err != nil {
			return &ValidationError{Name: "total", err: fmt.Errorf(`ent: validator failed for field "Revision.total": %w`, err)}
		}
	}
	return nil
}

func (_u *RevisionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(revision.Table, revision.Columns, sqlgraph.NewFieldSpec(revision.FieldID, field.TypeString))
	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.GetType(); ok {
		_spec.SetField(revision.FieldType, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedType(); ok {
		_spec.AddField(revision.FieldType, field.TypeUint, value)
	}
	if value, ok := _u.mutation.Applied(); ok {
		_spec.SetField(revision.FieldApplied, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedApplied(); ok {
		_spec.AddField(revision.FieldApplied, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Total(); ok {
		_spec.SetField(revision.FieldTotal, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedTotal(); ok {
		_spec.AddField(revision.FieldTotal, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ExecutionTime(); ok {
		_spec.SetField(revision.FieldExecutionTime, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedExecutionTime(); ok {
		_spec.AddField(revision.FieldExecutionTime, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Error(); ok {
		_spec.SetField(revision.FieldError, field.TypeString, value)
	}
	if _u.mutation.ErrorCleared() {
		_spec.ClearField(revision.FieldError, field.TypeString)
	}
	if value, ok := _u.mutation.ErrorStmt(); ok {
		_spec.SetField(revision.FieldErrorStmt, field.TypeString, value)
	}
	if _u.mutation.ErrorStmtCleared() {
		_spec.ClearField(revision.FieldErrorStmt, field.TypeString)
	}
	if value, ok := _u.mutation.Hash(); ok {
		_spec.SetField(revision.FieldHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.PartialHashes(); ok {
		_spec.SetField(revision.FieldPartialHashes, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedPartialHashes(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, revision.FieldPartialHashes, value)
		})
	}
	if _u.mutation.PartialHashesCleared() {
		_spec.ClearField(revision.FieldPartialHashes, field.TypeJSON)
	}
	if value, ok := _u.mutation.OperatorVersion(); ok {
		_spec.SetField(revision.FieldOperatorVersion, field.TypeString, value)
	}
	_spec.Node.Schema = _u.schemaConfig.Revision
	ctx = internal.NewSchemaConfigContext(ctx, _u.schemaConfig)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{revision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// RevisionUpdateOne is the builder for updating a single Revision entity.
type RevisionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *RevisionMutation
}

// SetType sets the "type" field.
func (_u *RevisionUpdateOne) SetType(v migrate.RevisionType) *RevisionUpdateOne {
	_u.mutation.ResetType()
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableType(v *migrate.RevisionType) *RevisionUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// AddType adds value to the "type" field.
func (_u *RevisionUpdateOne) AddType(v migrate.RevisionType) *RevisionUpdateOne {
	_u.mutation.AddType(v)
	return _u
}

// SetApplied sets the "applied" field.
func (_u *RevisionUpdateOne) SetApplied(v int) *RevisionUpdateOne {
	_u.mutation.ResetApplied()
	_u.mutation.SetApplied(v)
	return _u
}

// SetNillableApplied sets the "applied" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableApplied(v *int) *RevisionUpdateOne {
	if v != nil {
		_u.SetApplied(*v)
	}
	return _u
}

// AddApplied adds value to the "applied" field.
func (_u *RevisionUpdateOne) AddApplied(v int) *RevisionUpdateOne {
	_u.mutation.AddApplied(v)
	return _u
}

// SetTotal sets the "total" field.
func (_u *RevisionUpdateOne) SetTotal(v int) *RevisionUpdateOne {
	_u.mutation.ResetTotal()
	_u.mutation.SetTotal(v)
	return _u
}

// SetNillableTotal sets the "total" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableTotal(v *int) *RevisionUpdateOne {
	if v != nil {
		_u.SetTotal(*v)
	}
	return _u
}

// AddTotal adds value to the "total" field.
func (_u *RevisionUpdateOne) AddTotal(v int) *RevisionUpdateOne {
	_u.mutation.AddTotal(v)
	return _u
}

// SetExecutionTime sets the "execution_time" field.
func (_u *RevisionUpdateOne) SetExecutionTime(v time.Duration) *RevisionUpdateOne {
	_u.mutation.ResetExecutionTime()
	_u.mutation.SetExecutionTime(v)
	return _u
}

// SetNillableExecutionTime sets the "execution_time" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableExecutionTime(v *time.Duration) *RevisionUpdateOne {
	if v != nil {
		_u.SetExecutionTime(*v)
	}
	return _u
}

// AddExecutionTime adds value to the "execution_time" field.
func (_u *RevisionUpdateOne) AddExecutionTime(v time.Duration) *RevisionUpdateOne {
	_u.mutation.AddExecutionTime(v)
	return _u
}

// SetError sets the "error" field.
func (_u *RevisionUpdateOne) SetError(v string) *RevisionUpdateOne {
	_u.mutation.SetError(v)
	return _u
}

// SetNillableError sets the "error" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableError(v *string) *RevisionUpdateOne {
	if v != nil {
		_u.SetError(*v)
	}
	return _u
}

// ClearError clears the value of the "error" field.
func (_u *RevisionUpdateOne) ClearError() *RevisionUpdateOne {
	_u.mutation.ClearError()
	return _u
}

// SetErrorStmt sets the "error_stmt" field.
func (_u *RevisionUpdateOne) SetErrorStmt(v string) *RevisionUpdateOne {
	_u.mutation.SetErrorStmt(v)
	return _u
}

// SetNillableErrorStmt sets the "error_stmt" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableErrorStmt(v *string) *RevisionUpdateOne {
	if v != nil {
		_u.SetErrorStmt(*v)
	}
	return _u
}

// ClearErrorStmt clears the value of the "error_stmt" field.
func (_u *RevisionUpdateOne) ClearErrorStmt() *RevisionUpdateOne {
	_u.mutation.ClearErrorStmt()
	return _u
}

// SetHash sets the "hash" field.
func (_u *RevisionUpdateOne) SetHash(v string) *RevisionUpdateOne {
	_u.mutation.SetHash(v)
	return _u
}

// SetNillableHash sets the "hash" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableHash(v *string) *RevisionUpdateOne {
	if v != nil {
		_u.SetHash(*v)
	}
	return _u
}

// SetPartialHashes sets the "partial_hashes" field.
func (_u *RevisionUpdateOne) SetPartialHashes(v []string) *RevisionUpdateOne {
	_u.mutation.SetPartialHashes(v)
	return _u
}

// AppendPartialHashes appends value to the "partial_hashes" field.
func (_u *RevisionUpdateOne) AppendPartialHashes(v []string) *RevisionUpdateOne {
	_u.mutation.AppendPartialHashes(v)
	return _u
}

// ClearPartialHashes clears the value of the "partial_hashes" field.
func (_u *RevisionUpdateOne) ClearPartialHashes() *RevisionUpdateOne {
	_u.mutation.ClearPartialHashes()
	return _u
}

// SetOperatorVersion sets the "operator_version" field.
func (_u *RevisionUpdateOne) SetOperatorVersion(v string) *RevisionUpdateOne {
	_u.mutation.SetOperatorVersion(v)
	return _u
}

// SetNillableOperatorVersion sets the "operator_version" field if the given value is not nil.
func (_u *RevisionUpdateOne) SetNillableOperatorVersion(v *string) *RevisionUpdateOne {
	if v != nil {
		_u.SetOperatorVersion(*v)
	}
	return _u
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *RevisionUpdateOne) check() error {
	if v, ok := _u.mutation.Applied(); ok {
		if err := revision.AppliedValidator(v); err != nil {
			return &ValidationError{Name: "applied", err: fmt.Errorf(`ent: validator failed for field "Revision.applied": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Total(); ok {
		if err := revision.TotalValidator(v); err != nil {
			return &ValidationError{Name: "total", err: fmt.Errorf(`ent: validator failed for field "Revision.total": %w`, err)}
		}
	}
	return nil
}

func (_u *RevisionUpdateOne) sqlSave(ctx context.Context) (_node *Revision, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(revision.Table, revision.Columns, sqlgraph.NewFieldSpec(revision.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Revision.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, revision.FieldID)
		for _, f := range fields {
			if !revision.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != revision.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.GetType(); ok {
		_spec.SetField(revision.FieldType, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedType(); ok {
		_spec.AddField(revision.FieldType, field.TypeUint, value)
	}
	if value, ok := _u.mutation.Applied(); ok {
		_spec.SetField(revision.FieldApplied, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedApplied(); ok {
		_spec.AddField(revision.FieldApplied, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Total(); ok {
		_spec.SetField(revision.FieldTotal, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedTotal(); ok {
		_spec.AddField(revision.FieldTotal, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ExecutionTime(); ok {
		_spec.SetField(revision.FieldExecutionTime, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedExecutionTime(); ok {
		_spec.AddField(revision.FieldExecutionTime, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Error(); ok {
		_spec.SetField(revision.FieldError, field.TypeString, value)
	}
	if _u.mutation.ErrorCleared() {
		_spec.ClearField(revision.FieldError, field.TypeString)
	}
	if value, ok := _u.mutation.ErrorStmt(); ok {
		_spec.SetField(revision.FieldErrorStmt, field.TypeString, value)
	}
	if _u.mutation.ErrorStmtCleared() {
		_spec.ClearField(revision.FieldErrorStmt, field.TypeString)
	}
	if value, ok := _u.mutation.Hash(); ok {
		_spec.SetField(revision.FieldHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.PartialHashes(); ok {
		_spec.SetField(revision.FieldPartialHashes, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedPartialHashes(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, revision.FieldPartialHashes, value)
		})
	}
	if _u.mutation.PartialHashesCleared() {
		_spec.ClearField(revision.FieldPartialHashes, field.TypeJSON)
	}
	if value, ok := _u.mutation.OperatorVersion(); ok {
		_spec.SetField(revision.FieldOperatorVersion, field.TypeString, value)
	}
	_spec.Node.Schema = _u.schemaConfig.Revision
	ctx = internal.NewSchemaConfigContext(ctx, _u.schemaConfig)
	_node = &Revision{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{revision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
