Skip to content

chore: migration script - owner_id add#2321

Open
RostyslavKachan wants to merge 1 commit into
RedHatInsights:masterfrom
RostyslavKachan:owner_id_migration
Open

chore: migration script - owner_id add#2321
RostyslavKachan wants to merge 1 commit into
RedHatInsights:masterfrom
RostyslavKachan:owner_id_migration

Conversation

@RostyslavKachan
Copy link
Copy Markdown
Collaborator

@RostyslavKachan RostyslavKachan commented May 7, 2026

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

Enhancements:

  • Introduce a migration script that copies owner_id values from inventory.hosts system_profile into system_platform.owner_id when missing, with safety checks for view existence and error logging.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

Adds a new PostgreSQL migration script to backfill system_platform.owner_id from inventory.hosts.system_profile and bumps the schema version to 164 to register the migration.

Entity relationship diagram for owner_id migration between system_platform and inventory_hosts

erDiagram
  system_platform {
    uuid id PK
    uuid inventory_id FK
    uuid owner_id
  }

  inventory_hosts {
    uuid id PK
    jsonb system_profile
  }

  inventory_hosts ||--o| system_platform : inventory_id
Loading

Flow diagram for PostgreSQL owner_id backfill migration script

flowchart TD
  A[start DO block] --> B[Check if inventory.hosts view exists]
  B -->|does not exist| C[Raise NOTICE inventory.hosts view does not exist, skipping migration]
  C --> D[end DO block]
  B -->|exists| E[Select rows where system_platform.owner_id is NULL and inventory.hosts.system_profile owner_id is not NULL]
  E --> F{For each selected record}
  F -->|record| G[Begin record update]
  G --> H[Update system_platform set owner_id from ih_owner_id cast to UUID]
  H --> I{Update succeeded}
  I --> J[next record]
  F -->|no more records| D
  G --> K[On exception: raise WARNING with inventory_id, ih_owner_id, SQLERRM]
  K --> J
  J --> F
Loading

File-Level Changes

Change Details Files
Register new schema version for the database to include the owner_id backfill migration.
  • Incremented db_version schema_version value from 163 to 164.
  • Left commented placeholder insert for dynamic schema versioning unchanged.
database/schema/ve_db_postgresql.sql
Introduce PL/pgSQL migration to populate system_platform.owner_id from the inventory.hosts view when possible, with safety checks and error logging.
  • Wrapped migration in an anonymous DO block that first checks for existence of the inventory.hosts view and exits early with a notice if missing.
  • Selected system_platform rows joined to inventory.hosts where system_platform.owner_id is NULL and inventory.system_profile->>'owner_id' is non-null, ordering by system_platform.id for deterministic processing.
  • Iterated over the selected rows in a FOR loop to update system_platform.owner_id by casting the JSON-extracted owner_id string to UUID.
  • Wrapped each update in a BEGIN/EXCEPTION block to catch any errors, logging a warning that includes inventory_id, owner_id, and SQLERRM without aborting the entire migration.
database/schema/upgrade_scripts/164-migrate-owner-id-to-system-platform.sql

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

SC Environment Impact Assessment

Overall Impact:NONE

No SC Environment-specific impacts detected in this PR.

What was checked

This PR was automatically scanned for:

  • Database migrations
  • ClowdApp configuration changes
  • Kessel integration changes
  • AWS service integrations (S3, RDS, ElastiCache)
  • Kafka topic changes
  • Secrets management changes
  • External dependencies

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 found 1 issue, and left some high level feedback:

  • The migration currently iterates row by row in a PL/pgSQL loop; this can be simplified and made more efficient with a single UPDATE system_platform sp SET owner_id = ih.system_profile->>'owner_id'::uuid FROM inventory.hosts ih WHERE sp.inventory_id = ih.id AND sp.owner_id IS NULL AND ih.system_profile->>'owner_id' IS NOT NULL statement while retaining exception handling if needed.
  • Consider narrowing the exception block to only catch UUID cast-related errors (e.g., invalid_text_representation) instead of WHEN OTHERS, so that unexpected issues during the migration are not silently downgraded to warnings.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The migration currently iterates row by row in a PL/pgSQL loop; this can be simplified and made more efficient with a single `UPDATE system_platform sp SET owner_id = ih.system_profile->>'owner_id'::uuid FROM inventory.hosts ih WHERE sp.inventory_id = ih.id AND sp.owner_id IS NULL AND ih.system_profile->>'owner_id' IS NOT NULL` statement while retaining exception handling if needed.
- Consider narrowing the exception block to only catch UUID cast-related errors (e.g., `invalid_text_representation`) instead of `WHEN OTHERS`, so that unexpected issues during the migration are not silently downgraded to warnings.

## Individual Comments

### Comment 1
<location path="database/schema/upgrade_scripts/164-migrate-owner-id-to-system-platform.sql" line_range="28-29" />
<code_context>
+        UPDATE system_platform SET
+            owner_id = (rec.ih_owner_id)::UUID
+        WHERE id = rec.sp_id;
+      EXCEPTION WHEN OTHERS THEN
+        RAISE WARNING 'Migration failed for inventory_id=%, owner_id=%: %', rec.inventory_id, rec.ih_owner_id, SQLERRM;
+      END;
+    END LOOP;
</code_context>
<issue_to_address>
**issue (bug_risk):** Catching `WHEN OTHERS` globally can hide non-casting issues

`EXCEPTION WHEN OTHERS` turns all failures (including permissions, constraints, etc.) into warnings, so the migration can falsely appear successful. If you only expect cast issues, narrow this to specific errors like `invalid_text_representation` (and related cast errors) so unexpected problems still fail the migration while bad `owner_id` values are logged and handled explicitly.
</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 on lines +28 to +29
EXCEPTION WHEN OTHERS THEN
RAISE WARNING 'Migration failed for inventory_id=%, owner_id=%: %', rec.inventory_id, rec.ih_owner_id, SQLERRM;
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): Catching WHEN OTHERS globally can hide non-casting issues

EXCEPTION WHEN OTHERS turns all failures (including permissions, constraints, etc.) into warnings, so the migration can falsely appear successful. If you only expect cast issues, narrow this to specific errors like invalid_text_representation (and related cast errors) so unexpected problems still fail the migration while bad owner_id values are logged and handled explicitly.

@jdobes jdobes added the onhold label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants