Upsert document no change by fogelito · Pull Request #10364 · appwrite/appwrite · GitHub
Skip to content

Upsert document no change - #10364

Merged
abnegate merged 3 commits into
1.8.xfrom
upsert-documnet-no-change-1.8
Aug 25, 2025
Merged

abnegate merged 3 commits into
1.8.xfrom
upsert-documnet-no-change-1.8

Conversation

@fogelito

Copy link
Copy Markdown
Contributor

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

  • (Related PR or issue)

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@coderabbitai

coderabbitai Bot commented Aug 24, 2025 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Aug 24, 2025 •

Copy link
Copy Markdown

Security Scan Results for PR

Docker Image Scan Results

Package Version Vulnerability Severity
libecpg 17.5-r0 CVE-2025-8714 HIGH
libecpg 17.5-r0 CVE-2025-8715 HIGH
libecpg-dev 17.5-r0 CVE-2025-8714 HIGH
libecpg-dev 17.5-r0 CVE-2025-8715 HIGH
libpq 17.5-r0 CVE-2025-8714 HIGH
libpq 17.5-r0 CVE-2025-8715 HIGH
libpq-dev 17.5-r0 CVE-2025-8714 HIGH
libpq-dev 17.5-r0 CVE-2025-8715 HIGH
postgresql17-dev 17.5-r0 CVE-2025-8714 HIGH
postgresql17-dev 17.5-r0 CVE-2025-8715 HIGH
golang.org/x/crypto v0.31.0 CVE-2025-22869 HIGH
golang.org/x/oauth2 v0.24.0 CVE-2025-22868 HIGH
stdlib 1.22.10 CVE-2025-47907 HIGH

Source Code Scan Results

🎉 No vulnerabilities found!

@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

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 Pint

Pint 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 $updatedAt and exact permissions

The 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 $updatedAt and $permissions immediately before resubmitting.
  • After the second PUT, assert that $updatedAt is 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 null vs. [] 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' => $sameData
tests/e2e/Services/Databases/Legacy/DatabasesBase.php (2)

1756-1794: PUT with array payload under “data” is schema-invalid; clarify intent and avoid false positives

This 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 count

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

📥 Commits

Reviewing files that changed from the base of the PR and between 038290a and ba79dda.

📒 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 same DOCUMENT_NOT_FOUND constant 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_FOUND constant is defined in src/Appwrite/Extend/Exception.php and is already used by getNotFoundException() in Documents/Action.php for both collections and row-based APIs.
  • No other changes are required.

Comment thread tests/e2e/Services/Databases/Legacy/DatabasesBase.php
Comment thread tests/e2e/Services/Databases/Legacy/DatabasesBase.php Outdated
@github-actions

github-actions Bot commented Aug 24, 2025 •

Copy link
Copy Markdown

✨ Benchmark results

  • Requests per second: 1,256
  • Requests with 200 status code: 226,166
  • P99 latency: 0.15379648

⚡ Benchmark Comparison

Metric This PR Latest version
RPS 1,256 975
200 226,166 175,522
P99 0.15379648 0.20309653

@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: 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 MissingPayload

Allowing a single-item list passes a malformed structure downstream (mixing numeric index 0 with $id when 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 mutations

The 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 $permissions remain 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 empty

If 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 $upserted size 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.

📥 Commits

Reviewing files that changed from the base of the PR and between ba79dda and ad96379.

📒 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

@abnegate
abnegate merged commit d826e5c into 1.8.x Aug 25, 2025
@abnegate
abnegate deleted the upsert-documnet-no-change-1.8 branch August 25, 2025 04:03
@coderabbitai coderabbitai Bot mentioned this pull request Sep 26, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Oct 30, 2025
2 tasks
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