Skip to content

RHINENG-26117: Add recompute infrastructure for account_advisory#2195

Merged
rverdile merged 1 commit into
RedHatInsights:masterfrom
rverdile:recompute-query
May 28, 2026
Merged

RHINENG-26117: Add recompute infrastructure for account_advisory#2195
rverdile merged 1 commit into
RedHatInsights:masterfrom
rverdile:recompute-query

Conversation

@rverdile
Copy link
Copy Markdown
Contributor

@rverdile rverdile commented May 14, 2026

Summary

This adds recompute and trigger infrastructure for account_advisory. It's mostly modeled after the existing recompute and trigger for account_advisory_data. I also added a little bit of Go infrastructure so that I could add unit tests.

This is dependent on this PR: #2184

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Add database functions, indexes, models, and tests to support recomputing and backfilling account-level advisory aggregates.

New Features:

  • Introduce PL/pgSQL functions to refresh and backfill account_advisory caches by advisory and account.
  • Add indexes on account_advisory aggregate columns to support cache queries.
  • Expose an AccountAdvisory GORM model and test helper functions for manipulating account_advisory data in tests.

Enhancements:

  • Version the schema with a new migration adding account_advisory cache functions and indexes, with a corresponding down migration.

Tests:

  • Add integration tests verifying that account_advisory cache refresh correctly recalculates counts and removes orphaned rows.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 14, 2026

Reviewer's Guide

Implements recompute/backfill functions and indices for the workspace-scoped account_advisory aggregation, exposes a Go model plus DB test helpers, and adds integration tests to validate refresh and cleanup behavior.

File-Level Changes

Change Details Files
Add refresh/backfill infrastructure and indexes for account_advisory aggregates in the database schema and migrations.
  • Bump schema_migrations version to 157 to introduce new migration
  • Define refresh_account_advisory_caches_multi to recompute per-account, per-workspace advisory counts from system_advisories/system_inventory/system_patch, with upsert and orphan cleanup semantics
  • Add wrapper functions refresh_account_advisory_caches and backfill_account_advisory to drive recomputation for a single advisory or whole account
  • Create indices on account_advisory for systems_applicable and systems_installable to optimize queries and updates
  • Mirror the new PL/pgSQL functions and indexes into dedicated 157 up/down migration files, including cleanup and truncation in the down migration
database_admin/schema/create_schema.sql
database_admin/migrations/157_account_advisory_queries.up.sql
database_admin/migrations/157_account_advisory_queries.down.sql
Introduce a Go model for account_advisory and helper functions for manipulating it in tests.
  • Define AccountAdvisory struct with composite primary key (AdvisoryID, RhAccountID, WorkspaceID) and the systems/notified fields, plus TableName() binding to account_advisory
  • Add AccountAdvisorySlice type alias for collections of account advisories
  • Extend testing DB helpers to create, verify, and delete account_advisory rows, including account-scoped cleanup utility
base/models/models.go
base/database/testing.go
Add integration tests to validate backfill and recompute behavior for account_advisory caches.
  • Create refresh_account_advisory_caches_test.go with DB-backed tests that backfill account_advisory for rh_account_id=1 and then verify refresh_account_advisory_caches restores correct counts after corruption
  • Add test ensuring refresh_account_advisory_caches removes rows for workspaces whose systems have all become stale and validates cleanup after resetting staleness
  • Use a fixed test workspace UUID and existing test setup utilities (SetupTestEnvironment, configure, SkipWithoutDB) to integrate with the current test harness
tasks/caches/refresh_account_advisory_caches_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 15, 2026

Codecov Report

❌ Patch coverage is 0% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.01%. Comparing base (8f1f21e) to head (f892d92).

Files with missing lines Patch % Lines
base/database/testing.go 0.00% 28 Missing ⚠️
base/models/models.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2195      +/-   ##
==========================================
- Coverage   59.21%   59.01%   -0.21%     
==========================================
  Files         137      137              
  Lines        8798     8828      +30     
==========================================
  Hits         5210     5210              
- Misses       3040     3070      +30     
  Partials      548      548              
Flag Coverage Δ
unittests 59.01% <0.00%> (-0.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rverdile rverdile changed the title RHINENG-26116: Add recompute and trigger infrastructure for account_advisory RHINENG-26116: Add recompute infrastructure for account_advisory May 15, 2026
@rverdile rverdile changed the title RHINENG-26116: Add recompute infrastructure for account_advisory RHINENG-26117: Add recompute infrastructure for account_advisory May 19, 2026
@rverdile rverdile force-pushed the recompute-query branch 8 times, most recently from 4d04534 to efc5b2b Compare May 27, 2026 16:19
Add SQL functions that mirror advisory_account_data functions to
populate account_advisory table
@rverdile rverdile marked this pull request as ready for review May 27, 2026 17:50
@rverdile rverdile requested a review from a team as a code owner May 27, 2026 17:50
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The DDL for account_advisory indexes and the refresh/backfill functions is duplicated in both create_schema.sql and 157_account_advisory_queries.up.sql; consider centralizing this in migrations (and having create_schema generated from them) to avoid future drift between fresh installs and upgraded environments.
  • The down migration 157_account_advisory_queries.down.sql truncates account_advisory, which is more destructive than a typical rollback; if this table contains any non-derivable or user-relevant data, consider dropping only the added indexes/functions and leaving the data intact.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The DDL for `account_advisory` indexes and the refresh/backfill functions is duplicated in both `create_schema.sql` and `157_account_advisory_queries.up.sql`; consider centralizing this in migrations (and having `create_schema` generated from them) to avoid future drift between fresh installs and upgraded environments.
- The down migration `157_account_advisory_queries.down.sql` truncates `account_advisory`, which is more destructive than a typical rollback; if this table contains any non-derivable or user-relevant data, consider dropping only the added indexes/functions and leaving the data intact.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@MichaelMraka MichaelMraka self-assigned this May 27, 2026
@rverdile rverdile merged commit 748202a into RedHatInsights:master May 28, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants