Skip to content

feat(db): create systems table#2754

Merged
petrblaho merged 1 commit into
RedHatInsights:masterfrom
petrblaho:ciao-cyndi-systems-table
Jun 8, 2026
Merged

feat(db): create systems table#2754
petrblaho merged 1 commit into
RedHatInsights:masterfrom
petrblaho:ciao-cyndi-systems-table

Conversation

@petrblaho

Copy link
Copy Markdown
Collaborator

Setup UUID-based systems table with org_id, tags, system_profile,
and unique insights_id to host data locally.

https://redhat.atlassian.net/browse/RHINENG-23278

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

@petrblaho petrblaho requested a review from a team as a code owner June 1, 2026 11:04
@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Adds a Rails migration that creates a systems table (UUID primary key) with account/org identifiers, display metadata, JSONB fields (tags, system_profile, groups), timestamps including stale_timestamp, soft-delete deleted_at, optional insights_id (unique), and updates db/schema.rb version.

Changes

Systems Table Creation

Layer / File(s) Summary
Create systems table migration
db/migrate/20260601090600_create_systems.rb, db/schema.rb
Rails migration adds systems table with UUID id, account, required org_id, required display_name, JSONB tags (default {}), system_profile (default {}), JSONB groups (default []), datetime created, updated, stale_timestamp (not null), optional insights_id (unique index), optional deleted_at, and updates schema version in db/schema.rb.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(db): create systems table' clearly and concisely describes the main change: adding a new systems table to the database.
Description check ✅ Passed The description includes the secure coding checklist template and provides context about the systems table setup with a tracking ticket reference, meeting the required structure.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🧹 Nitpick comments (1)
db/migrate/20260601090600_create_systems.rb (1)

3-19: Consider adding indexes on frequently queried columns.

The migration doesn't include indexes on org_id, account, or deleted_at, which are likely to be used in WHERE clauses for common queries:

  • org_id: Filtering systems by organization
  • deleted_at: Soft-delete queries (e.g., WHERE deleted_at IS NULL)
  • Composite index on (org_id, deleted_at): For queries filtering by both

These indexes can be added later once query patterns are established and performance monitoring indicates they're beneficial. Consider adding them in a follow-up migration if query performance becomes a concern.

Example indexes for common query patterns
add_index :systems, :org_id
add_index :systems, :deleted_at
# Or a composite index for queries filtering by both:
add_index :systems, [:org_id, :deleted_at]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@db/migrate/20260601090600_create_systems.rb` around lines 3 - 19, The
migration creates the systems table but omits indexes on frequently queried
columns; add indexes for org_id and deleted_at (and account if used in queries)
and consider a composite index on [:org_id, :deleted_at] to optimize common
WHERE clauses; update the migration by adding add_index :systems, :org_id,
add_index :systems, :deleted_at, optionally add_index :systems, :account, and/or
add_index :systems, [:org_id, :deleted_at] (use the existing create_table
:systems and add_index :systems, :insights_id context to place these).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@db/migrate/20260601090600_create_systems.rb`:
- Around line 8-9: The migration currently defines custom timestamp columns
t.datetime :created and t.datetime :updated alongside Rails' t.timestamps,
creating duplicate timestamp columns; remove the custom t.datetime :created and
t.datetime :updated lines and keep t.timestamps so ActiveRecord manages
created_at/updated_at automatically (locate these symbols in the CreateSystems
migration and delete the two custom timestamp declarations).
- Line 12: Update the migration definition for the groups JSONB column to
prevent NULLs: change the column declaration "t.jsonb :groups, default: []" to
include "null: false" (i.e. "t.jsonb :groups, default: [], null: false") in the
CreateSystems migration (the line with t.jsonb :groups) so the groups field has
the same non-null constraint as tags and system_profile; if this migration has
already run in environments, instead add a separate migration that sets existing
NULL groups to [] and then alters the column to set NOT NULL on the systems
table.

---

Nitpick comments:
In `@db/migrate/20260601090600_create_systems.rb`:
- Around line 3-19: The migration creates the systems table but omits indexes on
frequently queried columns; add indexes for org_id and deleted_at (and account
if used in queries) and consider a composite index on [:org_id, :deleted_at] to
optimize common WHERE clauses; update the migration by adding add_index
:systems, :org_id, add_index :systems, :deleted_at, optionally add_index
:systems, :account, and/or add_index :systems, [:org_id, :deleted_at] (use the
existing create_table :systems and add_index :systems, :insights_id context to
place these).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: a37639a0-748b-441e-b682-106245aee8e0

