// 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/buildbarn/bb-portal/ent/gen/ent/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testcollection"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testresultbes"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testsummary"
)

// TestCollectionUpdate is the builder for updating TestCollection entities.
type TestCollectionUpdate struct {
	config
	hooks    []Hook
	mutation *TestCollectionMutation
}

// Where appends a list predicates to the TestCollectionUpdate builder.
func (tcu *TestCollectionUpdate) Where(ps ...predicate.TestCollection) *TestCollectionUpdate {
	tcu.mutation.Where(ps...)
	return tcu
}

// SetLabel sets the "label" field.
func (tcu *TestCollectionUpdate) SetLabel(s string) *TestCollectionUpdate {
	tcu.mutation.SetLabel(s)
	return tcu
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableLabel(s *string) *TestCollectionUpdate {
	if s != nil {
		tcu.SetLabel(*s)
	}
	return tcu
}

// ClearLabel clears the value of the "label" field.
func (tcu *TestCollectionUpdate) ClearLabel() *TestCollectionUpdate {
	tcu.mutation.ClearLabel()
	return tcu
}

// SetOverallStatus sets the "overall_status" field.
func (tcu *TestCollectionUpdate) SetOverallStatus(ts testcollection.OverallStatus) *TestCollectionUpdate {
	tcu.mutation.SetOverallStatus(ts)
	return tcu
}

// SetNillableOverallStatus sets the "overall_status" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableOverallStatus(ts *testcollection.OverallStatus) *TestCollectionUpdate {
	if ts != nil {
		tcu.SetOverallStatus(*ts)
	}
	return tcu
}

// ClearOverallStatus clears the value of the "overall_status" field.
func (tcu *TestCollectionUpdate) ClearOverallStatus() *TestCollectionUpdate {
	tcu.mutation.ClearOverallStatus()
	return tcu
}

// SetStrategy sets the "strategy" field.
func (tcu *TestCollectionUpdate) SetStrategy(s string) *TestCollectionUpdate {
	tcu.mutation.SetStrategy(s)
	return tcu
}

// SetNillableStrategy sets the "strategy" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableStrategy(s *string) *TestCollectionUpdate {
	if s != nil {
		tcu.SetStrategy(*s)
	}
	return tcu
}

// ClearStrategy clears the value of the "strategy" field.
func (tcu *TestCollectionUpdate) ClearStrategy() *TestCollectionUpdate {
	tcu.mutation.ClearStrategy()
	return tcu
}

// SetCachedLocally sets the "cached_locally" field.
func (tcu *TestCollectionUpdate) SetCachedLocally(b bool) *TestCollectionUpdate {
	tcu.mutation.SetCachedLocally(b)
	return tcu
}

// SetNillableCachedLocally sets the "cached_locally" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableCachedLocally(b *bool) *TestCollectionUpdate {
	if b != nil {
		tcu.SetCachedLocally(*b)
	}
	return tcu
}

// ClearCachedLocally clears the value of the "cached_locally" field.
func (tcu *TestCollectionUpdate) ClearCachedLocally() *TestCollectionUpdate {
	tcu.mutation.ClearCachedLocally()
	return tcu
}

// SetCachedRemotely sets the "cached_remotely" field.
func (tcu *TestCollectionUpdate) SetCachedRemotely(b bool) *TestCollectionUpdate {
	tcu.mutation.SetCachedRemotely(b)
	return tcu
}

// SetNillableCachedRemotely sets the "cached_remotely" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableCachedRemotely(b *bool) *TestCollectionUpdate {
	if b != nil {
		tcu.SetCachedRemotely(*b)
	}
	return tcu
}

// ClearCachedRemotely clears the value of the "cached_remotely" field.
func (tcu *TestCollectionUpdate) ClearCachedRemotely() *TestCollectionUpdate {
	tcu.mutation.ClearCachedRemotely()
	return tcu
}

// SetFirstSeen sets the "first_seen" field.
func (tcu *TestCollectionUpdate) SetFirstSeen(t time.Time) *TestCollectionUpdate {
	tcu.mutation.SetFirstSeen(t)
	return tcu
}

// SetNillableFirstSeen sets the "first_seen" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableFirstSeen(t *time.Time) *TestCollectionUpdate {
	if t != nil {
		tcu.SetFirstSeen(*t)
	}
	return tcu
}

// ClearFirstSeen clears the value of the "first_seen" field.
func (tcu *TestCollectionUpdate) ClearFirstSeen() *TestCollectionUpdate {
	tcu.mutation.ClearFirstSeen()
	return tcu
}

// SetDurationMs sets the "duration_ms" field.
func (tcu *TestCollectionUpdate) SetDurationMs(i int64) *TestCollectionUpdate {
	tcu.mutation.ResetDurationMs()
	tcu.mutation.SetDurationMs(i)
	return tcu
}

// SetNillableDurationMs sets the "duration_ms" field if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableDurationMs(i *int64) *TestCollectionUpdate {
	if i != nil {
		tcu.SetDurationMs(*i)
	}
	return tcu
}

// AddDurationMs adds i to the "duration_ms" field.
func (tcu *TestCollectionUpdate) AddDurationMs(i int64) *TestCollectionUpdate {
	tcu.mutation.AddDurationMs(i)
	return tcu
}

// ClearDurationMs clears the value of the "duration_ms" field.
func (tcu *TestCollectionUpdate) ClearDurationMs() *TestCollectionUpdate {
	tcu.mutation.ClearDurationMs()
	return tcu
}

// SetBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID.
func (tcu *TestCollectionUpdate) SetBazelInvocationID(id int) *TestCollectionUpdate {
	tcu.mutation.SetBazelInvocationID(id)
	return tcu
}

// SetNillableBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableBazelInvocationID(id *int) *TestCollectionUpdate {
	if id != nil {
		tcu = tcu.SetBazelInvocationID(*id)
	}
	return tcu
}

// SetBazelInvocation sets the "bazel_invocation" edge to the BazelInvocation entity.
func (tcu *TestCollectionUpdate) SetBazelInvocation(b *BazelInvocation) *TestCollectionUpdate {
	return tcu.SetBazelInvocationID(b.ID)
}

// SetTestSummaryID sets the "test_summary" edge to the TestSummary entity by ID.
func (tcu *TestCollectionUpdate) SetTestSummaryID(id int) *TestCollectionUpdate {
	tcu.mutation.SetTestSummaryID(id)
	return tcu
}

// SetNillableTestSummaryID sets the "test_summary" edge to the TestSummary entity by ID if the given value is not nil.
func (tcu *TestCollectionUpdate) SetNillableTestSummaryID(id *int) *TestCollectionUpdate {
	if id != nil {
		tcu = tcu.SetTestSummaryID(*id)
	}
	return tcu
}

// SetTestSummary sets the "test_summary" edge to the TestSummary entity.
func (tcu *TestCollectionUpdate) SetTestSummary(t *TestSummary) *TestCollectionUpdate {
	return tcu.SetTestSummaryID(t.ID)
}

// AddTestResultIDs adds the "test_results" edge to the TestResultBES entity by IDs.
func (tcu *TestCollectionUpdate) AddTestResultIDs(ids ...int) *TestCollectionUpdate {
	tcu.mutation.AddTestResultIDs(ids...)
	return tcu
}

// AddTestResults adds the "test_results" edges to the TestResultBES entity.
func (tcu *TestCollectionUpdate) AddTestResults(t ...*TestResultBES) *TestCollectionUpdate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcu.AddTestResultIDs(ids...)
}

// Mutation returns the TestCollectionMutation object of the builder.
func (tcu *TestCollectionUpdate) Mutation() *TestCollectionMutation {
	return tcu.mutation
}

// ClearBazelInvocation clears the "bazel_invocation" edge to the BazelInvocation entity.
func (tcu *TestCollectionUpdate) ClearBazelInvocation() *TestCollectionUpdate {
	tcu.mutation.ClearBazelInvocation()
	return tcu
}

// ClearTestSummary clears the "test_summary" edge to the TestSummary entity.
func (tcu *TestCollectionUpdate) ClearTestSummary() *TestCollectionUpdate {
	tcu.mutation.ClearTestSummary()
	return tcu
}

// ClearTestResults clears all "test_results" edges to the TestResultBES entity.
func (tcu *TestCollectionUpdate) ClearTestResults() *TestCollectionUpdate {
	tcu.mutation.ClearTestResults()
	return tcu
}

// RemoveTestResultIDs removes the "test_results" edge to TestResultBES entities by IDs.
func (tcu *TestCollectionUpdate) RemoveTestResultIDs(ids ...int) *TestCollectionUpdate {
	tcu.mutation.RemoveTestResultIDs(ids...)
	return tcu
}

