Skip to content

Commit

Permalink
RHINENG-9028: fix comparison of yum_updates
Browse files Browse the repository at this point in the history
digest can't be used because the json contains map which doesn't remain sorted in DB
add yum_checksum which is calculated from marshaled json (marshaling maintains the same key order for structs and maps)
  • Loading branch information
psegedy committed Mar 26, 2024
1 parent eae9c89 commit 08cc83b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
11 changes: 6 additions & 5 deletions base/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ type SystemPlatform struct {
ThirdParty bool
ReporterID *int
BaselineID *int64
BaselineUpToDate *bool `gorm:"column:baseline_uptodate"`
TemplateID *int64 `gorm:"column:template_id"`
YumUpdates []byte `gorm:"column:yum_updates"`
SatelliteManaged bool `gorm:"column:satellite_managed"`
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
BaselineUpToDate *bool `gorm:"column:baseline_uptodate"`
TemplateID *int64 `gorm:"column:template_id"`
YumUpdates []byte `gorm:"column:yum_updates"`
YumChecksum *string `gorm:"column:yum_checksum"`
SatelliteManaged bool `gorm:"column:satellite_managed"`
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
}

func (SystemPlatform) TableName() string {
Expand Down
19 changes: 19 additions & 0 deletions database_admin/migrations/125_yum_checksum.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE OR REPLACE FUNCTION check_unchanged()
RETURNS TRIGGER AS
$check_unchanged$
BEGIN
IF (TG_OP = 'INSERT') AND
NEW.unchanged_since IS NULL THEN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
IF (TG_OP = 'UPDATE') AND
(NEW.json_checksum <> OLD.json_checksum OR NEW.yum_updates <> OLD.yum_updates) THEN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
RETURN NEW;
END;
$check_unchanged$
LANGUAGE 'plpgsql';


ALTER TABLE system_platform DROP COLUMN IF EXISTS yum_checksum;
18 changes: 18 additions & 0 deletions database_admin/migrations/125_yum_checksum.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ALTER TABLE system_platform ADD COLUMN IF NOT EXISTS yum_checksum TEXT CHECK (NOT empty(yum_checksum));

CREATE OR REPLACE FUNCTION check_unchanged()
RETURNS TRIGGER AS
$check_unchanged$
BEGIN
IF (TG_OP = 'INSERT') AND
NEW.unchanged_since IS NULL THEN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
IF (TG_OP = 'UPDATE') AND
(NEW.json_checksum <> OLD.json_checksum OR NEW.yum_checksum <> OLD.yum_checksum) THEN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
RETURN NEW;
END;
$check_unchanged$
LANGUAGE 'plpgsql';
5 changes: 3 additions & 2 deletions database_admin/schema/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations


INSERT INTO schema_migrations
VALUES (124, false);
VALUES (125, false);

-- ---------------------------------------------------------------------------
-- Functions
Expand Down Expand Up @@ -70,7 +70,7 @@ BEGIN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
IF (TG_OP = 'UPDATE') AND
(NEW.json_checksum <> OLD.json_checksum OR NEW.yum_updates <> OLD.yum_updates) THEN
(NEW.json_checksum <> OLD.json_checksum OR NEW.yum_checksum <> OLD.yum_checksum) THEN
NEW.unchanged_since := CURRENT_TIMESTAMP;
END IF;
RETURN NEW;
Expand Down Expand Up @@ -740,6 +740,7 @@ CREATE TABLE IF NOT EXISTS system_platform
built_pkgcache BOOLEAN NOT NULL DEFAULT FALSE,
packages_applicable INT NOT NULL DEFAULT 0,
template_id BIGINT,
yum_checksum TEXT CHECK (NOT empty(yum_checksum)),
PRIMARY KEY (rh_account_id, id),
UNIQUE (rh_account_id, inventory_id),
CONSTRAINT reporter_id FOREIGN KEY (reporter_id) REFERENCES reporter (id),
Expand Down
5 changes: 3 additions & 2 deletions listener/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func updateSystemPlatform(tx *gorm.DB, inventoryID string, accountID int, host *
Stale: staleWarning != nil && staleWarning.Before(time.Now()),
ReporterID: getReporterID(host.Reporter),
YumUpdates: yumUpdates.GetRawParsed(),
YumChecksum: utils.EmptyToNil(&yumChecksum),
SatelliteManaged: host.SystemProfile.SatelliteManaged,
BuiltPkgcache: yumUpdates.GetBuiltPkgcache(),
}
Expand All @@ -344,7 +345,7 @@ func updateSystemPlatform(tx *gorm.DB, inventoryID string, accountID int, host *
Model(models.SystemPlatform{}).
Where("inventory_id = ?::uuid", inventoryID).
Where("rh_account_id = ?", accountID).
Select("json_checksum, encode(digest(yum_updates::text,'sha256'), 'hex') as yum_checksum").
Select("json_checksum, yum_checksum").
First(&oldChecksums)

shouldUpdateRepos := false
Expand All @@ -358,7 +359,7 @@ func updateSystemPlatform(tx *gorm.DB, inventoryID string, accountID int, host *

// Skip updating yum_updates if the checksum haven't changed.
if oldChecksums.YumChecksum != yumChecksum {
colsToUpdate = append(colsToUpdate, "yum_updates")
colsToUpdate = append(colsToUpdate, "yum_updates", "yum_checksum")
}

if err := storeOrUpdateSysPlatform(tx, &systemPlatform, colsToUpdate); err != nil {
Expand Down

0 comments on commit 08cc83b

Please sign in to comment.