@@ -392,13 +392,6 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
392392
393393 isBootc := len (host .SystemProfile .BootcStatus .Booted .Image ) > 0
394394
395- templateID := hostTemplate (tx , accountID , host )
396- if templateID != nil {
397- colsToUpdate = append (colsToUpdate , "template_id" )
398- utils .LogDebug ("inventoryID" , host .ID , "candlepin_env" , host .SystemProfile .Rhsm .Environments ,
399- "template" , * templateID , "reporter" , host .Reporter )
400- }
401-
402395 updatesReqJSONString := string (updatesReqJSON )
403396 systemPlatform := models.SystemPlatform {
404397 InventoryID : inventoryID ,
@@ -417,7 +410,7 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
417410 BuiltPkgcache : yumUpdates .GetBuiltPkgcache (),
418411 Arch : host .SystemProfile .Arch ,
419412 Bootc : isBootc ,
420- TemplateID : templateID ,
413+ TemplateID : hostTemplate ( tx , accountID , host ) ,
421414 }
422415
423416 type OldChecksums struct {
@@ -476,22 +469,83 @@ func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsTo
476469 }
477470
478471 // return system_platform record after update
479- tx = tx .Clauses (clause.Returning {
472+ txi : = tx .Clauses (clause.Returning {
480473 Columns : []clause.Column {
481474 {Name : "id" }, {Name : "inventory_id" }, {Name : "rh_account_id" },
482- {Name : "unchanged_since" }, { Name : "last_evaluation" },
475+ {Name : "unchanged_since" },
483476 },
484477 })
485478
486- if system .ID != 0 {
487- // update system
488- err := tx .Select (colsToUpdate ).Updates (system ).Error
489- return base .WrapFatalDBError (err , "unable to update system_platform" )
479+ inventoryRecord := models.SystemInventory {
480+ ID : system .ID ,
481+ InventoryID : system .InventoryID ,
482+ RhAccountID : system .RhAccountID ,
483+ DisplayName : system .DisplayName ,
484+ LastUpload : system .LastUpload ,
485+ SatelliteManaged : system .SatelliteManaged ,
486+ BuiltPkgcache : system .BuiltPkgcache ,
487+ Arch : system .Arch ,
488+ Bootc : system .Bootc ,
489+ VmaasJSON : system .VmaasJSON ,
490+ JSONChecksum : system .JSONChecksum ,
491+ ReporterID : system .ReporterID ,
492+ YumUpdates : system .YumUpdates ,
493+ YumChecksum : system .YumChecksum ,
494+ StaleTimestamp : system .StaleTimestamp ,
495+ StaleWarningTimestamp : system .StaleWarningTimestamp ,
496+ CulledTimestamp : system .CulledTimestamp ,
497+ Tags : []byte ("[]" ),
498+ }
499+
500+ err := database .OnConflictUpdateMulti (txi , []string {"rh_account_id" , "inventory_id" }, colsToUpdate ... ).
501+ Create (& inventoryRecord ).Error
502+ if err != nil {
503+ return base .WrapFatalDBError (err , "unable to insert to system_inventory" )
504+ }
505+
506+ system .ID = inventoryRecord .ID
507+ system .InventoryID = inventoryRecord .InventoryID
508+ system .RhAccountID = inventoryRecord .RhAccountID
509+ system .UnchangedSince = inventoryRecord .UnchangedSince
510+
511+ return upsertSystemPatch (tx , system )
512+ }
513+
514+ func upsertSystemPatch (tx * gorm.DB , system * models.SystemPlatform ) error {
515+ tx = tx .Clauses (clause.Returning {Columns : []clause.Column {{Name : "last_evaluation" }}})
516+
517+ patchRecord := models.SystemPatch {
518+ SystemID : system .ID ,
519+ RhAccountID : system .RhAccountID ,
520+ TemplateID : system .TemplateID ,
490521 }
491- // insert system
492- err := database .OnConflictUpdateMulti (tx , []string {"rh_account_id" , "inventory_id" }, colsToUpdate ... ).
493- Save (system ).Error
494- return base .WrapFatalDBError (err , "unable to insert to system_platform" )
522+
523+ var patchColsToUpdate = []string {}
524+ if system .TemplateID != nil {
525+ patchColsToUpdate = append (patchColsToUpdate , "template_id" )
526+ }
527+
528+ // !existuje, * => create
529+ // existuje, templateID => update templateID
530+ // existuje, !templateID => nothing, we need to load last_evaluation
531+
532+ patchResult := database .OnConflictUpdateMulti (tx , []string {"rh_account_id" , "system_id" }, patchColsToUpdate ... ).
533+ Create (& patchRecord )
534+ if patchResult .Error != nil {
535+ return base .WrapFatalDBError (patchResult .Error , "unable to insert to system_patch" )
536+ }
537+ if patchResult .RowsAffected == 0 {
538+ err := tx .Model (& models.SystemPatch {}).
539+ Select ("last_evaluation" ).
540+ Where ("system_id = ? AND rh_account_id = ?" , patchRecord .SystemID , patchRecord .RhAccountID ).
541+ First (& patchRecord ).Error
542+ if err != nil {
543+ return base .WrapFatalDBError (err , "unable to load system_patch last_evaluation" )
544+ }
545+ }
546+
547+ system .LastEvaluation = patchRecord .LastEvaluation
548+ return nil
495549}
496550
497551func getReporterID (reporter string ) * int {
0 commit comments