Skip to content

Disable index for array#601

Closed
fogelito wants to merge 1 commit into0.69.xfrom
disable-array-index
Closed

Disable index for array#601
fogelito wants to merge 1 commit into0.69.xfrom
disable-array-index

Conversation

@fogelito
Copy link
Copy Markdown
Contributor

@fogelito fogelito commented Jun 3, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation to immediately prevent creation of indexes on array attributes, displaying a consistent error message: "Index on array attributes is forbidden".
  • Tests

    • Updated tests to expect the new unified error message when attempting to create indexes on array attributes.
    • Enhanced exception handling in tests to ensure correct error types and messages are asserted.
    • Removed unused test trait from the base test class.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2025

Walkthrough

The changes enforce a strict prohibition against creating indexes on array attributes in the database layer. The validator now immediately rejects such attempts, and all related tests have been updated to expect a unified exception message. Test cases were adjusted to handle and assert this new behavior, ensuring consistency across the codebase.

Changes

Files/Paths Change Summary
src/Database/Validator/Index.php Modified checkArrayIndex to immediately reject indexes on array attributes with a unified error message.
tests/e2e/Adapter/Base.php Removed usage of the IndexTests trait from the Base abstract test class.
tests/e2e/Adapter/Scopes/AttributeTests.php Updated tests to expect unified "Index on array attributes is forbidden" exception; added try-catch blocks.
tests/e2e/Adapter/Scopes/CollectionTests.php Modified test to expect an exception when creating a collection with an index on an array attribute; removed assertions on created indexes.
tests/e2e/Adapter/Scopes/IndexTests.php Updated tests to assert IndexException with unified message for array attribute indexes; added debug output.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant Validator
    participant Database

    Tester->>Validator: Attempt to create index on array attribute
    Validator-->>Tester: Throw IndexException ("Index on array attributes is forbidden")
    Tester->>Tester: Catch and assert exception message
Loading

Poem

In the warren where data hops and flows,
No more indexes where the array grass grows.
If you try to build, the validator will shout,
"Forbidden!"—no arrays, the rabbit points out.
Tests now agree, with unified cheer,
The message is clear: no arrays indexed here! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@fogelito fogelito changed the title Start disabling index for array Disable index for array Jun 3, 2025
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (1)
src/Database/Validator/Index.php (1)

140-161: ⚠️ Potential issue

Remove unreachable code after early return.

The code following the early return statement is unreachable and should be removed to fix the PHPStan error in the pipeline.

