RHINENG-24787: simplify system load and go directly into SystemPlatfo… - #2158
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors system data loading to query SystemPlatformV2 directly via a joined system_inventory/system_patch GORM query and adjusts the composite model mapping accordingly, removing the now-redundant loader implementation and its tests. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
SC Environment Impact AssessmentOverall Impact: ⚪ NONE No SC Environment-specific impacts detected in this PR. What was checkedThis PR was automatically scanned for:
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The new
loadSystemDataquery usesFindon a singleSystemPlatformV2instance, which silently returns a zero value when no row matches; if callers rely on a not-found error, consider switching toFirstor explicitly checkingRowsAffectedto preserve behavior. - By changing
SystemPlatformV2to use twoembeddedstructs and selectingsi.*, sp.*without aliases, overlapping column names fromsystem_inventoryandsystem_patch(e.g.,id, timestamps, account IDs) may map unpredictably; it would be safer to addembeddedPrefix/explicit column tags or adjust the SELECT to alias fields to avoid collisions. - Inlining the system load SQL into
loadSystemDataremoves the dedicated loader function and its tests; if this pattern will be reused, consider keeping a single helper for the join logic to avoid duplication and make future changes less error-prone.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `loadSystemData` query uses `Find` on a single `SystemPlatformV2` instance, which silently returns a zero value when no row matches; if callers rely on a not-found error, consider switching to `First` or explicitly checking `RowsAffected` to preserve behavior.
- By changing `SystemPlatformV2` to use two `embedded` structs and selecting `si.*, sp.*` without aliases, overlapping column names from `system_inventory` and `system_patch` (e.g., `id`, timestamps, account IDs) may map unpredictably; it would be safer to add `embeddedPrefix`/explicit column tags or adjust the SELECT to alias fields to avoid collisions.
- Inlining the system load SQL into `loadSystemData` removes the dedicated loader function and its tests; if this pattern will be reused, consider keeping a single helper for the join logic to avoid duplication and make future changes less error-prone.
## Individual Comments
### Comment 1
<location path="evaluator/evaluate.go" line_range="684-689" />
<code_context>
- }
- return v2, nil
+ var system models.SystemPlatformV2
+ err := database.DB.Table("system_inventory si").
+ Select("si.*, sp.*").
+ Joins("JOIN system_patch sp ON sp.system_id = si.id AND sp.rh_account_id = si.rh_account_id").
+ Where("si.rh_account_id = ?", accountID).
+ Where("si.inventory_id = ?::uuid", inventoryID).
+ Find(&system).Error
+ return &system, err
}
</code_context>
<issue_to_address>
**issue:** Consider how multiple system_patch rows for a single inventory/system are handled in this join.
Because the join doesn’t disambiguate among multiple `system_patch` rows for the same system, `Find(&system)` will iterate over all matches and the final struct will be populated by whichever row is returned last. If you need a specific patch (e.g., the latest), add an appropriate `ORDER BY` + `LIMIT 1` or additional predicates on fields like `sp.id`/`sp.created_at`/`sp.reported_at` to make the selection deterministic.
</issue_to_address>
### Comment 2
<location path="base/models/models.go" line_range="132-133" />
<code_context>
type SystemPlatformV2 struct {
- Inventory SystemInventory
- Patch SystemPatch
+ Inventory SystemInventory `gorm:"embedded"`
+ Patch SystemPatch `gorm:"embedded"`
}
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Embedded structs without prefixes may lead to column name collisions in the join-based load.
Because both `SystemInventory` and `SystemPatch` are embedded without `embeddedPrefix`, the `Select("si.*, sp.*")` will cause any shared column names (e.g., `id`, `rh_account_id`, timestamps) to map to a single field, with the last-scanned table winning. If you need both values, add distinct `embeddedPrefix` values and update the `Select` to use matching aliases (e.g., `si.id AS inventory_id`).
Suggested implementation:
```golang
type SystemPlatformV2 struct {
Inventory SystemInventory `gorm:"embedded;embeddedPrefix:inventory_"`
Patch SystemPatch `gorm:"embedded;embeddedPrefix:patch_"`
}
```
` since we don't see that code here.
Here are the changes:
<file_operations>
<file_operation operation="edit" file_path="base/models/models.go">
<<<<<<< SEARCH
type SystemPlatformV2 struct {
Inventory SystemInventory `gorm:"embedded"`
Patch SystemPatch `gorm:"embedded"`
}
=======
type SystemPlatformV2 struct {
Inventory SystemInventory `gorm:"embedded;embeddedPrefix:inventory_"`
Patch SystemPatch `gorm:"embedded;embeddedPrefix:patch_"`
}
>>>>>>> REPLACE
</file_operation>
</file_operations>
<additional_changes>
Any code that loads `SystemPlatformV2` via joins and `Select("si.*, sp.*")` must be updated to:
1. Explicitly alias columns to match the new prefixes. For example:
- `si.id AS inventory_id`, `si.rh_account_id AS inventory_rh_account_id`, etc.
- `sp.id AS patch_id`, `sp.rh_account_id AS patch_rh_account_id`, etc.
2. Ensure the table aliases (`si`, `sp`) and column lists cover all fields that GORM will map into the embedded structs, or use `SELECT si.*` into a separate model and then map manually if needed.
Look for any queries that:
- Use `SystemPlatformV2` as the destination model, and
- Use `Select("si.*, sp.*")` or similar unaliased selects with joins on `system_inventory` and `system_patch`,
and update those `Select` clauses accordingly.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2158 +/- ##
==========================================
- Coverage 59.29% 59.03% -0.26%
==========================================
Files 135 134 -1
Lines 8748 8688 -60
==========================================
- Hits 5187 5129 -58
Misses 3022 3022
+ Partials 539 537 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…rmV2
Secure Coding Practices Checklist GitHub Link
Secure Coding Checklist
Summary by Sourcery
Simplify system data loading by querying system and patch data directly into the SystemPlatformV2 model and aligning the model structure with the underlying tables.
Enhancements: