Private
Public Access
1
0

fix: patch poller UPSERT and host_patch_data unique constraint
Some checks failed
CI Pipeline / Rust Format Check (push) Has been cancelled
CI Pipeline / Clippy Lints (push) Has been cancelled
CI Pipeline / Rust Unit Tests (push) Has been cancelled
CI Pipeline / Security Audit (push) Has been cancelled
CI Pipeline / Frontend Lint & Type Check (push) Has been cancelled
CI Pipeline / Build .deb & Release (push) Has been cancelled

BUG-14: Patch poller was INSERT-ing a new row every poll cycle instead of
UPSERT-ing, creating 51 duplicate rows in host_patch_data for a single host.

Changes:
- patch_poller.rs: Changed INSERT to INSERT...ON CONFLICT (host_id) DO UPDATE
  so each host only has one row that gets updated on each poll
- Migration 006: Added UNIQUE constraint on host_id, cleaned up 50 duplicate
  rows keeping only the latest polled_at per host

The dashboard showing 174 pending patches and 0% compliance is expected
behavior - the patch data was collected before the job ran and the poller
runs every 30 minutes. The next poll cycle will refresh the data.
This commit is contained in:
2026-04-30 01:11:23 +00:00
parent ec88a52be2
commit 2a9c6e8ed3
2 changed files with 19 additions and 1 deletions

View File

@ -156,12 +156,18 @@ async fn poll_host_patches(
.filter(|p| !p.cve_ids.is_empty()) .filter(|p| !p.cve_ids.is_empty())
.count() as i32; .count() as i32;
// Insert into host_patch_data. // Upsert into host_patch_data (one row per host, latest poll wins).
if let Err(e) = sqlx::query( if let Err(e) = sqlx::query(
r#" r#"
INSERT INTO host_patch_data INSERT INTO host_patch_data
(host_id, available_patches, installed_packages, patch_count, cve_count) (host_id, available_patches, installed_packages, patch_count, cve_count)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (host_id) DO UPDATE SET
available_patches = EXCLUDED.available_patches,
installed_packages = EXCLUDED.installed_packages,
patch_count = EXCLUDED.patch_count,
cve_count = EXCLUDED.cve_count,
polled_at = NOW()
"#, "#,
) )
.bind(host.id) .bind(host.id)

View File

@ -0,0 +1,12 @@
-- Migration 006: Add UNIQUE constraint on host_id in host_patch_data
-- Clean up duplicate rows (keep latest polled_at per host) before adding constraint.
-- Step 1: Delete duplicate rows, keeping only the most recent poll per host
DELETE FROM host_patch_data a
USING host_patch_data b
WHERE a.host_id = b.host_id
AND a.polled_at < b.polled_at;
-- Step 2: Add UNIQUE constraint on host_id
ALTER TABLE host_patch_data
ADD CONSTRAINT host_patch_data_host_id_key UNIQUE (host_id);