Conversation
📝 WalkthroughWalkthrough
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.1.17)Note: Using configuration file /phpstan.neon. If the excluded path can sometimes exist, append (?) parameters: Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
Security Scan Results for PRDocker Image Scan Results
Source Code Scan Results🎉 No vulnerabilities found! |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (1)
1-1: Ensure PHP 8.3 syntax support and re-run PintPint correctly fixed the PSR-12 indentation in Upsert.php by re-indenting the commented debug lines, but you’re now hitting parse errors on PHP 8.3’s new typed class constants in several files (e.g. Audits.php, Attributes.php, Collections.php, Columns.php, Tables.php, and the GraphQL Base trait). PHP 8.3 does support declaring types on class constants (e.g.
const string X = '…';) (php.watch), yet Pint/PHPCS-Fixer in the current toolchain isn’t parsing that syntax (see laravel/pint #248) (github.com).• Verify your local and CI PHP version is ≥ 8.3.0 (composer.json requires PHP >=8.3.0).
• Update your formatter to a release of Laravel Pint (and its underlying PHP-CS-Fixer) that includes PHP 8.3 support:composer update laravel/pint friendsofphp/php-cs-fixer• Re-run Pint in “test” mode to confirm no remaining errors:
./vendor/bin/pint --test --config pint.json
🧹 Nitpick comments (3)
tests/e2e/Services/Databases/TablesDB/DatabasesBase.php (1)
1696-1724: Improve idempotent-upsert test: assert true no-op via$updatedAtand exact permissionsThe existing test only checks that the second PUT returns status 200, that the title remains “Thor: Ragnarok,” and that there are three permissions. To guarantee a true no-op, we should:
- Capture the row’s
$updatedAtand$permissionsimmediately before resubmitting.- After the second PUT, assert that
$updatedAtis unchanged and that the permission sets are identical (not merely equal in count).- (Optional) Construct the upsert payload from the persisted row to avoid mismatches between
nullvs.[]defaults.Locations to update:
- tests/e2e/Services/Databases/TablesDB/DatabasesBase.php, around lines 1696–1724.
Proposed diff:
@@ tests/e2e/Services/Databases/TablesDB/DatabasesBase.php $this->assertEquals('Thor: Ragnarok', $row['body']['title']); /** * Resubmit same document, nothing to update */ + // Capture current state to verify true no-op behavior + $before = $this->client->call( + Client::METHOD_GET, + '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, + array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()) + ); + $prevUpdatedAt = $before['body']['$updatedAt']; + $prevPermissions = $before['body']['$permissions']; @@ $row = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'data' => [ 'title' => 'Thor: Ragnarok', 'releaseYear' => 2000, 'integers' => [], 'birthDay' => null, 'duration' => null, 'starringActors' => [], 'actors' => [], 'tagline' => '', 'description' => '', ], 'permissions' => [ Permission::read(Role::users()), Permission::update(Role::users()), Permission::delete(Role::users()), ], ]); $this->assertEquals(200, $row['headers']['status-code']); $this->assertEquals('Thor: Ragnarok', $row['body']['title']); + $this->assertEquals(2000, $row['body']['releaseYear']); + // Prove no-op: timestamps and permissions unchanged + $this->assertEquals( + $prevUpdatedAt, + $row['body']['$updatedAt'], + 'Upsert with identical data should not bump $updatedAt' + ); + $this->assertEqualsCanonicalizing( + $prevPermissions, + $row['body']['$permissions'], + 'Permissions should remain identical on no-op' + );Optional safer payload construction to mirror persisted shape:
// Build payload from persisted row to avoid [] vs. null drift $writableKeys = ['title','releaseYear','integers','birthDay','duration','starringActors','actors','tagline','description']; $sameData = array_intersect_key($before['body'], array_flip($writableKeys)); // Then PUT with 'data' => $sameDatatests/e2e/Services/Databases/Legacy/DatabasesBase.php (2)
1756-1794: PUT with array payload under “data” is schema-invalid; clarify intent and avoid false positivesThis PUT sends an array for “data” where the API expects an object. The test then asserts 200 and that the resource is unchanged. If the goal is to validate server-side input rejection, assert a 400. If the goal is to validate idempotent behavior when payload is invalid/ignored, make it explicit and compare against a snapshot from the previous step. Also, drop the debug dump.
Option A (preferred: treat invalid shape as client error):
@@ - $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ + $invalid = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'data' => [ [ 'title' => 'Thor: Ragnarok1', @@ ] ], 'permissions' => [ Permission::read(Role::users()), Permission::update(Role::users()), Permission::delete(Role::users()), ], ]); -var_dump($document); - $this->assertEquals(200, $document['headers']['status-code']); - $this->assertEquals('Thor: Ragnarok', $document['body']['title']); - $this->assertCount(3, $document['body']['$permissions']); + $this->assertEquals(400, $invalid['headers']['status-code']);Option B (if the API intentionally ignores array payloads and returns the existing document): assert explicitly that nothing changed relative to the previous snapshot.
@@ - $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ + $unchanged = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'data' => [ [ @@ ], ]); -var_dump($document); - $this->assertEquals(200, $document['headers']['status-code']); - $this->assertEquals('Thor: Ragnarok', $document['body']['title']); - $this->assertCount(3, $document['body']['$permissions']); + $this->assertEquals(200, $unchanged['headers']['status-code']); + $this->assertEquals($prev['body']['title'], $unchanged['body']['title']); + $this->assertEquals($prev['body']['releaseYear'], $unchanged['body']['releaseYear']); + $this->assertEqualsCanonicalizing($prev['body']['$permissions'], $unchanged['body']['$permissions']);Follow-up: do you want this negative test split into its own method (e.g., testUpsertDocumentRejectsArrayPayload) for clearer intent?
1791-1794: Strengthen permission assertions to verify exact set, not just countCounting to 3 won’t catch wrong roles/types. Assert canonical equality against the expected permissions.
- $this->assertCount(3, $document['body']['$permissions']); + $this->assertCount(3, $document['body']['$permissions']); + $this->assertEqualsCanonicalizing( + [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ], + $document['body']['$permissions'] + );
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php(2 hunks)tests/e2e/Services/Databases/Legacy/DatabasesBase.php(1 hunks)tests/e2e/Services/Databases/TablesDB/DatabasesBase.php(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/e2e/Services/Databases/TablesDB/DatabasesBase.php (1)
tests/e2e/Client.php (1)
Client(8-328)
tests/e2e/Services/Databases/Legacy/DatabasesBase.php (1)
tests/e2e/Client.php (1)
Client(8-328)
🪛 GitHub Actions: Linter
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php
[error] 1-1: Pint: PSR-12 'statement_indentation' violation.
tests/e2e/Services/Databases/Legacy/DatabasesBase.php
[error] 1-1: Pint: PSR-12 'statement_indentation' violation.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
- GitHub Check: E2E Service Test (VCS)
- GitHub Check: E2E Service Test (Migrations)
- GitHub Check: E2E Service Test (Tokens)
- GitHub Check: E2E Service Test (Storage)
- GitHub Check: E2E Service Test (Realtime)
- GitHub Check: E2E Service Test (Health)
- GitHub Check: E2E Service Test (GraphQL)
- GitHub Check: E2E Service Test (Locale)
- GitHub Check: E2E Service Test (Projects)
- GitHub Check: E2E Service Test (Console)
- GitHub Check: E2E Service Test (Functions)
- GitHub Check: E2E Service Test (Account)
- GitHub Check: E2E Service Test (Avatars)
- GitHub Check: E2E Service Test (Databases/Legacy)
- GitHub Check: E2E Service Test (Dev Keys)
- GitHub Check: E2E Service Test (Site Screenshots)
- GitHub Check: E2E General Test
- GitHub Check: Unit Test
- GitHub Check: Setup & Build Appwrite Image
- GitHub Check: scan
🔇 Additional comments (1)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (1)
276-279: Harden fallback retrieval after no-op upsert
Elevate the fetch to skip authorization and fail fast if the document remains empty, using the sameDOCUMENT_NOT_FOUNDconstant as other document actions.Affected file:
- src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (around lines 276–279)
Proposed diff:
- if (empty($upserted[0])) { - $upserted[0] = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId); - } + if (empty($upserted[0])) { + // No onNext emitted (likely a no-op update) — fetch explicitly with elevated auth. + $doc = Authorization::skip(fn () => $dbForProject->getDocument( + 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $documentId + )); + if ($doc->isEmpty()) { + // Throw the standard exception used by document controllers. + throw new Exception(Exception::DOCUMENT_NOT_FOUND); + } + $upserted[0] = $doc; + }Notes:
- The
DOCUMENT_NOT_FOUNDconstant is defined insrc/Appwrite/Extend/Exception.phpand is already used bygetNotFoundException()inDocuments/Action.phpfor both collections and row-based APIs.- No other changes are required.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php
Outdated
Show resolved
Hide resolved
✨ Benchmark results
⚡ Benchmark Comparison
|
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (2)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (1)
100-103: Reject any top-level array payload for "data" and use InvalidStructure, not MissingPayloadAllowing a single-item list passes a malformed structure downstream (mixing numeric index 0 with
$idwhen you later set$data['$id']), which can yield subtle corruption. Semantically, this is not a “missing payload” but an invalid structure. Enforce object-only payloads.Apply this diff:
- if (\array_is_list($data) && \count($data) > 1) { // Allow 1 associated array - throw new Exception($this->getMissingPayloadException()); - } + // Reject top-level JSON arrays; document 'data' must be an object + if (\is_array($data) && \array_is_list($data)) { + throw new Exception( + $this->getInvalidStructureException(), + 'Payload "data" must be a JSON object, not an array.' + ); + }tests/e2e/Services/Databases/Legacy/DatabasesBase.php (1)
1731-1759: Strengthen the “no-op upsert” test to actually prove idempotence and avoid unintended mutationsThe current payload adds attributes that may not exist on the document yet (e.g.,
actors,integers), which can cause a real update. Capture the pre-state, send only truly unchanged fields, and assert that metadata like$updatedAt,$sequence, and$permissionsremain stable.Apply this diff:
$this->assertEquals('Thor: Ragnarok', $document['body']['title']); - /** - * Resubmit same document, nothing to update - */ - $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ + /** + * Resubmit same document, nothing to update (true no-op) + */ + $prev = $document; // snapshot GET state before no-op PUT + $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'data' => [ 'title' => 'Thor: Ragnarok', - 'releaseYear' => 2000, - 'integers' => [], - 'birthDay' => null, - 'duration' => null, - 'starringActors' => [], - 'actors' => [], - 'tagline' => '', - 'description' => '', + 'releaseYear' => 2000 ], 'permissions' => [ Permission::read(Role::users()), Permission::update(Role::users()), Permission::delete(Role::users()), ], ]); $this->assertEquals(200, $document['headers']['status-code']); $this->assertEquals('Thor: Ragnarok', $document['body']['title']); $this->assertCount(3, $document['body']['$permissions']); + // Prove idempotence: critical fields unchanged + $this->assertEquals($prev['body']['$id'], $document['body']['$id']); + $this->assertEquals($prev['body']['releaseYear'], $document['body']['releaseYear']); + $this->assertEquals($prev['body']['$sequence'], $document['body']['$sequence']); + $this->assertEquals($prev['body']['$updatedAt'], $document['body']['$updatedAt'], 'updatedAt must not change on a no-op PUT'); + $this->assertEqualsCanonicalizing($prev['body']['$permissions'], $document['body']['$permissions']);Notes:
- Removing empty arrays/nullable fields avoids converting “attribute absent” into “attribute present but empty,” which would mutate the document and defeat the intent of a no-op test.
🧹 Nitpick comments (1)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (1)
271-275: Make the fallback fetch authorization-safe and fail fast if still emptyIf the callback doesn’t emit a document, the immediate fetch should bypass permissions (the caller may have just removed their own read permission) and we should guard against an unexpected empty result instead of proceeding.
Apply this diff:
- if (empty($upserted[0])) { - $upserted[0] = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId); - } + if (empty($upserted[0])) { + $upserted[0] = Authorization::skip( + fn () => $dbForProject->getDocument( + 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $documentId + ) + ); + // Defensive: if still empty, surface a clear error instead of continuing with an empty document + if ($upserted[0]->isEmpty()) { + throw new Exception(Exception::DOCUMENT_NOT_FOUND); + } + }Follow-up:
- Can
createOrUpdateDocuments(..., onNext: ...)ever legitimately yield no items for a single-document upsert? If not, consider asserting on$upsertedsize and removing the fallback entirely to simplify the code path.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php(2 hunks)tests/e2e/Services/Databases/Legacy/DatabasesBase.php(1 hunks)tests/e2e/Services/Databases/TablesDB/DatabasesBase.php(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/e2e/Services/Databases/TablesDB/DatabasesBase.php
🧰 Additional context used
🧬 Code graph analysis (2)
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php (2)
src/Appwrite/Extend/Exception.php (1)
Exception(7-442)src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php (1)
getMissingPayloadException(184-189)
tests/e2e/Services/Databases/Legacy/DatabasesBase.php (2)
tests/e2e/Client.php (1)
Client(8-328)src/Appwrite/Deletes/Targets.php (1)
delete(12-23)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
- GitHub Check: E2E Service Test (VCS)
- GitHub Check: E2E Service Test (Users)
- GitHub Check: E2E Service Test (Teams)
- GitHub Check: E2E Service Test (Webhooks)
- GitHub Check: E2E Service Test (Sites)
- GitHub Check: E2E Service Test (Databases/TablesDB)
- GitHub Check: E2E Service Test (Projects)
- GitHub Check: E2E Service Test (Avatars)
- GitHub Check: E2E Service Test (GraphQL)
- GitHub Check: E2E Service Test (Functions)
- GitHub Check: E2E Service Test (Health)
- GitHub Check: E2E Service Test (Databases/Legacy)
- GitHub Check: E2E Service Test (FunctionsSchedule)
- GitHub Check: E2E Service Test (Account)
- GitHub Check: E2E Service Test (Console)
- GitHub Check: E2E Service Test (Site Screenshots)
- GitHub Check: E2E Service Test (Dev Keys)
- GitHub Check: Unit Test
- GitHub Check: E2E General Test
- GitHub Check: Benchmark
What does this PR do?
(Provide a description of what this PR does and why it's needed.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Screenshots may also be helpful.)
Related PRs and Issues
Checklist