// RemoveTestResults removes "test_results" edges to TestResultBES entities.
func (tcu *TestCollectionUpdate) RemoveTestResults(t ...*TestResultBES) *TestCollectionUpdate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcu.RemoveTestResultIDs(ids...)
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tcu *TestCollectionUpdate) check() error {
	if v, ok := tcu.mutation.OverallStatus(); ok {
		if err := testcollection.OverallStatusValidator(v); err != nil {
			return &ValidationError{Name: "overall_status", err: fmt.Errorf(`ent: validator failed for field "TestCollection.overall_status": %w`, err)}
		}
	}
	return nil
}

func (tcu *TestCollectionUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tcu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(testcollection.Table, testcollection.Columns, sqlgraph.NewFieldSpec(testcollection.FieldID, field.TypeInt))
	if ps := tcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcu.mutation.Label(); ok {
		_spec.SetField(testcollection.FieldLabel, field.TypeString, value)
	}
	if tcu.mutation.LabelCleared() {
		_spec.ClearField(testcollection.FieldLabel, field.TypeString)
	}
	if value, ok := tcu.mutation.OverallStatus(); ok {
		_spec.SetField(testcollection.FieldOverallStatus, field.TypeEnum, value)
	}
	if tcu.mutation.OverallStatusCleared() {
		_spec.ClearField(testcollection.FieldOverallStatus, field.TypeEnum)
	}
	if value, ok := tcu.mutation.Strategy(); ok {
		_spec.SetField(testcollection.FieldStrategy, field.TypeString, value)
	}
	if tcu.mutation.StrategyCleared() {
		_spec.ClearField(testcollection.FieldStrategy, field.TypeString)
	}
	if value, ok := tcu.mutation.CachedLocally(); ok {
		_spec.SetField(testcollection.FieldCachedLocally, field.TypeBool, value)
	}
	if tcu.mutation.CachedLocallyCleared() {
		_spec.ClearField(testcollection.FieldCachedLocally, field.TypeBool)
	}
	if value, ok := tcu.mutation.CachedRemotely(); ok {
		_spec.SetField(testcollection.FieldCachedRemotely, field.TypeBool, value)
	}
	if tcu.mutation.CachedRemotelyCleared() {
		_spec.ClearField(testcollection.FieldCachedRemotely, field.TypeBool)
	}
	if value, ok := tcu.mutation.FirstSeen(); ok {
		_spec.SetField(testcollection.FieldFirstSeen, field.TypeTime, value)
	}
	if tcu.mutation.FirstSeenCleared() {
		_spec.ClearField(testcollection.FieldFirstSeen, field.TypeTime)
	}
	if value, ok := tcu.mutation.DurationMs(); ok {
		_spec.SetField(testcollection.FieldDurationMs, field.TypeInt64, value)
	}
	if value, ok := tcu.mutation.AddedDurationMs(); ok {
		_spec.AddField(testcollection.FieldDurationMs, field.TypeInt64, value)
	}
	if tcu.mutation.DurationMsCleared() {
		_spec.ClearField(testcollection.FieldDurationMs, field.TypeInt64)
	}
	if tcu.mutation.BazelInvocationCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testcollection.BazelInvocationTable,
			Columns: []string{testcollection.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testcollection.BazelInvocationTable,
			Columns: []string{testcollection.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcu.mutation.TestSummaryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   testcollection.TestSummaryTable,
			Columns: []string{testcollection.TestSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testsummary.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.TestSummaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   testcollection.TestSummaryTable,
			Columns: []string{testcollection.TestSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testsummary.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcu.mutation.TestResultsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.RemovedTestResultsIDs(); len(nodes) > 0 && !tcu.mutation.TestResultsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.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 := tcu.mutation.TestResultsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, tcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{testcollection.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tcu.mutation.done = true
	return n, nil
}

// TestCollectionUpdateOne is the builder for updating a single TestCollection entity.
type TestCollectionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TestCollectionMutation
}

// SetLabel sets the "label" field.
func (tcuo *TestCollectionUpdateOne) SetLabel(s string) *TestCollectionUpdateOne {
	tcuo.mutation.SetLabel(s)
	return tcuo
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableLabel(s *string) *TestCollectionUpdateOne {
	if s != nil {
		tcuo.SetLabel(*s)
	}
	return tcuo
}

// ClearLabel clears the value of the "label" field.
func (tcuo *TestCollectionUpdateOne) ClearLabel() *TestCollectionUpdateOne {
	tcuo.mutation.ClearLabel()
	return tcuo
}

// SetOverallStatus sets the "overall_status" field.
func (tcuo *TestCollectionUpdateOne) SetOverallStatus(ts testcollection.OverallStatus) *TestCollectionUpdateOne {
	tcuo.mutation.SetOverallStatus(ts)
	return tcuo
}

// SetNillableOverallStatus sets the "overall_status" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableOverallStatus(ts *testcollection.OverallStatus) *TestCollectionUpdateOne {
	if ts != nil {
		tcuo.SetOverallStatus(*ts)
	}
	return tcuo
}

// ClearOverallStatus clears the value of the "overall_status" field.
func (tcuo *TestCollectionUpdateOne) ClearOverallStatus() *TestCollectionUpdateOne {
	tcuo.mutation.ClearOverallStatus()
	return tcuo
}

// SetStrategy sets the "strategy" field.
func (tcuo *TestCollectionUpdateOne) SetStrategy(s string) *TestCollectionUpdateOne {
	tcuo.mutation.SetStrategy(s)
	return tcuo
}

// SetNillableStrategy sets the "strategy" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableStrategy(s *string) *TestCollectionUpdateOne {
	if s != nil {
		tcuo.SetStrategy(*s)
	}
	return tcuo
}

// ClearStrategy clears the value of the "strategy" field.
func (tcuo *TestCollectionUpdateOne) ClearStrategy() *TestCollectionUpdateOne {
	tcuo.mutation.ClearStrategy()
	return tcuo
}

// SetCachedLocally sets the "cached_locally" field.
func (tcuo *TestCollectionUpdateOne) SetCachedLocally(b bool) *TestCollectionUpdateOne {
	tcuo.mutation.SetCachedLocally(b)
	return tcuo
}

// SetNillableCachedLocally sets the "cached_locally" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableCachedLocally(b *bool) *TestCollectionUpdateOne {
	if b != nil {
		tcuo.SetCachedLocally(*b)
	}
	return tcuo
}

// ClearCachedLocally clears the value of the "cached_locally" field.
func (tcuo *TestCollectionUpdateOne) ClearCachedLocally() *TestCollectionUpdateOne {
	tcuo.mutation.ClearCachedLocally()
	return tcuo
}

// SetCachedRemotely sets the "cached_remotely" field.
func (tcuo *TestCollectionUpdateOne) SetCachedRemotely(b bool) *TestCollectionUpdateOne {
	tcuo.mutation.SetCachedRemotely(b)
	return tcuo
}

// SetNillableCachedRemotely sets the "cached_remotely" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableCachedRemotely(b *bool) *TestCollectionUpdateOne {
	if b != nil {
		tcuo.SetCachedRemotely(*b)
	}
	return tcuo
}

// ClearCachedRemotely clears the value of the "cached_remotely" field.
func (tcuo *TestCollectionUpdateOne) ClearCachedRemotely() *TestCollectionUpdateOne {
	tcuo.mutation.ClearCachedRemotely()
	return tcuo
}

// SetFirstSeen sets the "first_seen" field.
func (tcuo *TestCollectionUpdateOne) SetFirstSeen(t time.Time) *TestCollectionUpdateOne {
	tcuo.mutation.SetFirstSeen(t)
	return tcuo
}

// SetNillableFirstSeen sets the "first_seen" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableFirstSeen(t *time.Time) *TestCollectionUpdateOne {
	if t != nil {
		tcuo.SetFirstSeen(*t)
	}
	return tcuo
}

// ClearFirstSeen clears the value of the "first_seen" field.
func (tcuo *TestCollectionUpdateOne) ClearFirstSeen() *TestCollectionUpdateOne {
	tcuo.mutation.ClearFirstSeen()
	return tcuo
}

// SetDurationMs sets the "duration_ms" field.
func (tcuo *TestCollectionUpdateOne) SetDurationMs(i int64) *TestCollectionUpdateOne {
	tcuo.mutation.ResetDurationMs()
	tcuo.mutation.SetDurationMs(i)
	return tcuo
}

// SetNillableDurationMs sets the "duration_ms" field if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableDurationMs(i *int64) *TestCollectionUpdateOne {
	if i != nil {
		tcuo.SetDurationMs(*i)
	}
	return tcuo
}

// AddDurationMs adds i to the "duration_ms" field.
func (tcuo *TestCollectionUpdateOne) AddDurationMs(i int64) *TestCollectionUpdateOne {
	tcuo.mutation.AddDurationMs(i)
	return tcuo
}

// ClearDurationMs clears the value of the "duration_ms" field.
func (tcuo *TestCollectionUpdateOne) ClearDurationMs() *TestCollectionUpdateOne {
	tcuo.mutation.ClearDurationMs()
	return tcuo
}

// SetBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID.
func (tcuo *TestCollectionUpdateOne) SetBazelInvocationID(id int) *TestCollectionUpdateOne {
	tcuo.mutation.SetBazelInvocationID(id)
	return tcuo
}

// SetNillableBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableBazelInvocationID(id *int) *TestCollectionUpdateOne {
	if id != nil {
		tcuo = tcuo.SetBazelInvocationID(*id)
	}
	return tcuo
}

// SetBazelInvocation sets the "bazel_invocation" edge to the BazelInvocation entity.
func (tcuo *TestCollectionUpdateOne) SetBazelInvocation(b *BazelInvocation) *TestCollectionUpdateOne {
	return tcuo.SetBazelInvocationID(b.ID)
}

// SetTestSummaryID sets the "test_summary" edge to the TestSummary entity by ID.
func (tcuo *TestCollectionUpdateOne) SetTestSummaryID(id int) *TestCollectionUpdateOne {
	tcuo.mutation.SetTestSummaryID(id)
	return tcuo
}

// SetNillableTestSummaryID sets the "test_summary" edge to the TestSummary entity by ID if the given value is not nil.
func (tcuo *TestCollectionUpdateOne) SetNillableTestSummaryID(id *int) *TestCollectionUpdateOne {
	if id != nil {
		tcuo = tcuo.SetTestSummaryID(*id)
	}
	return tcuo
}

// SetTestSummary sets the "test_summary" edge to the TestSummary entity.
func (tcuo *TestCollectionUpdateOne) SetTestSummary(t *TestSummary) *TestCollectionUpdateOne {
	return tcuo.SetTestSummaryID(t.ID)
}

// AddTestResultIDs adds the "test_results" edge to the TestResultBES entity by IDs.
func (tcuo *TestCollectionUpdateOne) AddTestResultIDs(ids ...int) *TestCollectionUpdateOne {
	tcuo.mutation.AddTestResultIDs(ids...)
	return tcuo
}

// AddTestResults adds the "test_results" edges to the TestResultBES entity.
func (tcuo *TestCollectionUpdateOne) AddTestResults(t ...*TestResultBES) *TestCollectionUpdateOne {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcuo.AddTestResultIDs(ids...)
}

// Mutation returns the TestCollectionMutation object of the builder.
func (tcuo *TestCollectionUpdateOne) Mutation() *TestCollectionMutation {
	return tcuo.mutation
}

// ClearBazelInvocation clears the "bazel_invocation" edge to the BazelInvocation entity.
func (tcuo *TestCollectionUpdateOne) ClearBazelInvocation() *TestCollectionUpdateOne {
	tcuo.mutation.ClearBazelInvocation()
	return tcuo
}

// ClearTestSummary clears the "test_summary" edge to the TestSummary entity.
func (tcuo *TestCollectionUpdateOne) ClearTestSummary() *TestCollectionUpdateOne {
	tcuo.mutation.ClearTestSummary()
	return tcuo
}

// ClearTestResults clears all "test_results" edges to the TestResultBES entity.
func (tcuo *TestCollectionUpdateOne) ClearTestResults() *TestCollectionUpdateOne {
	tcuo.mutation.ClearTestResults()
	return tcuo
}

// RemoveTestResultIDs removes the "test_results" edge to TestResultBES entities by IDs.
func (tcuo *TestCollectionUpdateOne) RemoveTestResultIDs(ids ...int) *TestCollectionUpdateOne {
	tcuo.mutation.RemoveTestResultIDs(ids...)
	return tcuo
}

// RemoveTestResults removes "test_results" edges to TestResultBES entities.
func (tcuo *TestCollectionUpdateOne) RemoveTestResults(t ...*TestResultBES) *TestCollectionUpdateOne {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tcuo.RemoveTestResultIDs(ids...)
}

