Skip to content

HMS-10821: create template advisories table#2234

Merged
TenSt merged 1 commit into
RedHatInsights:masterfrom
TenSt:stepan/HMS-10821-create-template-advisory-table
Jun 17, 2026
Merged

HMS-10821: create template advisories table#2234
TenSt merged 1 commit into
RedHatInsights:masterfrom
TenSt:stepan/HMS-10821-create-template-advisory-table

Conversation

@TenSt

@TenSt TenSt commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

This PR:

  • creates new template_advisory table to store template↔advisory associations (one row per pair, keyed by rh_account_id, template_id, advisory_id)
  • adds index on (rh_account_id, advisory_id) for reverse lookups by advisory
  • partitions the table by rh_account_id (16 partitions, matching template) with FKs to template (CASCADE on delete) and advisory_metadata
  • updates schema
  • introduces the TemplateAdvisory model
  • updates TestTableSizes expected table count for the new partitioned table

Summary by Sourcery

Introduce a partitioned template_advisory table to store associations between templates and advisories and expose it via a new GORM model.

New Features:

  • Add template_advisory table partitioned by rh_account_id to model template-to-advisory relationships with appropriate foreign keys and indexing.
  • Introduce a TemplateAdvisory GORM model and slice type for application-level access to template_advisory records.

Build:

  • Add schema migration 161 to create and grant access to the template_advisory table and update the base schema version.

Tests:

  • Update TestTableSizes expectations and assertions to account for the new partitioned template_advisory table.

@TenSt TenSt requested a review from a team as a code owner June 17, 2026 13:11
@sourcery-ai

sourcery-ai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces a new partitioned template_advisory join table between templates and advisory_metadata, wires it into the GORM data model, and updates schema/migrations and table-size tests to account for the new table and its partitions.

File-Level Changes

Change Details Files
Add partitioned template_advisory join table with constraints, index, and grants, wired into schema migrations.
  • Bump schema_migrations version from 160 to 161.
  • Define template_advisory table keyed by (rh_account_id, template_id, advisory_id) with hash partitioning on rh_account_id.
  • Add foreign key from (rh_account_id, template_id) to template with ON DELETE CASCADE and from advisory_id to advisory_metadata.
  • Create 16 partitions for template_advisory with specific fillfactor and autovacuum settings.
  • Add composite index on (rh_account_id, advisory_id) for reverse lookup by advisory.
  • Grant SELECT/CRUD privileges on template_advisory partitions to manager, evaluator, listener, and vmaas_sync roles.
  • Introduce up/down migration pair for creating and dropping template_advisory with the same definition as in the base schema.
database_admin/schema/create_schema.sql
database_admin/migrations/161_template_advisory.up.sql
database_admin/migrations/161_template_advisory.down.sql
Expose template_advisory in the Go data model and adjust tests for the increased table count.
  • Add TemplateAdvisory GORM model with primary key fields rh_account_id, template_id, advisory_id and associated AdvisoryMetadata relation.
  • Define TableName() for TemplateAdvisory to map to template_advisory and introduce a TemplateAdvisorySlice alias.
  • Update TestTableSizes expectations for total table count and uniqueness to reflect new partitions.
  • Add assertion that public.template_advisory exists in the loaded table names.
base/models/models.go
tasks/vmaas_sync/metrics_db_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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • In the TemplateAdvisory model, consider adding explicit GORM tags for the Advisory relationship (e.g., foreignKey:AdvisoryID;references:ID) so that the ORM correctly wires the association to advisory_metadata rather than relying on default naming.
  • The down migration simply DROPs template_advisory; if your partition helper creates child tables explicitly, confirm that dropping the parent will clean up all partitions as expected or call the corresponding drop-partitions helper to avoid leaving orphaned partition tables.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the `TemplateAdvisory` model, consider adding explicit GORM tags for the `Advisory` relationship (e.g., `foreignKey:AdvisoryID;references:ID`) so that the ORM correctly wires the association to `advisory_metadata` rather than relying on default naming.
- The down migration simply `DROP`s `template_advisory`; if your partition helper creates child tables explicitly, confirm that dropping the parent will clean up all partitions as expected or call the corresponding drop-partitions helper to avoid leaving orphaned partition tables.

## Individual Comments

### Comment 1
<location path="database_admin/schema/create_schema.sql" line_range="800-801" />
<code_context>
+    CONSTRAINT template_advisory_template_id
+        FOREIGN KEY (rh_account_id, template_id)
+            REFERENCES template (rh_account_id, id) ON DELETE CASCADE,
+    CONSTRAINT template_advisory_advisory_id
+        FOREIGN KEY (advisory_id)
+            REFERENCES advisory_metadata (id)
+) PARTITION BY HASH (rh_account_id);
</code_context>
<issue_to_address>
**question (bug_risk):** Consider whether `advisory_id` should cascade on delete to avoid blocking advisory deletions.

`template_advisory_template_id` uses `ON DELETE CASCADE`, but `template_advisory_advisory_id` does not. As a result, deleting an `advisory_metadata` row will fail if related `template_advisory` rows exist. If you want those rows cleaned up automatically, add `ON DELETE CASCADE` here as well; otherwise, confirm callers correctly handle constraint violations when deletes are blocked.
</issue_to_address>

### Comment 2
<location path="base/models/models.go" line_range="55-60" />
<code_context>
 	return "template"
 }

+type TemplateAdvisory struct {
+	RhAccountID int   `gorm:"primaryKey"`
+	TemplateID  int64 `gorm:"primaryKey"`
+	AdvisoryID  int64 `gorm:"primaryKey"`
+	Advisory    AdvisoryMetadata
+}
+
</code_context>
<issue_to_address>
**suggestion:** Clarify the GORM relationship configuration for the `Advisory` field.

GORM will infer the `Advisory``AdvisoryID` relationship by name, but there are no explicit `gorm` tags for this association. For predictable `Preload("Advisory")`/joins and to future-proof against naming changes, consider making it explicit, e.g.:

```go
Advisory AdvisoryMetadata `gorm:"foreignKey:AdvisoryID;references:ID"`
```

```suggestion
type TemplateAdvisory struct {
	RhAccountID int   `gorm:"primaryKey"`
	TemplateID  int64 `gorm:"primaryKey"`
	AdvisoryID  int64 `gorm:"primaryKey"`
	Advisory    AdvisoryMetadata `gorm:"foreignKey:AdvisoryID;references:ID"`
}
```
</issue_to_address>

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.

Comment thread database_admin/schema/create_schema.sql
Comment thread base/models/models.go
@codecov-commenter

codecov-commenter commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.03%. Comparing base (d0e3296) to head (c2c1e63).

Files with missing lines Patch % Lines
base/models/models.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2234      +/-   ##
==========================================
- Coverage   59.07%   59.03%   -0.05%     
==========================================
  Files         138      138              
  Lines        8845     8848       +3     
==========================================
- Hits         5225     5223       -2     
- Misses       3074     3079       +5     
  Partials      546      546              
Flag Coverage Δ
unittests 59.03% <33.33%> (-0.05%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@TenSt TenSt force-pushed the stepan/HMS-10821-create-template-advisory-table branch from e4eefb6 to c2c1e63 Compare June 17, 2026 13:25
@xbhouse xbhouse self-assigned this Jun 17, 2026

@xbhouse xbhouse left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

@TenSt TenSt merged commit 704b877 into RedHatInsights:master Jun 17, 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