Skip to content

Conversation

@abnegate
Copy link
Member

@abnegate abnegate commented Jun 10, 2025

Summary by CodeRabbit

  • Refactor
    • Improved readability of method declarations by reformatting parameters to multi-line style.
    • Enhanced error handling for database operations with more detailed exception processing.
    • Updated methods for increasing and decreasing document attributes to return the updated document instead of a boolean.
  • Tests
    • Adjusted tests to verify that attribute operations return the updated value rather than a boolean result.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 10, 2025

Walkthrough

The changes refactor the increaseDocumentAttribute and decreaseDocumentAttribute methods to return the updated Document instead of a boolean, move document retrieval and authorization checks inside transactions, improve error handling with detailed PDO exception processing, and reformat method signatures for readability. Tests are updated to expect the new return type.

Changes

File(s) Change Summary
src/Database/Adapter.php Reformatted the abstract method signature for increaseDocumentAttribute to multi-line.
src/Database/Adapter/MariaDB.php Reformatted increaseDocumentAttribute signature; replaced generic error handling with detailed PDO exception processing.
src/Database/Database.php Refactored increaseDocumentAttribute and decreaseDocumentAttribute to return Document, moved logic inside transactions, updated exception handling, and adjusted method signatures.
src/Database/Mirror.php Updated method signatures of increaseDocumentAttribute and decreaseDocumentAttribute to return Document instead of boolean.
tests/e2e/Adapter/Scopes/DocumentTests.php Updated tests to assert returned Document values from increase/decrease methods instead of booleans.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Database
    participant Adapter

    Client->>Database: increaseDocumentAttribute(collection, id, attribute, value, max)
    Database->>Database: Begin transaction
    Database->>Database: Fetch document with forUpdate lock
    Database->>Database: Authorize operation
    Database->>Adapter: increaseDocumentAttribute(collection, id, attribute, value, updatedAt, min, max)
    Adapter-->>Database: Success or throws PDOException
    Database->>Database: Purge cache, trigger event
    Database->>Database: Commit transaction
    Database-->>Client: Return updated Document
Loading

Poem

In the warren where data grows tall,
We nibbled at numbers, no bools left at all!
Now each hop returns a value, not just a cheer,
With careful transactions, our code’s crystal clear.
Bugs beware, for with every test,
The rabbits return what’s truly the best!
🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ae2055c and 376945c.

📒 Files selected for processing (2)
  • src/Database/Database.php (5 hunks)
  • tests/e2e/Adapter/Scopes/DocumentTests.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • src/Database/Database.php
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: Adapter Tests (SharedTables/SQLite)
  • GitHub Check: Adapter Tests (Postgres)
  • GitHub Check: Adapter Tests (Pool)
  • GitHub Check: Adapter Tests (SQLite)
  • GitHub Check: Adapter Tests (SharedTables/MySQL)
  • GitHub Check: Adapter Tests (SharedTables/Postgres)
  • GitHub Check: Adapter Tests (SharedTables/MariaDB)
  • GitHub Check: Adapter Tests (Mirror)
  • GitHub Check: Adapter Tests (MySQL)
  • GitHub Check: Adapter Tests (MariaDB)
  • GitHub Check: Unit Test
✨ 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.

Copy link
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: 3

🔭 Outside diff range comments (1)
tests/e2e/Adapter/Scopes/DocumentTests.php (1)

1024-1024: ⚠️ Potential issue

Inconsistent return type expectations in error test cases.

These test methods still expect boolean returns (true or false) from the increment/decrement methods, which is inconsistent with the refactored methods that now return the new attribute values. These should be updated to expect the appropriate exceptions or new values instead of booleans.

Lines:

  • 1024: testIncreaseLimitMax expects true but should likely expect an exception
  • 1036: testDecreaseLimitMin expects false but should likely expect an exception
  • 1049: testIncreaseTextAttribute expects false but should likely expect an exception
- $this->assertEquals(true, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase', 10.5, 102.4));
+ // This should likely expect an exception or updated value, not boolean true

- $this->assertEquals(false, $database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99));
+ // This should likely expect an exception, not boolean false

- $this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text'));
+ // This should likely expect an exception, not boolean false

Also applies to: 1036-1036, 1049-1049

♻️ Duplicate comments (1)
src/Database/Database.php (1)

5222-5226: Duplicate whitelist block

Same observations as in increaseDocumentAttribute() – typo and duplication.
Extract to a shared helper/constant to avoid divergence.

🧹 Nitpick comments (2)
src/Database/Database.php (2)

5093-5099: Doc-block return type is outdated

increaseDocumentAttribute() now returns int|float but the PHP-Doc still advertises int.
Update the annotation to keep IDEs, static analysers and code search tools in sync.

- * @return int The new value of the attribute after the increase
+ * @return int|float The new value of the attribute after the increase

5118-5134: Hard-coded whitelist & mis-spelling

  1. Typo – $whiteList$whitelist (project consistently uses camelCase).
  2. Instead of a literal array, leverage the existing constants – it avoids drift when new numeric types are added.
-$whiteList = [
-    self::VAR_INTEGER,
-    self::VAR_FLOAT
-];
+// Only numeric scalar types are allowed
+$whitelist = [self::VAR_INTEGER, self::VAR_FLOAT];

