Skip to content

RHINENG-29747: add workspace_id not null constraint - #2329

Open
Dugowitch wants to merge 2 commits into
RedHatInsights:masterfrom
Dugowitch:ws-migration
Open

RHINENG-29747: add workspace_id not null constraint#2329
Dugowitch wants to merge 2 commits into
RedHatInsights:masterfrom
Dugowitch:ws-migration

Conversation

@Dugowitch

@Dugowitch Dugowitch commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

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

Enforce workspace association for all system inventory records and propagate the required workspace identity consistently across the application.

Bug Fixes:

  • Reject system inventory updates that do not include a valid workspace UUID, matching the new database requirement.

Enhancements:

  • Make workspace identity mandatory throughout inventory models, API responses, advisory events, and cache refresh logic.
  • Add migration 167 to enforce the non-null workspace constraint and update the advisory cache function accordingly.

Tests:

  • Update affected tests and test fixtures for non-null workspace UUIDs.

Dugowitch and others added 2 commits September 11, 2026 14:48
WorkspaceID will not be nil after the migration in previous commit.

Co-Authored-By: Gemini <gemini@google.com>
@Dugowitch
Dugowitch requested a review from a team as a code owner September 11, 2026 12:49
@sourcery-ai

sourcery-ai Bot commented Sep 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR makes workspace_id mandatory end to end: migration 167 changes the database constraint and advisory-cache SQL, Go models and API representations use non-null UUIDs, and listener ingestion rejects missing or malformed workspace data while downstream events and responses assume a valid workspace.

File-Level Changes

Change Details Files
Enforce workspace presence at the database and model layers.
  • Add migration 167 to make system_inventory.workspace_id NOT NULL and update the schema version.
  • Change SystemInventory and API workspace fields from nullable pointers to non-null UUID values.
  • Provide a rollback that restores nullability and the prior advisory-cache filtering behavior.
database_admin/migrations/167_add_workspace_id_not_null.up.sql
database_admin/migrations/167_add_workspace_id_not_null.down.sql
database_admin/schema/create_schema.sql
base/models/models.go
Reject inventory updates that do not contain a valid workspace.
  • Require at least one host workspace group during upload.
  • Parse and store the first workspace as a value UUID, while retaining warnings for multiple groups.
  • Update listener assertions and upload fixtures for zero-value versus populated workspace IDs.
listener/upload.go
listener/common_test.go
listener/upload_test.go
Propagate non-null workspace IDs through advisory events and system responses.
  • Construct advisory update events directly with the required workspace UUID.
  • Always serialize system groups as a single workspace object and expose workspace IDs as non-pointer UUIDs.
  • Update affected tests and shared workspace test helpers.
evaluator/advisory_update.go
evaluator/advisory_update_test.go
manager/controllers/common_attributes.go
base/database/testing.go
manager/controllers/template_systems_update_test.go
tasks/system_culling/system_culling_test.go
turnpike/controllers/admin_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

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="database_admin/migrations/167_add_workspace_id_not_null.up.sql" line_range="1" />
<code_context>
+ALTER TABLE system_inventory ALTER COLUMN workspace_id SET NOT NULL;
+
+CREATE OR REPLACE FUNCTION refresh_account_advisory_caches_multi(advisory_ids_in INTEGER[] DEFAULT NULL,
</code_context>
<issue_to_address>
**issue (bug_risk):** The migration fails at `SET NOT NULL` when any existing `system_inventory.workspace_id` row is NULL. The migration does not backfill or delete those rows before applying the constraint, and the preceding schema explicitly allowed NULL values.

**Triggers:** When upgrading a database containing inventories created before this constraint.

**Suggested fix:** Backfill valid workspace IDs or otherwise handle NULL rows before setting the column to NOT NULL, ideally with an explicit validation step.
</issue_to_address>

### Comment 2
<location path="listener/upload.go" line_range="369-372" />
<code_context>
-			)
-		}
+	l := len(host.Groups)
+	if l == 0 {
+		utils.LogError("inventoryID", inventoryID, "workspace UUID missing for system")
+		return nil, errors.New("workspace UUID missing for system")
+	}
+	workspace := host.Groups[0]
+	workspaceID, err := uuid.Parse(workspace.ID)
</code_context>
<issue_to_address>
**issue (bug_risk):** Uploads for hosts with no workspace group now return an error instead of storing the inventory. The existing code accepted such hosts and persisted a NULL workspace ID, so any still-valid host event without `Groups[0]` is rejected and its upload transaction fails.

**Triggers:** When an inventory upload contains an empty `host.Groups` list.

**Suggested fix:** Ensure the event pipeline supplies a valid workspace before this function, or define and use a non-NULL fallback workspace value instead of rejecting the upload.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if existing rows have a null workspace_id, the migration can fail, and hosts without a workspace will stop being ingested or updated. A wrongly assigned workspace UUID would be persisted and could misgroup advisory caches and API results; reverting the code does not automatically repair those records, though they can be corrected by recomputation or update.

Blocking findings: database_admin/migrations/167_add_workspace_id_not_null.up.sql:1, listener/upload.go:372


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread database_admin/migrations/167_add_workspace_id_not_null.up.sql
Comment thread listener/upload.go
Comment on lines +369 to +372
if l == 0 {
utils.LogError("inventoryID", inventoryID, "workspace UUID missing for system")
return nil, errors.New("workspace UUID missing for system")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Uploads for hosts with no workspace group now return an error instead of storing the inventory. The existing code accepted such hosts and persisted a NULL workspace ID, so any still-valid host event without Groups[0] is rejected and its upload transaction fails.

Triggers: When an inventory upload contains an empty host.Groups list.

Suggested fix: Ensure the event pipeline supplies a valid workspace before this function, or define and use a non-NULL fallback workspace value instead of rejecting the upload.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 43.47826% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.98%. Comparing base (45d633a) to head (bb2e084).

Files with missing lines Patch % Lines
listener/upload.go 31.25% 8 Missing and 3 partials ⚠️
base/database/testing.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2329      +/-   ##
==========================================
- Coverage   59.06%   58.98%   -0.09%     
==========================================
  Files         150      150              
  Lines        9615     9608       -7     
==========================================
- Hits         5679     5667      -12     
- Misses       3342     3346       +4     
- Partials      594      595       +1     
Flag Coverage Δ
unittests 58.98% <43.47%> (-0.09%) ⬇️

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.

@Dugowitch

Copy link
Copy Markdown
Contributor Author

Is it better to error out, if there is no workspace id in the message, or to use the zero UUID?

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.

2 participants