📥 Commits

Reviewing files that changed from the base of the PR and between b744c1a and b6d8b3d.

📒 Files selected for processing (1)
  • db/migrate/20260601090600_create_systems.rb

Comment thread db/migrate/20260608090600_create_systems.rb
t.datetime :created, null: false
t.datetime :stale_timestamp, null: false
t.jsonb :system_profile, null: false, default: {}
t.jsonb :groups, default: []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add null: false constraint to groups for consistency.

The groups field allows NULL values, unlike the other JSONB fields (tags and system_profile) which have null: false constraints. This inconsistency could lead to unexpected behavior when querying or processing group data.

🛡️ Proposed fix to add null constraint
-      t.jsonb :groups, default: []
+      t.jsonb :groups, null: false, default: []
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
t.jsonb :groups, default: []
t.jsonb :groups, null: false, default: []
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@db/migrate/20260601090600_create_systems.rb` at line 12, Update the migration
definition for the groups JSONB column to prevent NULLs: change the column
declaration "t.jsonb :groups, default: []" to include "null: false" (i.e.
"t.jsonb :groups, default: [], null: false") in the CreateSystems migration (the
line with t.jsonb :groups) so the groups field has the same non-null constraint
as tags and system_profile; if this migration has already run in environments,
instead add a separate migration that sets existing NULL groups to [] and then
alters the column to set NOT NULL on the systems table.

@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch 2 times, most recently from c3436ef to 8983412 Compare June 1, 2026 12:10

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
db/schema.rb (1)

13-13: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Update db/schema.rb schema version to latest migration timestamp

db/schema.rb sets ActiveRecord::Schema[8.1].define(version: 2026_05_13_140100) but the newest migration is db/migrate/20260601090600_create_systems.rb, so the schema version should be 2026_06_01_090600.

🔧 Proposed fix
-ActiveRecord::Schema[8.1].define(version: 2026_05_13_140100) do
+ActiveRecord::Schema[8.1].define(version: 2026_06_01_090600) do
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@db/schema.rb` at line 13, The schema version in db/schema.rb is outdated;
update the value passed to ActiveRecord::Schema[...] .define(...) from
2026_05_13_140100 to the latest migration timestamp 2026_06_01_090600 so the
schema version matches the newest migration (migration file
20260601090600_create_systems.rb); locate the line containing
ActiveRecord::Schema[8.1].define(version: ...) and replace the version token
accordingly.
🧹 Nitpick comments (2)
db/schema.rb (2)

237-237: ⚡ Quick win

Consider adding null: false constraint to groups for consistency.

The groups column has a default value of [] but lacks a null: false constraint, unlike the tags and system_profile columns which both have null: false. This inconsistency means groups could be NULL despite having a default, requiring extra null checks in application code.

For consistency and to prevent NULL/empty-array confusion, consider adding null: false to the groups column definition.

🔧 Suggested improvement

Update the migration file db/migrate/20260601090600_create_systems.rb:

-  t.jsonb :groups, default: []
+  t.jsonb :groups, default: [], null: false

Then regenerate the schema:

rails db:migrate:redo VERSION=20260601090600
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@db/schema.rb` at line 237, The groups JSONB column currently has default: []
but lacks a null: false constraint; update the migration that creates the
systems table (the CreateSystems migration) to change the column definition for
"groups" to t.jsonb "groups", default: [], null: false so the DB enforces
non-null values, then re-run the migration (redo the migration version that
creates systems) and regenerate the schema so db/schema.rb reflects the new
null: false constraint.

232-232: 💤 Low value

Consider whether account should be required.

The account column is nullable while org_id is required. If account information is always available and should be tracked for all systems, adding null: false would enforce data integrity.

However, if systems can exist without account assignments (e.g., during migration scenarios or when ingesting from external sources that only provide org_id), the current nullable definition is appropriate.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@db/schema.rb` at line 232, Decide whether the "account" column should be
required: if it must always be present, add a migration to set any existing NULL
"account" values to a valid default or backfill from related data, then alter
the column to add NOT NULL (e.g., change_column_null :table_name, :account,
false) and add a model-level validation (validates :account, presence: true); if
it can remain optional, leave the schema as-is but consider adding a comment
explaining why it's nullable. Ensure you reference the "account" and "org_id"
columns when making the change so the migration and model validation target the
correct column.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@db/schema.rb`:
- Around line 233-234: Decide and document a single source-of-truth for system
timestamps: either remove the custom columns (:created / :updated) from the
CreateSystems migration (db/migrate/20260601090600_create_systems.rb) and rely
on Rails timestamps (created_at/updated_at), or keep the custom columns and stop
using t.timestamps; then update the System model to explicitly map/alias the
authoritative columns (e.g., alias_attribute :created, :created_at or vice
versa) and ensure record_timestamps behavior is correct, update the API schema
mapping in spec/api/v2/schemas/system.rb so the API’s `updated` field points to
the chosen column, and add a short comment in the migration and the System model
explaining which pair is authoritative and whether they are kept in sync.

---

Outside diff comments:
In `@db/schema.rb`:
- Line 13: The schema version in db/schema.rb is outdated; update the value
passed to ActiveRecord::Schema[...] .define(...) from 2026_05_13_140100 to the
latest migration timestamp 2026_06_01_090600 so the schema version matches the
newest migration (migration file 20260601090600_create_systems.rb); locate the
line containing ActiveRecord::Schema[8.1].define(version: ...) and replace the
version token accordingly.

---

Nitpick comments:
In `@db/schema.rb`:
- Line 237: The groups JSONB column currently has default: [] but lacks a null:
false constraint; update the migration that creates the systems table (the
CreateSystems migration) to change the column definition for "groups" to t.jsonb
"groups", default: [], null: false so the DB enforces non-null values, then
re-run the migration (redo the migration version that creates systems) and
regenerate the schema so db/schema.rb reflects the new null: false constraint.
- Line 232: Decide whether the "account" column should be required: if it must
always be present, add a migration to set any existing NULL "account" values to
a valid default or backfill from related data, then alter the column to add NOT
NULL (e.g., change_column_null :table_name, :account, false) and add a
model-level validation (validates :account, presence: true); if it can remain
optional, leave the schema as-is but consider adding a comment explaining why
it's nullable. Ensure you reference the "account" and "org_id" columns when
making the change so the migration and model validation target the correct
column.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 3d7d12c5-f2c5-4af0-bf01-9da4c3d9b7e1

📥 Commits

Reviewing files that changed from the base of the PR and between b6d8b3d and 8983412.

📒 Files selected for processing (2)
  • db/migrate/20260601090600_create_systems.rb
  • db/schema.rb
🚧 Files skipped from review as they are similar to previous changes (1)
  • db/migrate/20260601090600_create_systems.rb

Comment thread db/schema.rb Outdated
@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch 2 times, most recently from 3f6ae31 to 508eedc Compare June 1, 2026 12:29
@petrblaho

Copy link
Copy Markdown
Collaborator Author

/retest

@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.53%. Comparing base (25f3ed6) to head (7daa784).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2754   +/-   ##
=======================================
  Coverage   93.53%   93.53%           
=======================================
  Files         184      184           
  Lines        4390     4390           
=======================================
  Hits         4106     4106           
  Misses        284      284           

☔ 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.

@RoamingNoMaD

Copy link
Copy Markdown
Collaborator

Pretty please hold this back until we merge the DB remodel.
If I have to resolve merge conflicts in that monster once more I will cry. :(

@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch from 508eedc to 70e150e Compare June 3, 2026 12:49
@RoamingNoMaD

Copy link
Copy Markdown
Collaborator

Seemingly unrelated kafka consumer test failure.

For the record, we should go forward with this. The DB remodel release will happen next week due to stage instability...

@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch 2 times, most recently from 0687777 to 7daa784 Compare June 4, 2026 12:56
@petrblaho petrblaho enabled auto-merge (rebase) June 4, 2026 13:43
auto-merge was automatically disabled June 4, 2026 15:21

Branch protection rule check failed

@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch 2 times, most recently from 6cb4b6e to e612860 Compare June 8, 2026 08:45
Setup UUID-based systems table with org_id, tags, system_profile,
and unique insights_id to host data locally.

https://redhat.atlassian.net/browse/RHINENG-23278
@petrblaho petrblaho force-pushed the ciao-cyndi-systems-table branch from e612860 to dc33671 Compare June 8, 2026 08:52
@petrblaho petrblaho merged commit f9166eb into RedHatInsights:master Jun 8, 2026
14 of 17 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