// Where appends a list predicates to the TestCollectionUpdate builder.
func (tcuo *TestCollectionUpdateOne) Where(ps ...predicate.TestCollection) *TestCollectionUpdateOne {
	tcuo.mutation.Where(ps...)
	return tcuo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tcuo *TestCollectionUpdateOne) check() error {
	if v, ok := tcuo.mutation.OverallStatus(); ok {
		if err := testcollection.OverallStatusValidator(v); err != nil {
			return &ValidationError{Name: "overall_status", err: fmt.Errorf(`ent: validator failed for field "TestCollection.overall_status": %w`, err)}
		}
	}
	return nil
}

func (tcuo *TestCollectionUpdateOne) sqlSave(ctx context.Context) (_node *TestCollection, err error) {
	if err := tcuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(testcollection.Table, testcollection.Columns, sqlgraph.NewFieldSpec(testcollection.FieldID, field.TypeInt))
	id, ok := tcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TestCollection.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, testcollection.FieldID)
		for _, f := range fields {
			if !testcollection.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != testcollection.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcuo.mutation.Label(); ok {
		_spec.SetField(testcollection.FieldLabel, field.TypeString, value)
	}
	if tcuo.mutation.LabelCleared() {
		_spec.ClearField(testcollection.FieldLabel, field.TypeString)
	}
	if value, ok := tcuo.mutation.OverallStatus(); ok {
		_spec.SetField(testcollection.FieldOverallStatus, field.TypeEnum, value)
	}
	if tcuo.mutation.OverallStatusCleared() {
		_spec.ClearField(testcollection.FieldOverallStatus, field.TypeEnum)
	}
	if value, ok := tcuo.mutation.Strategy(); ok {
		_spec.SetField(testcollection.FieldStrategy, field.TypeString, value)
	}
	if tcuo.mutation.StrategyCleared() {
		_spec.ClearField(testcollection.FieldStrategy, field.TypeString)
	}
	if value, ok := tcuo.mutation.CachedLocally(); ok {
		_spec.SetField(testcollection.FieldCachedLocally, field.TypeBool, value)
	}
	if tcuo.mutation.CachedLocallyCleared() {
		_spec.ClearField(testcollection.FieldCachedLocally, field.TypeBool)
	}
	if value, ok := tcuo.mutation.CachedRemotely(); ok {
		_spec.SetField(testcollection.FieldCachedRemotely, field.TypeBool, value)
	}
	if tcuo.mutation.CachedRemotelyCleared() {
		_spec.ClearField(testcollection.FieldCachedRemotely, field.TypeBool)
	}
	if value, ok := tcuo.mutation.FirstSeen(); ok {
		_spec.SetField(testcollection.FieldFirstSeen, field.TypeTime, value)
	}
	if tcuo.mutation.FirstSeenCleared() {
		_spec.ClearField(testcollection.FieldFirstSeen, field.TypeTime)
	}
	if value, ok := tcuo.mutation.DurationMs(); ok {
		_spec.SetField(testcollection.FieldDurationMs, field.TypeInt64, value)
	}
	if value, ok := tcuo.mutation.AddedDurationMs(); ok {
		_spec.AddField(testcollection.FieldDurationMs, field.TypeInt64, value)
	}
	if tcuo.mutation.DurationMsCleared() {
		_spec.ClearField(testcollection.FieldDurationMs, field.TypeInt64)
	}
	if tcuo.mutation.BazelInvocationCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testcollection.BazelInvocationTable,
			Columns: []string{testcollection.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testcollection.BazelInvocationTable,
			Columns: []string{testcollection.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcuo.mutation.TestSummaryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   testcollection.TestSummaryTable,
			Columns: []string{testcollection.TestSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testsummary.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.TestSummaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   testcollection.TestSummaryTable,
			Columns: []string{testcollection.TestSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testsummary.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tcuo.mutation.TestResultsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcuo.mutation.RemovedTestResultsIDs(); len(nodes) > 0 && !tcuo.mutation.TestResultsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.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 := tcuo.mutation.TestResultsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   testcollection.TestResultsTable,
			Columns: []string{testcollection.TestResultsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &TestCollection{config: tcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{testcollection.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tcuo.mutation.done = true
	return _node, nil
}
