Skip to content

[Fix] Prevent database users page crash when a linked database is deleted - #1222

Merged
RichardAnderson merged 4 commits into
vitodeploy:4.xfrom
RichardAnderson:fix/database-users
Aug 1, 2026
Merged

[Fix] Prevent database users page crash when a linked database is deleted#1222
RichardAnderson merged 4 commits into
vitodeploy:4.xfrom
RichardAnderson:fix/database-users

Conversation

@RichardAnderson

@RichardAnderson RichardAnderson commented Aug 1, 2026

Copy link
Copy Markdown
Member

Deleting a mid-list linked database left a gap in the user's databases array, which serialized to a JSON object instead of an array and crashed the database users page on render - reindexed on delete, added a migration to heal existing rows, and hardened the cell component.

Single line tinker fix for 4.0.1 implementations unable or unwilling to update, however, if the root cause happens again, this will need to be rerun, use at your own risk, run a backup first.

php artisan tinker --execute="App\Models\DatabaseUser::all()->filter(fn (\$u) => is_array(\$u->databases))->each(fn (\$u) => \$u->update(['databases' => array_values(\$u->databases)]));"

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Fixed database removal so linked users’ remaining database entries are retained and displayed consistently.
    • Improved handling of database lists stored in different JSON formats.
  • Data Migration

    • Reindexed database lists with gaps into correctly ordered lists.
    • Preserved valid list data while skipping invalid or unsupported values.
    • Added safeguards for null, scalar, and mixed-value entries.

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 6758f422-39ce-4591-b90b-14ecb04d1257

📥 Commits

Reviewing files that changed from the base of the PR and between 1e5691a and 8ca72b6.

📒 Files selected for processing (2)
  • tests/Feature/DatabaseTest.php
  • tests/Feature/DatabaseUserDatabasesMigrationTest.php

📝 Walkthrough

Walkthrough

The change keeps linked database lists indexed after deletion, adds migration support for sparse JSON objects, and allows the frontend component to render object values. Feature tests cover deletion and migration behaviour.

Changes

Database list normalisation

Layer / File(s) Summary
Database list migration
database/migrations/..., tests/Feature/DatabaseUserDatabasesMigrationTest.php
The migration converts sparse JSON objects into indexed arrays in batches. It skips invalid JSON and existing arrays. Tests cover transformed, unchanged, null, and scalar values.
Deletion list reindexing
app/Models/Database.php, tests/Feature/DatabaseTest.php
Database deletion applies array_values to linked database names. The feature test checks stored JSON and the database-user response.
Database list rendering
resources/js/components/database-user-databases.tsx
The component extracts object values and renders them only when all values are strings.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • vitodeploy/vito#1161: Both changes update resources/js/components/database-user-databases.tsx to normalise and render database lists.

Suggested reviewers: saeedvaziry

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the database users page crash and its cause, matching the main changes in the pull request.
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.
✨ 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.

@RichardAnderson
RichardAnderson marked this pull request as ready for review August 1, 2026 09:22

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

Actionable comments posted: 3

🤖 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 `@database/migrations/2026_08_01_090402_reindex_database_users_databases.php`:
- Around line 8-10: Remove the redundant PHPDoc blocks above the migration
methods, including both the “Run the migrations” and corresponding rollback
documentation blocks; leave the method signatures and migration behavior
unchanged.
- Around line 13-23: Update the migration’s database_users rewrite inside the
chunkById callback to use an optimistic conditional update: include the original
databases value in the update predicate, and retry the read/transform/update
when the condition no longer matches. Ensure stale JSON cannot overwrite
concurrent database-link changes, while preserving the existing filtering and
array normalization behavior.

In `@resources/js/components/database-user-databases.tsx`:
- Line 5: Update the databases value normalization expression to accept only
arrays whose elements are strings or objects whose values are all strings; use
Object.values only after validating the object shape, and return an empty list
for null, scalar, or otherwise malformed values.
🪄 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: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7472a55d-84f4-4dcf-8ea4-3c6af48c68d8

📥 Commits

Reviewing files that changed from the base of the PR and between d306018 and 0b4acde.

📒 Files selected for processing (5)
  • app/Models/Database.php
  • database/migrations/2026_08_01_090402_reindex_database_users_databases.php
  • resources/js/components/database-user-databases.tsx
  • tests/Feature/DatabaseTest.php
  • tests/Feature/DatabaseUserDatabasesMigrationTest.php

Comment thread database/migrations/2026_08_01_090402_reindex_database_users_databases.php Outdated
Comment thread resources/js/components/database-user-databases.tsx Outdated

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

Actionable comments posted: 2

🤖 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 `@tests/Feature/DatabaseUserDatabasesMigrationTest.php`:
- Around line 58-61: Update the database assertion in the migration test to use
assertDatabaseHas('database_users', [...]) for the row identified by
$databaseUser->id, preserving the exact JSON string
["db_one","db_three","db_six"] as the databases value and removing the direct
DB::table(...)->value() assertion.
- Line 56: Update the manual recovery statement in the DatabaseUser migration
test to modify only records whose databases value is non-null and an array,
preserving NULL and scalar values unchanged. Avoid applying array_values to
invalid types, and add fixtures/assertions covering both NULL and non-array
databases values.
🪄 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: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2cd115c1-132e-46b4-9015-7e27805b2e22

📥 Commits

Reviewing files that changed from the base of the PR and between 0b4acde and b38a6c2.

📒 Files selected for processing (3)
  • database/migrations/2026_08_01_090402_reindex_database_users_databases.php
  • resources/js/components/database-user-databases.tsx
  • tests/Feature/DatabaseUserDatabasesMigrationTest.php
💤 Files with no reviewable changes (1)
  • database/migrations/2026_08_01_090402_reindex_database_users_databases.php

Comment thread tests/Feature/DatabaseUserDatabasesMigrationTest.php Outdated
Comment thread tests/Feature/DatabaseUserDatabasesMigrationTest.php Outdated

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

Actionable comments posted: 2

🤖 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 `@tests/Feature/DatabaseUserDatabasesMigrationTest.php`:
- Around line 52-56: Replace the three DB::table('database_users') fixture
updates with DatabaseUser::query()->whereKey(...) updates, preserving the
existing sparse JSON object, null, and scalar values and bypassing model casts.
- Line 58: Replace the unbounded DatabaseUser::all() scan with a
DatabaseUser::query() pipeline using whereNotNull('databases') and lazyById(100)
or chunkById(), while preserving the existing is_array($u->databases) guard and
update behavior.
🪄 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: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5c02ca2a-75b2-4fc5-8c58-6e81d2b25a48

📥 Commits

Reviewing files that changed from the base of the PR and between b38a6c2 and 1e5691a.

📒 Files selected for processing (1)
  • tests/Feature/DatabaseUserDatabasesMigrationTest.php

Comment thread tests/Feature/DatabaseUserDatabasesMigrationTest.php Outdated
Comment thread tests/Feature/DatabaseUserDatabasesMigrationTest.php Outdated
@RichardAnderson
RichardAnderson merged commit 548a6db into vitodeploy:4.x Aug 1, 2026
6 checks passed
@RichardAnderson
RichardAnderson deleted the fix/database-users branch August 1, 2026 11:12
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