Skip to content

Commit d835f2b

Browse files
MichaelMrakaDugowitch
authored andcommitted
RHINENG-21214: update the new tables in storeOrUpdateSysPlatform
1 parent 11c5b6a commit d835f2b

1 file changed

Lines changed: 72 additions & 18 deletions

File tree

listener/upload.go

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,6 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
335335

336336
isBootc := len(host.SystemProfile.BootcStatus.Booted.Image) > 0
337337

338-
templateID := hostTemplate(tx, accountID, host)
339-
if templateID != nil {
340-
colsToUpdate = append(colsToUpdate, "template_id")
341-
utils.LogDebug("inventoryID", host.ID, "candlepin_env", host.SystemProfile.Rhsm.Environments,
342-
"template", *templateID, "reporter", host.Reporter)
343-
}
344-
345338
updatesReqJSONString := string(updatesReqJSON)
346339
systemPlatform := models.SystemPlatform{
347340
InventoryID: inventoryID,
@@ -360,7 +353,7 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
360353
BuiltPkgcache: yumUpdates.GetBuiltPkgcache(),
361354
Arch: host.SystemProfile.Arch,
362355
Bootc: isBootc,
363-
TemplateID: templateID,
356+
TemplateID: hostTemplate(tx, accountID, host),
364357
}
365358

366359
type OldChecksums struct {
@@ -419,22 +412,83 @@ func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsTo
419412
}
420413

421414
// return system_platform record after update
422-
tx = tx.Clauses(clause.Returning{
415+
txi := tx.Clauses(clause.Returning{
423416
Columns: []clause.Column{
424417
{Name: "id"}, {Name: "inventory_id"}, {Name: "rh_account_id"},
425-
{Name: "unchanged_since"}, {Name: "last_evaluation"},
418+
{Name: "unchanged_since"},
426419
},
427420
})
428421

429-
if system.ID != 0 {
430-
// update system
431-
err := tx.Select(colsToUpdate).Updates(system).Error
432-
return base.WrapFatalDBError(err, "unable to update system_platform")
422+
inventoryRecord := models.SystemInventory{
423+
ID: system.ID,
424+
InventoryID: system.InventoryID,
425+
RhAccountID: system.RhAccountID,
426+
DisplayName: system.DisplayName,
427+
LastUpload: system.LastUpload,
428+
SatelliteManaged: system.SatelliteManaged,
429+
BuiltPkgcache: system.BuiltPkgcache,
430+
Arch: system.Arch,
431+
Bootc: system.Bootc,
432+
VmaasJSON: system.VmaasJSON,
433+
JSONChecksum: system.JSONChecksum,
434+
ReporterID: system.ReporterID,
435+
YumUpdates: system.YumUpdates,
436+
YumChecksum: system.YumChecksum,
437+
StaleTimestamp: system.StaleTimestamp,
438+
StaleWarningTimestamp: system.StaleWarningTimestamp,
439+
CulledTimestamp: system.CulledTimestamp,
440+
Tags: []byte("[]"),
441+
}
442+
443+
err := database.OnConflictUpdateMulti(txi, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).
444+
Create(&inventoryRecord).Error
445+
if err != nil {
446+
return base.WrapFatalDBError(err, "unable to insert to system_inventory")
447+
}
448+
449+
system.ID = inventoryRecord.ID
450+
system.InventoryID = inventoryRecord.InventoryID
451+
system.RhAccountID = inventoryRecord.RhAccountID
452+
system.UnchangedSince = inventoryRecord.UnchangedSince
453+
454+
return upsertSystemPatch(tx, system)
455+
}
456+
457+
func upsertSystemPatch(tx *gorm.DB, system *models.SystemPlatform) error {
458+
tx = tx.Clauses(clause.Returning{Columns: []clause.Column{{Name: "last_evaluation"}}})
459+
460+
patchRecord := models.SystemPatch{
461+
SystemID: system.ID,
462+
RhAccountID: system.RhAccountID,
463+
TemplateID: system.TemplateID,
433464
}
434-
// insert system
435-
err := database.OnConflictUpdateMulti(tx, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).
436-
Save(system).Error
437-
return base.WrapFatalDBError(err, "unable to insert to system_platform")
465+
466+
var patchColsToUpdate = []string{}
467+
if system.TemplateID != nil {
468+
patchColsToUpdate = append(patchColsToUpdate, "template_id")
469+
}
470+
471+
// !existuje, * => create
472+
// existuje, templateID => update templateID
473+
// existuje, !templateID => nothing, we need to load last_evaluation
474+
475+
patchResult := database.OnConflictUpdateMulti(tx, []string{"rh_account_id", "system_id"}, patchColsToUpdate...).
476+
Create(&patchRecord)
477+
if patchResult.Error != nil {
478+
return base.WrapFatalDBError(patchResult.Error, "unable to insert to system_patch")
479+
}
480+
if patchResult.RowsAffected == 0 {
481+
err := tx.Model(&models.SystemPatch{}).
482+
Select("last_evaluation").
483+
Where("system_id = ? AND rh_account_id = ?", patchRecord.SystemID, patchRecord.RhAccountID).
484+
First(&patchRecord).Error
485+
if err != nil {
486+
return base.WrapFatalDBError(err, "unable to load system_patch last_evaluation")
487+
}
488+
}
489+
490+
system.LastEvaluation = patchRecord.LastEvaluation
491+
return nil
438492
}
439493

440494
func getReporterID(reporter string) *int {

0 commit comments

Comments
 (0)