Apply this diff to remove the unreachable code:

             if ($attribute->getAttribute('array', false)) {
                 $this->message = 'Index on array attributes is forbidden';
                 return false;
-
-                // Database::INDEX_UNIQUE Is not allowed! since mariaDB VS MySQL makes the unique Different on values
-                if ($index->getAttribute('type') != Database::INDEX_KEY) {
-                    $this->message = '"' . ucfirst($index->getAttribute('type')) . '" index is forbidden on array attributes';
-                    return false;
-                }
-
-                if (empty($lengths[$attributePosition])) {
-                    $this->message = 'Index length for array not specified';
-                    return false;
-                }
-
-                $arrayAttributes[] = $attribute->getAttribute('key', '');
-                if (count($arrayAttributes) > 1) {
-                    $this->message = 'An index may only contain one array attribute';
-                    return false;
-                }
-
-                $direction = $orders[$attributePosition] ?? '';
-                if (!empty($direction)) {
-                    $this->message = 'Invalid index order "' . $direction . '" on array attribute "'. $attribute->getAttribute('key', '') .'"';
-                    return false;
-                }
             } elseif ($attribute->getAttribute('type') !== Database::VAR_STRING && !empty($lengths[$attributePosition])) {
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 141-141: PHPStan: Unreachable statement - code above always terminates.

🧹 Nitpick comments (1)
tests/e2e/Adapter/Scopes/IndexTests.php (1)

373-373: Remove debug statement from production test.

The var_dump statement appears to be leftover debug code and should be removed from the production test suite.

Apply this diff to remove the debug statement:

-        var_dump(static::getDatabase()->getCollection('documents'));
-
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3db51ec and 25e1656.

⛔ Files ignored due to path filters (1)
  • composer.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • src/Database/Validator/Index.php (1 hunks)
  • tests/e2e/Adapter/Base.php (0 hunks)
  • tests/e2e/Adapter/Scopes/AttributeTests.php (3 hunks)
  • tests/e2e/Adapter/Scopes/CollectionTests.php (2 hunks)
  • tests/e2e/Adapter/Scopes/IndexTests.php (4 hunks)
💤 Files with no reviewable changes (1)
  • tests/e2e/Adapter/Base.php
🧰 Additional context used
🧬 Code Graph Analysis (2)
tests/e2e/Adapter/Scopes/IndexTests.php (4)
src/Database/Database.php (1)
  • Database (33-6732)
src/Database/Exception.php (1)
  • Exception (7-21)
src/Database/Validator/Index.php (1)
  • Index (10-312)
tests/e2e/Adapter/Base.php (1)
  • getDatabase (33-33)
tests/e2e/Adapter/Scopes/AttributeTests.php (5)
src/Database/Database.php (2)
  • Database (33-6732)
  • createIndex (3002-3119)
src/Database/Exception.php (1)
  • Exception (7-21)
src/Database/Validator/Index.php (1)
  • Index (10-312)
src/Database/Adapter.php (1)
  • createIndex (649-649)
src/Database/Adapter/Pool.php (1)
  • createIndex (213-216)
🪛 GitHub Actions: CodeQL
src/Database/Validator/Index.php

[error] 141-141: PHPStan: Unreachable statement - code above always terminates.

🪛 GitHub Actions: Linter
tests/e2e/Adapter/Scopes/IndexTests.php

[error] 1-1: PSR 12 linting failed: 'ordered_imports' rule violation.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup & Build Docker Image
🔇 Additional comments (10)
src/Database/Validator/Index.php (1)

137-139: LGTM! Clean implementation of array index prohibition.

The early return with the clear error message effectively enforces the new policy against indexing array attributes.

tests/e2e/Adapter/Scopes/IndexTests.php (2)

32-38: Excellent test update for array index prohibition.

The try-catch block correctly verifies that IndexException is thrown with the expected message when attempting to create an index on an array attribute.


47-47: Good consistency improvement.

Updating the duplicate attribute tests to expect IndexException instead of the generic DatabaseException provides better test specificity and consistency with the new validation behavior.

Also applies to: 54-54

tests/e2e/Adapter/Scopes/CollectionTests.php (2)

11-11: LGTM! Proper import addition.

The IndexException import is correctly placed in alphabetical order with other exception imports.


650-663: Excellent test coverage for collection creation with array indexes.

The test properly validates that attempting to create a collection with schema indexes on array attributes throws an IndexException with the correct message "Index on array attributes is forbidden". This ensures the prohibition is enforced at the collection creation level as well.

tests/e2e/Adapter/Scopes/AttributeTests.php (5)

15-15: LGTM: Necessary import for IndexException testing.

The import addition is required for the updated test cases that expect IndexException to be thrown when attempting to create indexes on array attributes.


1353-1359: LGTM: Correct test for array attribute index prohibition.

The test correctly verifies that attempting to create an index on an array attribute (cards) throws an IndexException with the expected message. This aligns with the PR objective to enforce strict prohibition against indexing array attributes.


1405-1405: LGTM: Updated exception message for fulltext index on array attributes.

The assertion correctly expects the unified error message "Index on array attributes is forbidden" when attempting to create a fulltext index on an array attribute, consistent with the new validation behavior.


1407-1407: LGTM: Consistent error message for unsupported fulltext indexes.

The assertion ensures that regardless of adapter fulltext support capabilities, the unified error message "Index on array attributes is forbidden" is used when attempting to create indexes on array attributes.


1415-1415: LGTM: Correct error message for multi-attribute index with array attributes.

The assertion properly expects the unified error message when attempting to create an index containing multiple attributes where at least one is an array attribute, ensuring comprehensive coverage of the array indexing prohibition.

use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Query as QueryException;
use Utopia\Database\Exception\Index as IndexException;
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.

⚠️ Potential issue

Fix import ordering to resolve PSR 12 linting error.

The new IndexException import should be ordered alphabetically with other exception imports.

Apply this diff to fix the import ordering:

 use Utopia\Database\Exception\Duplicate as DuplicateException;
+use Utopia\Database\Exception\Index as IndexException;
 use Utopia\Database\Exception\Limit as LimitException;
 use Utopia\Database\Exception\Query as QueryException;
-use Utopia\Database\Exception\Index as IndexException;
🤖 Prompt for AI Agents
In tests/e2e/Adapter/Scopes/IndexTests.php at line 12, the import statement for
IndexException is not ordered alphabetically according to PSR 12 standards.
Reorder the use statements so that the import for
Utopia\Database\Exception\Index as IndexException is placed in the correct
alphabetical position among other exception imports to fix the linting error.

@abnegate abnegate closed this Jul 21, 2025
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