machine_update.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "time"
  7. "entgo.io/ent/dialect/sql"
  8. "entgo.io/ent/dialect/sql/sqlgraph"
  9. "entgo.io/ent/schema/field"
  10. "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
  11. "github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
  12. "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
  13. )
  14. // MachineUpdate is the builder for updating Machine entities.
  15. type MachineUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *MachineMutation
  19. }
  20. // Where appends a list predicates to the MachineUpdate builder.
  21. func (mu *MachineUpdate) Where(ps ...predicate.Machine) *MachineUpdate {
  22. mu.mutation.Where(ps...)
  23. return mu
  24. }
  25. // SetCreatedAt sets the "created_at" field.
  26. func (mu *MachineUpdate) SetCreatedAt(t time.Time) *MachineUpdate {
  27. mu.mutation.SetCreatedAt(t)
  28. return mu
  29. }
  30. // ClearCreatedAt clears the value of the "created_at" field.
  31. func (mu *MachineUpdate) ClearCreatedAt() *MachineUpdate {
  32. mu.mutation.ClearCreatedAt()
  33. return mu
  34. }
  35. // SetUpdatedAt sets the "updated_at" field.
  36. func (mu *MachineUpdate) SetUpdatedAt(t time.Time) *MachineUpdate {
  37. mu.mutation.SetUpdatedAt(t)
  38. return mu
  39. }
  40. // ClearUpdatedAt clears the value of the "updated_at" field.
  41. func (mu *MachineUpdate) ClearUpdatedAt() *MachineUpdate {
  42. mu.mutation.ClearUpdatedAt()
  43. return mu
  44. }
  45. // SetLastPush sets the "last_push" field.
  46. func (mu *MachineUpdate) SetLastPush(t time.Time) *MachineUpdate {
  47. mu.mutation.SetLastPush(t)
  48. return mu
  49. }
  50. // ClearLastPush clears the value of the "last_push" field.
  51. func (mu *MachineUpdate) ClearLastPush() *MachineUpdate {
  52. mu.mutation.ClearLastPush()
  53. return mu
  54. }
  55. // SetMachineId sets the "machineId" field.
  56. func (mu *MachineUpdate) SetMachineId(s string) *MachineUpdate {
  57. mu.mutation.SetMachineId(s)
  58. return mu
  59. }
  60. // SetPassword sets the "password" field.
  61. func (mu *MachineUpdate) SetPassword(s string) *MachineUpdate {
  62. mu.mutation.SetPassword(s)
  63. return mu
  64. }
  65. // SetIpAddress sets the "ipAddress" field.
  66. func (mu *MachineUpdate) SetIpAddress(s string) *MachineUpdate {
  67. mu.mutation.SetIpAddress(s)
  68. return mu
  69. }
  70. // SetScenarios sets the "scenarios" field.
  71. func (mu *MachineUpdate) SetScenarios(s string) *MachineUpdate {
  72. mu.mutation.SetScenarios(s)
  73. return mu
  74. }
  75. // SetNillableScenarios sets the "scenarios" field if the given value is not nil.
  76. func (mu *MachineUpdate) SetNillableScenarios(s *string) *MachineUpdate {
  77. if s != nil {
  78. mu.SetScenarios(*s)
  79. }
  80. return mu
  81. }
  82. // ClearScenarios clears the value of the "scenarios" field.
  83. func (mu *MachineUpdate) ClearScenarios() *MachineUpdate {
  84. mu.mutation.ClearScenarios()
  85. return mu
  86. }
  87. // SetVersion sets the "version" field.
  88. func (mu *MachineUpdate) SetVersion(s string) *MachineUpdate {
  89. mu.mutation.SetVersion(s)
  90. return mu
  91. }
  92. // SetNillableVersion sets the "version" field if the given value is not nil.
  93. func (mu *MachineUpdate) SetNillableVersion(s *string) *MachineUpdate {
  94. if s != nil {
  95. mu.SetVersion(*s)
  96. }
  97. return mu
  98. }
  99. // ClearVersion clears the value of the "version" field.
  100. func (mu *MachineUpdate) ClearVersion() *MachineUpdate {
  101. mu.mutation.ClearVersion()
  102. return mu
  103. }
  104. // SetIsValidated sets the "isValidated" field.
  105. func (mu *MachineUpdate) SetIsValidated(b bool) *MachineUpdate {
  106. mu.mutation.SetIsValidated(b)
  107. return mu
  108. }
  109. // SetNillableIsValidated sets the "isValidated" field if the given value is not nil.
  110. func (mu *MachineUpdate) SetNillableIsValidated(b *bool) *MachineUpdate {
  111. if b != nil {
  112. mu.SetIsValidated(*b)
  113. }
  114. return mu
  115. }
  116. // SetStatus sets the "status" field.
  117. func (mu *MachineUpdate) SetStatus(s string) *MachineUpdate {
  118. mu.mutation.SetStatus(s)
  119. return mu
  120. }
  121. // SetNillableStatus sets the "status" field if the given value is not nil.
  122. func (mu *MachineUpdate) SetNillableStatus(s *string) *MachineUpdate {
  123. if s != nil {
  124. mu.SetStatus(*s)
  125. }
  126. return mu
  127. }
  128. // ClearStatus clears the value of the "status" field.
  129. func (mu *MachineUpdate) ClearStatus() *MachineUpdate {
  130. mu.mutation.ClearStatus()
  131. return mu
  132. }
  133. // AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
  134. func (mu *MachineUpdate) AddAlertIDs(ids ...int) *MachineUpdate {
  135. mu.mutation.AddAlertIDs(ids...)
  136. return mu
  137. }
  138. // AddAlerts adds the "alerts" edges to the Alert entity.
  139. func (mu *MachineUpdate) AddAlerts(a ...*Alert) *MachineUpdate {
  140. ids := make([]int, len(a))
  141. for i := range a {
  142. ids[i] = a[i].ID
  143. }
  144. return mu.AddAlertIDs(ids...)
  145. }
  146. // Mutation returns the MachineMutation object of the builder.
  147. func (mu *MachineUpdate) Mutation() *MachineMutation {
  148. return mu.mutation
  149. }
  150. // ClearAlerts clears all "alerts" edges to the Alert entity.
  151. func (mu *MachineUpdate) ClearAlerts() *MachineUpdate {
  152. mu.mutation.ClearAlerts()
  153. return mu
  154. }
  155. // RemoveAlertIDs removes the "alerts" edge to Alert entities by IDs.
  156. func (mu *MachineUpdate) RemoveAlertIDs(ids ...int) *MachineUpdate {
  157. mu.mutation.RemoveAlertIDs(ids...)
  158. return mu
  159. }
  160. // RemoveAlerts removes "alerts" edges to Alert entities.
  161. func (mu *MachineUpdate) RemoveAlerts(a ...*Alert) *MachineUpdate {
  162. ids := make([]int, len(a))
  163. for i := range a {
  164. ids[i] = a[i].ID
  165. }
  166. return mu.RemoveAlertIDs(ids...)
  167. }
  168. // Save executes the query and returns the number of nodes affected by the update operation.
  169. func (mu *MachineUpdate) Save(ctx context.Context) (int, error) {
  170. var (
  171. err error
  172. affected int
  173. )
  174. mu.defaults()
  175. if len(mu.hooks) == 0 {
  176. if err = mu.check(); err != nil {
  177. return 0, err
  178. }
  179. affected, err = mu.sqlSave(ctx)
  180. } else {
  181. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  182. mutation, ok := m.(*MachineMutation)
  183. if !ok {
  184. return nil, fmt.Errorf("unexpected mutation type %T", m)
  185. }
  186. if err = mu.check(); err != nil {
  187. return 0, err
  188. }
  189. mu.mutation = mutation
  190. affected, err = mu.sqlSave(ctx)
  191. mutation.done = true
  192. return affected, err
  193. })
  194. for i := len(mu.hooks) - 1; i >= 0; i-- {
  195. if mu.hooks[i] == nil {
  196. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  197. }
  198. mut = mu.hooks[i](mut)
  199. }
  200. if _, err := mut.Mutate(ctx, mu.mutation); err != nil {
  201. return 0, err
  202. }
  203. }
  204. return affected, err
  205. }
  206. // SaveX is like Save, but panics if an error occurs.
  207. func (mu *MachineUpdate) SaveX(ctx context.Context) int {
  208. affected, err := mu.Save(ctx)
  209. if err != nil {
  210. panic(err)
  211. }
  212. return affected
  213. }
  214. // Exec executes the query.
  215. func (mu *MachineUpdate) Exec(ctx context.Context) error {
  216. _, err := mu.Save(ctx)
  217. return err
  218. }
  219. // ExecX is like Exec, but panics if an error occurs.
  220. func (mu *MachineUpdate) ExecX(ctx context.Context) {
  221. if err := mu.Exec(ctx); err != nil {
  222. panic(err)
  223. }
  224. }
  225. // defaults sets the default values of the builder before save.
  226. func (mu *MachineUpdate) defaults() {
  227. if _, ok := mu.mutation.CreatedAt(); !ok && !mu.mutation.CreatedAtCleared() {
  228. v := machine.UpdateDefaultCreatedAt()
  229. mu.mutation.SetCreatedAt(v)
  230. }
  231. if _, ok := mu.mutation.UpdatedAt(); !ok && !mu.mutation.UpdatedAtCleared() {
  232. v := machine.UpdateDefaultUpdatedAt()
  233. mu.mutation.SetUpdatedAt(v)
  234. }
  235. if _, ok := mu.mutation.LastPush(); !ok && !mu.mutation.LastPushCleared() {
  236. v := machine.UpdateDefaultLastPush()
  237. mu.mutation.SetLastPush(v)
  238. }
  239. }
  240. // check runs all checks and user-defined validators on the builder.
  241. func (mu *MachineUpdate) check() error {
  242. if v, ok := mu.mutation.Scenarios(); ok {
  243. if err := machine.ScenariosValidator(v); err != nil {
  244. return &ValidationError{Name: "scenarios", err: fmt.Errorf("ent: validator failed for field \"scenarios\": %w", err)}
  245. }
  246. }
  247. return nil
  248. }
  249. func (mu *MachineUpdate) sqlSave(ctx context.Context) (n int, err error) {
  250. _spec := &sqlgraph.UpdateSpec{
  251. Node: &sqlgraph.NodeSpec{
  252. Table: machine.Table,
  253. Columns: machine.Columns,
  254. ID: &sqlgraph.FieldSpec{
  255. Type: field.TypeInt,
  256. Column: machine.FieldID,
  257. },
  258. },
  259. }
  260. if ps := mu.mutation.predicates; len(ps) > 0 {
  261. _spec.Predicate = func(selector *sql.Selector) {
  262. for i := range ps {
  263. ps[i](selector)
  264. }
  265. }
  266. }
  267. if value, ok := mu.mutation.CreatedAt(); ok {
  268. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  269. Type: field.TypeTime,
  270. Value: value,
  271. Column: machine.FieldCreatedAt,
  272. })
  273. }
  274. if mu.mutation.CreatedAtCleared() {
  275. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  276. Type: field.TypeTime,
  277. Column: machine.FieldCreatedAt,
  278. })
  279. }
  280. if value, ok := mu.mutation.UpdatedAt(); ok {
  281. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  282. Type: field.TypeTime,
  283. Value: value,
  284. Column: machine.FieldUpdatedAt,
  285. })
  286. }
  287. if mu.mutation.UpdatedAtCleared() {
  288. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  289. Type: field.TypeTime,
  290. Column: machine.FieldUpdatedAt,
  291. })
  292. }
  293. if value, ok := mu.mutation.LastPush(); ok {
  294. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  295. Type: field.TypeTime,
  296. Value: value,
  297. Column: machine.FieldLastPush,
  298. })
  299. }
  300. if mu.mutation.LastPushCleared() {
  301. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  302. Type: field.TypeTime,
  303. Column: machine.FieldLastPush,
  304. })
  305. }
  306. if value, ok := mu.mutation.MachineId(); ok {
  307. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  308. Type: field.TypeString,
  309. Value: value,
  310. Column: machine.FieldMachineId,
  311. })
  312. }
  313. if value, ok := mu.mutation.Password(); ok {
  314. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  315. Type: field.TypeString,
  316. Value: value,
  317. Column: machine.FieldPassword,
  318. })
  319. }
  320. if value, ok := mu.mutation.IpAddress(); ok {
  321. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  322. Type: field.TypeString,
  323. Value: value,
  324. Column: machine.FieldIpAddress,
  325. })
  326. }
  327. if value, ok := mu.mutation.Scenarios(); ok {
  328. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  329. Type: field.TypeString,
  330. Value: value,
  331. Column: machine.FieldScenarios,
  332. })
  333. }
  334. if mu.mutation.ScenariosCleared() {
  335. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  336. Type: field.TypeString,
  337. Column: machine.FieldScenarios,
  338. })
  339. }
  340. if value, ok := mu.mutation.Version(); ok {
  341. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  342. Type: field.TypeString,
  343. Value: value,
  344. Column: machine.FieldVersion,
  345. })
  346. }
  347. if mu.mutation.VersionCleared() {
  348. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  349. Type: field.TypeString,
  350. Column: machine.FieldVersion,
  351. })
  352. }
  353. if value, ok := mu.mutation.IsValidated(); ok {
  354. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  355. Type: field.TypeBool,
  356. Value: value,
  357. Column: machine.FieldIsValidated,
  358. })
  359. }
  360. if value, ok := mu.mutation.Status(); ok {
  361. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  362. Type: field.TypeString,
  363. Value: value,
  364. Column: machine.FieldStatus,
  365. })
  366. }
  367. if mu.mutation.StatusCleared() {
  368. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  369. Type: field.TypeString,
  370. Column: machine.FieldStatus,
  371. })
  372. }
  373. if mu.mutation.AlertsCleared() {
  374. edge := &sqlgraph.EdgeSpec{
  375. Rel: sqlgraph.O2M,
  376. Inverse: false,
  377. Table: machine.AlertsTable,
  378. Columns: []string{machine.AlertsColumn},
  379. Bidi: false,
  380. Target: &sqlgraph.EdgeTarget{
  381. IDSpec: &sqlgraph.FieldSpec{
  382. Type: field.TypeInt,
  383. Column: alert.FieldID,
  384. },
  385. },
  386. }
  387. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  388. }
  389. if nodes := mu.mutation.RemovedAlertsIDs(); len(nodes) > 0 && !mu.mutation.AlertsCleared() {
  390. edge := &sqlgraph.EdgeSpec{
  391. Rel: sqlgraph.O2M,
  392. Inverse: false,
  393. Table: machine.AlertsTable,
  394. Columns: []string{machine.AlertsColumn},
  395. Bidi: false,
  396. Target: &sqlgraph.EdgeTarget{
  397. IDSpec: &sqlgraph.FieldSpec{
  398. Type: field.TypeInt,
  399. Column: alert.FieldID,
  400. },
  401. },
  402. }
  403. for _, k := range nodes {
  404. edge.Target.Nodes = append(edge.Target.Nodes, k)
  405. }
  406. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  407. }
  408. if nodes := mu.mutation.AlertsIDs(); len(nodes) > 0 {
  409. edge := &sqlgraph.EdgeSpec{
  410. Rel: sqlgraph.O2M,
  411. Inverse: false,
  412. Table: machine.AlertsTable,
  413. Columns: []string{machine.AlertsColumn},
  414. Bidi: false,
  415. Target: &sqlgraph.EdgeTarget{
  416. IDSpec: &sqlgraph.FieldSpec{
  417. Type: field.TypeInt,
  418. Column: alert.FieldID,
  419. },
  420. },
  421. }
  422. for _, k := range nodes {
  423. edge.Target.Nodes = append(edge.Target.Nodes, k)
  424. }
  425. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  426. }
  427. if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
  428. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  429. err = &NotFoundError{machine.Label}
  430. } else if sqlgraph.IsConstraintError(err) {
  431. err = &ConstraintError{err.Error(), err}
  432. }
  433. return 0, err
  434. }
  435. return n, nil
  436. }
  437. // MachineUpdateOne is the builder for updating a single Machine entity.
  438. type MachineUpdateOne struct {
  439. config
  440. fields []string
  441. hooks []Hook
  442. mutation *MachineMutation
  443. }
  444. // SetCreatedAt sets the "created_at" field.
  445. func (muo *MachineUpdateOne) SetCreatedAt(t time.Time) *MachineUpdateOne {
  446. muo.mutation.SetCreatedAt(t)
  447. return muo
  448. }
  449. // ClearCreatedAt clears the value of the "created_at" field.
  450. func (muo *MachineUpdateOne) ClearCreatedAt() *MachineUpdateOne {
  451. muo.mutation.ClearCreatedAt()
  452. return muo
  453. }
  454. // SetUpdatedAt sets the "updated_at" field.
  455. func (muo *MachineUpdateOne) SetUpdatedAt(t time.Time) *MachineUpdateOne {
  456. muo.mutation.SetUpdatedAt(t)
  457. return muo
  458. }
  459. // ClearUpdatedAt clears the value of the "updated_at" field.
  460. func (muo *MachineUpdateOne) ClearUpdatedAt() *MachineUpdateOne {
  461. muo.mutation.ClearUpdatedAt()
  462. return muo
  463. }
  464. // SetLastPush sets the "last_push" field.
  465. func (muo *MachineUpdateOne) SetLastPush(t time.Time) *MachineUpdateOne {
  466. muo.mutation.SetLastPush(t)
  467. return muo
  468. }
  469. // ClearLastPush clears the value of the "last_push" field.
  470. func (muo *MachineUpdateOne) ClearLastPush() *MachineUpdateOne {
  471. muo.mutation.ClearLastPush()
  472. return muo
  473. }
  474. // SetMachineId sets the "machineId" field.
  475. func (muo *MachineUpdateOne) SetMachineId(s string) *MachineUpdateOne {
  476. muo.mutation.SetMachineId(s)
  477. return muo
  478. }
  479. // SetPassword sets the "password" field.
  480. func (muo *MachineUpdateOne) SetPassword(s string) *MachineUpdateOne {
  481. muo.mutation.SetPassword(s)
  482. return muo
  483. }
  484. // SetIpAddress sets the "ipAddress" field.
  485. func (muo *MachineUpdateOne) SetIpAddress(s string) *MachineUpdateOne {
  486. muo.mutation.SetIpAddress(s)
  487. return muo
  488. }
  489. // SetScenarios sets the "scenarios" field.
  490. func (muo *MachineUpdateOne) SetScenarios(s string) *MachineUpdateOne {
  491. muo.mutation.SetScenarios(s)
  492. return muo
  493. }
  494. // SetNillableScenarios sets the "scenarios" field if the given value is not nil.
  495. func (muo *MachineUpdateOne) SetNillableScenarios(s *string) *MachineUpdateOne {
  496. if s != nil {
  497. muo.SetScenarios(*s)
  498. }
  499. return muo
  500. }
  501. // ClearScenarios clears the value of the "scenarios" field.
  502. func (muo *MachineUpdateOne) ClearScenarios() *MachineUpdateOne {
  503. muo.mutation.ClearScenarios()
  504. return muo
  505. }
  506. // SetVersion sets the "version" field.
  507. func (muo *MachineUpdateOne) SetVersion(s string) *MachineUpdateOne {
  508. muo.mutation.SetVersion(s)
  509. return muo
  510. }
  511. // SetNillableVersion sets the "version" field if the given value is not nil.
  512. func (muo *MachineUpdateOne) SetNillableVersion(s *string) *MachineUpdateOne {
  513. if s != nil {
  514. muo.SetVersion(*s)
  515. }
  516. return muo
  517. }
  518. // ClearVersion clears the value of the "version" field.
  519. func (muo *MachineUpdateOne) ClearVersion() *MachineUpdateOne {
  520. muo.mutation.ClearVersion()
  521. return muo
  522. }
  523. // SetIsValidated sets the "isValidated" field.
  524. func (muo *MachineUpdateOne) SetIsValidated(b bool) *MachineUpdateOne {
  525. muo.mutation.SetIsValidated(b)
  526. return muo
  527. }
  528. // SetNillableIsValidated sets the "isValidated" field if the given value is not nil.
  529. func (muo *MachineUpdateOne) SetNillableIsValidated(b *bool) *MachineUpdateOne {
  530. if b != nil {
  531. muo.SetIsValidated(*b)
  532. }
  533. return muo
  534. }
  535. // SetStatus sets the "status" field.
  536. func (muo *MachineUpdateOne) SetStatus(s string) *MachineUpdateOne {
  537. muo.mutation.SetStatus(s)
  538. return muo
  539. }
  540. // SetNillableStatus sets the "status" field if the given value is not nil.
  541. func (muo *MachineUpdateOne) SetNillableStatus(s *string) *MachineUpdateOne {
  542. if s != nil {
  543. muo.SetStatus(*s)
  544. }
  545. return muo
  546. }
  547. // ClearStatus clears the value of the "status" field.
  548. func (muo *MachineUpdateOne) ClearStatus() *MachineUpdateOne {
  549. muo.mutation.ClearStatus()
  550. return muo
  551. }
  552. // AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
  553. func (muo *MachineUpdateOne) AddAlertIDs(ids ...int) *MachineUpdateOne {
  554. muo.mutation.AddAlertIDs(ids...)
  555. return muo
  556. }
  557. // AddAlerts adds the "alerts" edges to the Alert entity.
  558. func (muo *MachineUpdateOne) AddAlerts(a ...*Alert) *MachineUpdateOne {
  559. ids := make([]int, len(a))
  560. for i := range a {
  561. ids[i] = a[i].ID
  562. }
  563. return muo.AddAlertIDs(ids...)
  564. }
  565. // Mutation returns the MachineMutation object of the builder.
  566. func (muo *MachineUpdateOne) Mutation() *MachineMutation {
  567. return muo.mutation
  568. }
  569. // ClearAlerts clears all "alerts" edges to the Alert entity.
  570. func (muo *MachineUpdateOne) ClearAlerts() *MachineUpdateOne {
  571. muo.mutation.ClearAlerts()
  572. return muo
  573. }
  574. // RemoveAlertIDs removes the "alerts" edge to Alert entities by IDs.
  575. func (muo *MachineUpdateOne) RemoveAlertIDs(ids ...int) *MachineUpdateOne {
  576. muo.mutation.RemoveAlertIDs(ids...)
  577. return muo
  578. }
  579. // RemoveAlerts removes "alerts" edges to Alert entities.
  580. func (muo *MachineUpdateOne) RemoveAlerts(a ...*Alert) *MachineUpdateOne {
  581. ids := make([]int, len(a))
  582. for i := range a {
  583. ids[i] = a[i].ID
  584. }
  585. return muo.RemoveAlertIDs(ids...)
  586. }
  587. // Select allows selecting one or more fields (columns) of the returned entity.
  588. // The default is selecting all fields defined in the entity schema.
  589. func (muo *MachineUpdateOne) Select(field string, fields ...string) *MachineUpdateOne {
  590. muo.fields = append([]string{field}, fields...)
  591. return muo
  592. }
  593. // Save executes the query and returns the updated Machine entity.
  594. func (muo *MachineUpdateOne) Save(ctx context.Context) (*Machine, error) {
  595. var (
  596. err error
  597. node *Machine
  598. )
  599. muo.defaults()
  600. if len(muo.hooks) == 0 {
  601. if err = muo.check(); err != nil {
  602. return nil, err
  603. }
  604. node, err = muo.sqlSave(ctx)
  605. } else {
  606. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  607. mutation, ok := m.(*MachineMutation)
  608. if !ok {
  609. return nil, fmt.Errorf("unexpected mutation type %T", m)
  610. }
  611. if err = muo.check(); err != nil {
  612. return nil, err
  613. }
  614. muo.mutation = mutation
  615. node, err = muo.sqlSave(ctx)
  616. mutation.done = true
  617. return node, err
  618. })
  619. for i := len(muo.hooks) - 1; i >= 0; i-- {
  620. if muo.hooks[i] == nil {
  621. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  622. }
  623. mut = muo.hooks[i](mut)
  624. }
  625. if _, err := mut.Mutate(ctx, muo.mutation); err != nil {
  626. return nil, err
  627. }
  628. }
  629. return node, err
  630. }
  631. // SaveX is like Save, but panics if an error occurs.
  632. func (muo *MachineUpdateOne) SaveX(ctx context.Context) *Machine {
  633. node, err := muo.Save(ctx)
  634. if err != nil {
  635. panic(err)
  636. }
  637. return node
  638. }
  639. // Exec executes the query on the entity.
  640. func (muo *MachineUpdateOne) Exec(ctx context.Context) error {
  641. _, err := muo.Save(ctx)
  642. return err
  643. }
  644. // ExecX is like Exec, but panics if an error occurs.
  645. func (muo *MachineUpdateOne) ExecX(ctx context.Context) {
  646. if err := muo.Exec(ctx); err != nil {
  647. panic(err)
  648. }
  649. }
  650. // defaults sets the default values of the builder before save.
  651. func (muo *MachineUpdateOne) defaults() {
  652. if _, ok := muo.mutation.CreatedAt(); !ok && !muo.mutation.CreatedAtCleared() {
  653. v := machine.UpdateDefaultCreatedAt()
  654. muo.mutation.SetCreatedAt(v)
  655. }
  656. if _, ok := muo.mutation.UpdatedAt(); !ok && !muo.mutation.UpdatedAtCleared() {
  657. v := machine.UpdateDefaultUpdatedAt()
  658. muo.mutation.SetUpdatedAt(v)
  659. }
  660. if _, ok := muo.mutation.LastPush(); !ok && !muo.mutation.LastPushCleared() {
  661. v := machine.UpdateDefaultLastPush()
  662. muo.mutation.SetLastPush(v)
  663. }
  664. }
  665. // check runs all checks and user-defined validators on the builder.
  666. func (muo *MachineUpdateOne) check() error {
  667. if v, ok := muo.mutation.Scenarios(); ok {
  668. if err := machine.ScenariosValidator(v); err != nil {
  669. return &ValidationError{Name: "scenarios", err: fmt.Errorf("ent: validator failed for field \"scenarios\": %w", err)}
  670. }
  671. }
  672. return nil
  673. }
  674. func (muo *MachineUpdateOne) sqlSave(ctx context.Context) (_node *Machine, err error) {
  675. _spec := &sqlgraph.UpdateSpec{
  676. Node: &sqlgraph.NodeSpec{
  677. Table: machine.Table,
  678. Columns: machine.Columns,
  679. ID: &sqlgraph.FieldSpec{
  680. Type: field.TypeInt,
  681. Column: machine.FieldID,
  682. },
  683. },
  684. }
  685. id, ok := muo.mutation.ID()
  686. if !ok {
  687. return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Machine.ID for update")}
  688. }
  689. _spec.Node.ID.Value = id
  690. if fields := muo.fields; len(fields) > 0 {
  691. _spec.Node.Columns = make([]string, 0, len(fields))
  692. _spec.Node.Columns = append(_spec.Node.Columns, machine.FieldID)
  693. for _, f := range fields {
  694. if !machine.ValidColumn(f) {
  695. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  696. }
  697. if f != machine.FieldID {
  698. _spec.Node.Columns = append(_spec.Node.Columns, f)
  699. }
  700. }
  701. }
  702. if ps := muo.mutation.predicates; len(ps) > 0 {
  703. _spec.Predicate = func(selector *sql.Selector) {
  704. for i := range ps {
  705. ps[i](selector)
  706. }
  707. }
  708. }
  709. if value, ok := muo.mutation.CreatedAt(); ok {
  710. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  711. Type: field.TypeTime,
  712. Value: value,
  713. Column: machine.FieldCreatedAt,
  714. })
  715. }
  716. if muo.mutation.CreatedAtCleared() {
  717. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  718. Type: field.TypeTime,
  719. Column: machine.FieldCreatedAt,
  720. })
  721. }
  722. if value, ok := muo.mutation.UpdatedAt(); ok {
  723. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  724. Type: field.TypeTime,
  725. Value: value,
  726. Column: machine.FieldUpdatedAt,
  727. })
  728. }
  729. if muo.mutation.UpdatedAtCleared() {
  730. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  731. Type: field.TypeTime,
  732. Column: machine.FieldUpdatedAt,
  733. })
  734. }
  735. if value, ok := muo.mutation.LastPush(); ok {
  736. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  737. Type: field.TypeTime,
  738. Value: value,
  739. Column: machine.FieldLastPush,
  740. })
  741. }
  742. if muo.mutation.LastPushCleared() {
  743. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  744. Type: field.TypeTime,
  745. Column: machine.FieldLastPush,
  746. })
  747. }
  748. if value, ok := muo.mutation.MachineId(); ok {
  749. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  750. Type: field.TypeString,
  751. Value: value,
  752. Column: machine.FieldMachineId,
  753. })
  754. }
  755. if value, ok := muo.mutation.Password(); ok {
  756. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  757. Type: field.TypeString,
  758. Value: value,
  759. Column: machine.FieldPassword,
  760. })
  761. }
  762. if value, ok := muo.mutation.IpAddress(); ok {
  763. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  764. Type: field.TypeString,
  765. Value: value,
  766. Column: machine.FieldIpAddress,
  767. })
  768. }
  769. if value, ok := muo.mutation.Scenarios(); ok {
  770. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  771. Type: field.TypeString,
  772. Value: value,
  773. Column: machine.FieldScenarios,
  774. })
  775. }
  776. if muo.mutation.ScenariosCleared() {
  777. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  778. Type: field.TypeString,
  779. Column: machine.FieldScenarios,
  780. })
  781. }
  782. if value, ok := muo.mutation.Version(); ok {
  783. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  784. Type: field.TypeString,
  785. Value: value,
  786. Column: machine.FieldVersion,
  787. })
  788. }
  789. if muo.mutation.VersionCleared() {
  790. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  791. Type: field.TypeString,
  792. Column: machine.FieldVersion,
  793. })
  794. }
  795. if value, ok := muo.mutation.IsValidated(); ok {
  796. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  797. Type: field.TypeBool,
  798. Value: value,
  799. Column: machine.FieldIsValidated,
  800. })
  801. }
  802. if value, ok := muo.mutation.Status(); ok {
  803. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  804. Type: field.TypeString,
  805. Value: value,
  806. Column: machine.FieldStatus,
  807. })
  808. }
  809. if muo.mutation.StatusCleared() {
  810. _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
  811. Type: field.TypeString,
  812. Column: machine.FieldStatus,
  813. })
  814. }
  815. if muo.mutation.AlertsCleared() {
  816. edge := &sqlgraph.EdgeSpec{
  817. Rel: sqlgraph.O2M,
  818. Inverse: false,
  819. Table: machine.AlertsTable,
  820. Columns: []string{machine.AlertsColumn},
  821. Bidi: false,
  822. Target: &sqlgraph.EdgeTarget{
  823. IDSpec: &sqlgraph.FieldSpec{
  824. Type: field.TypeInt,
  825. Column: alert.FieldID,
  826. },
  827. },
  828. }
  829. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  830. }
  831. if nodes := muo.mutation.RemovedAlertsIDs(); len(nodes) > 0 && !muo.mutation.AlertsCleared() {
  832. edge := &sqlgraph.EdgeSpec{
  833. Rel: sqlgraph.O2M,
  834. Inverse: false,
  835. Table: machine.AlertsTable,
  836. Columns: []string{machine.AlertsColumn},
  837. Bidi: false,
  838. Target: &sqlgraph.EdgeTarget{
  839. IDSpec: &sqlgraph.FieldSpec{
  840. Type: field.TypeInt,
  841. Column: alert.FieldID,
  842. },
  843. },
  844. }
  845. for _, k := range nodes {
  846. edge.Target.Nodes = append(edge.Target.Nodes, k)
  847. }
  848. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  849. }
  850. if nodes := muo.mutation.AlertsIDs(); len(nodes) > 0 {
  851. edge := &sqlgraph.EdgeSpec{
  852. Rel: sqlgraph.O2M,
  853. Inverse: false,
  854. Table: machine.AlertsTable,
  855. Columns: []string{machine.AlertsColumn},
  856. Bidi: false,
  857. Target: &sqlgraph.EdgeTarget{
  858. IDSpec: &sqlgraph.FieldSpec{
  859. Type: field.TypeInt,
  860. Column: alert.FieldID,
  861. },
  862. },
  863. }
  864. for _, k := range nodes {
  865. edge.Target.Nodes = append(edge.Target.Nodes, k)
  866. }
  867. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  868. }
  869. _node = &Machine{config: muo.config}
  870. _spec.Assign = _node.assignValues
  871. _spec.ScanValues = _node.scanValues
  872. if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
  873. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  874. err = &NotFoundError{machine.Label}
  875. } else if sqlgraph.IsConstraintError(err) {
  876. err = &ConstraintError{err.Error(), err}
  877. }
  878. return nil, err
  879. }
  880. return _node, nil
  881. }