Consider extracting the list into a private constant to remove duplication with decreaseDocumentAttribute.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d4b2048 and 590a784.

📒 Files selected for processing (4)
  • src/Database/Adapter.php (1 hunks)
  • src/Database/Adapter/MariaDB.php (2 hunks)
  • src/Database/Database.php (5 hunks)
  • tests/e2e/Adapter/Scopes/DocumentTests.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/Database/Adapter.php (5)
src/Database/Adapter/Postgres.php (1)
  • increaseDocumentAttribute (1280-1312)
src/Database/Adapter/MariaDB.php (1)
  • increaseDocumentAttribute (1385-1429)
src/Database/Adapter/Pool.php (1)
  • increaseDocumentAttribute (463-466)
src/Database/Database.php (1)
  • increaseDocumentAttribute (5107-5185)
src/Database/Mirror.php (1)
  • increaseDocumentAttribute (951-954)
src/Database/Adapter/MariaDB.php (6)
src/Database/Adapter.php (2)
  • increaseDocumentAttribute (1156-1164)
  • execute (1202-1202)
src/Database/Adapter/Postgres.php (3)
  • increaseDocumentAttribute (1280-1312)
  • execute (87-107)
  • processException (1997-2030)
src/Database/Database.php (1)
  • increaseDocumentAttribute (5107-5185)
src/Database/Adapter/SQL.php (2)
  • execute (1753-1756)
  • processException (1744-1747)
src/Database/Adapter/SQLite.php (1)
  • processException (1225-1238)
src/Database/Adapter/MySQL.php (1)
  • processException (86-99)
src/Database/Database.php (5)
src/Database/Adapter.php (4)
  • increaseDocumentAttribute (1156-1164)
  • withTransaction (372-404)
  • getDocument (670-670)
  • trigger (429-439)
src/Database/Adapter/Postgres.php (1)
  • increaseDocumentAttribute (1280-1312)
src/Database/Adapter/MariaDB.php (1)
  • increaseDocumentAttribute (1385-1429)
src/Database/Document.php (5)
  • getAttribute (212-219)
  • getId (59-62)
  • isEmpty (386-389)
  • getUpdate (107-110)
  • getUpdatedAt (160-163)
src/Database/Validator/Authorization.php (4)
  • Authorization (7-225)
  • skip (160-170)
  • isValid (55-77)
  • getDescription (41-44)
🪛 GitHub Actions: CodeQL
src/Database/Database.php

[error] 5184-5184: Cannot call method getAttribute() on bool|Utopia\Database\Document.


[error] 5280-5280: Cannot call method getAttribute() on bool|Utopia\Database\Document.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup & Build Docker Image
🔇 Additional comments (9)
tests/e2e/Adapter/Scopes/DocumentTests.php (4)

995-995: LGTM: Test assertion correctly updated for new return type.

The assertion now verifies that increaseDocumentAttribute returns the new attribute value (101) instead of a boolean, which aligns with the method's refactored behavior.


1001-1001: LGTM: Test assertion correctly updated for new return type.

The assertion now verifies that decreaseDocumentAttribute returns the new attribute value (99) instead of a boolean, which is consistent with the refactored method behavior.


1005-1005: LGTM: Test assertion correctly updated for float operations.

The assertion correctly expects the new float value (105.5) to be returned by increaseDocumentAttribute, demonstrating that the method works correctly with both integer and float attributes.


1009-1009: LGTM: Test assertion correctly updated for float operations.

The assertion correctly expects the new float value (104.4) to be returned by decreaseDocumentAttribute, maintaining consistency with the new return type behavior.

src/Database/Adapter.php (1)

1156-1164: LGTM! Improved method signature readability.

The reformatting from single-line to multi-line method signature enhances code readability and follows consistent formatting standards.

src/Database/Adapter/MariaDB.php (2)

1385-1393: LGTM! Method signature formatting improved.

The multi-line method signature aligns with the abstract method declaration and enhances readability.


1422-1427: Excellent improvement to error handling.

The change from direct execution with generic DatabaseException to a proper try-catch block that leverages processException() is a significant improvement. This enables conversion of specific PDO error codes into domain-specific exceptions (TimeoutException, DuplicateException, etc.), providing better error handling and debugging capabilities.

src/Database/Database.php (2)

5180-5185: Potential bool dereference after earlier false return

This is the crash site highlighted by CodeQL: if the inner callback returned false, $document is a boolean.
Fixing the previous comment resolves this, but keep an assert here to satisfy static analysers:

assert($document instanceof Document);
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 5184-5184: Cannot call method getAttribute() on bool|Utopia\Database\Document.


5194-5197: Sync the return annotation for decreaseDocumentAttribute()

The PHP-Doc correctly lists int|float, but the method header comment a few lines earlier still says “@return int|float”.
Nothing critical, just confirm both annotations match.

@abnegate abnegate merged commit f0c28b7 into main Jun 10, 2025
15 checks passed
@abnegate abnegate deleted the feat-type-except branch June 10, 2025 03:53
@coderabbitai coderabbitai bot mentioned this pull request Aug 5, 2025
@coderabbitai coderabbitai bot mentioned this pull request Nov 5, 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