Skip to content

Commit eb0fe48

Browse files
committed
wip: replace save into system_platform with system_{inventory,patch}
1 parent d231db0 commit eb0fe48

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

listener/upload.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020
"net/url"
2121
"regexp"
22+
"slices"
2223
"strings"
2324
"sync"
2425
"time"
@@ -469,6 +470,7 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
469470
return &systemPlatform, nil
470471
}
471472

473+
// nolint: funlen
472474
func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsToUpdate []string) error {
473475
if err := tx.Where("rh_account_id = ? AND inventory_id = ?", system.RhAccountID, system.InventoryID).
474476
Select("id").Find(system).Error; err != nil {
@@ -479,7 +481,7 @@ func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsTo
479481
tx = tx.Clauses(clause.Returning{
480482
Columns: []clause.Column{
481483
{Name: "id"}, {Name: "inventory_id"}, {Name: "rh_account_id"},
482-
{Name: "unchanged_since"}, {Name: "last_evaluation"},
484+
{Name: "unchanged_since"},
483485
},
484486
})
485487

@@ -488,9 +490,54 @@ func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsTo
488490
err := tx.Select(colsToUpdate).Updates(system).Error
489491
return base.WrapFatalDBError(err, "unable to update system_platform")
490492
}
491-
// insert system
492-
err := database.OnConflictUpdateMulti(tx, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).
493-
Save(system).Error
493+
inventoryRecord := models.SystemInventory{
494+
ID: system.ID,
495+
InventoryID: system.InventoryID,
496+
RhAccountID: system.RhAccountID,
497+
DisplayName: system.DisplayName,
498+
LastUpload: system.LastUpload,
499+
SatelliteManaged: system.SatelliteManaged,
500+
BuiltPkgcache: system.BuiltPkgcache,
501+
Arch: system.Arch,
502+
Bootc: system.Bootc,
503+
VmaasJSON: system.VmaasJSON,
504+
JSONChecksum: system.JSONChecksum,
505+
ReporterID: system.ReporterID,
506+
YumUpdates: system.YumUpdates,
507+
YumChecksum: system.YumChecksum,
508+
StaleTimestamp: system.StaleTimestamp,
509+
StaleWarningTimestamp: system.StaleWarningTimestamp,
510+
CulledTimestamp: system.CulledTimestamp,
511+
Tags: []byte("[]"),
512+
}
513+
inventoryColsToUpdate := slices.DeleteFunc(colsToUpdate, func(col string) bool {
514+
return col == "template_id"
515+
})
516+
err := database.OnConflictUpdateMulti(tx, []string{"rh_account_id", "inventory_id"}, inventoryColsToUpdate...).
517+
Save(&inventoryRecord).Error
518+
if err != nil {
519+
return base.WrapFatalDBError(err, "unable to insert to system_inventory")
520+
}
521+
522+
system.ID = inventoryRecord.ID
523+
system.InventoryID = inventoryRecord.InventoryID
524+
system.RhAccountID = inventoryRecord.RhAccountID
525+
system.UnchangedSince = inventoryRecord.UnchangedSince
526+
527+
var patchColsToUpdate []string
528+
if slices.Contains(colsToUpdate, "template_id") {
529+
patchColsToUpdate = []string{"template_id"}
530+
}
531+
patchRecord := models.SystemPatch{
532+
SystemID: inventoryRecord.ID,
533+
RhAccountID: system.RhAccountID,
534+
LastEvaluation: system.LastEvaluation,
535+
TemplateID: system.TemplateID,
536+
}
537+
tx = tx.Clauses(clause.Returning{Columns: []clause.Column{{Name: "last_evaluation"}}})
538+
err = database.OnConflictUpdateMulti(tx, []string{"rh_account_id", "system_id"}, patchColsToUpdate...).
539+
Save(patchRecord).Error
540+
system.LastEvaluation = patchRecord.LastEvaluation
494541
return base.WrapFatalDBError(err, "unable to insert to system_platform")
495542
}
496543

0 commit comments

Comments
 (0)