-
Notifications
You must be signed in to change notification settings - Fork 60
fix: query relations in ManyToOne deleteSetNull and guard empty collections #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8194,6 +8194,10 @@ private function deleteSetNull(Document $collection, Document $relatedCollection | |||||||||||||||||||||
| if ($side === Database::RELATION_SIDE_CHILD) { | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (empty($value)) { | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| foreach ($value as $relation) { | ||||||||||||||||||||||
| $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { | ||||||||||||||||||||||
| $this->skipRelationships(fn () => $this->updateDocument( | ||||||||||||||||||||||
|
|
@@ -8212,12 +8216,14 @@ private function deleteSetNull(Document $collection, Document $relatedCollection | |||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (!$twoWay) { | ||||||||||||||||||||||
| $value = $this->find($relatedCollection->getId(), [ | ||||||||||||||||||||||
| Query::select(['$id']), | ||||||||||||||||||||||
| Query::equal($twoWayKey, [$document->getId()]), | ||||||||||||||||||||||
| Query::limit(PHP_INT_MAX) | ||||||||||||||||||||||
| ]); | ||||||||||||||||||||||
| $value = $this->find($relatedCollection->getId(), [ | ||||||||||||||||||||||
| Query::select(['$id']), | ||||||||||||||||||||||
| Query::equal($twoWayKey, [$document->getId()]), | ||||||||||||||||||||||
| Query::limit(PHP_INT_MAX) | ||||||||||||||||||||||
| ]); | ||||||||||||||||||||||
|
Comment on lines
+8219
to
+8223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller may delete the child but cannot read the referencing collection, this new
Suggested change
Knowledge Base Used: Prompt To Fix With AIThis is a comment left during a code review.
Path: src/Database/Database.php
Line: 8219-8223
Comment:
**Lookup Uses Caller Authorization**
When a caller may delete the child but cannot read the referencing collection, this new `find()` runs with the caller's read authorization. It can either reject the permitted deletion or omit unreadable parent documents, leaving those parents pointing to the deleted child. This internal relationship lookup should bypass read authorization, as the analogous one-to-one set-null and many-to-one restrict paths do.
```suggestion
$value = $this->authorization->skip(fn () => $this->find($relatedCollection->getId(), [
Query::select(['$id']),
Query::equal($twoWayKey, [$document->getId()]),
Query::limit(PHP_INT_MAX)
]));
```
**Knowledge Base Used:**
- [Database orchestration](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/utopia-php/database/-/docs/database-orchestration.md)
- [Validation and authorization](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/utopia-php/database/-/docs/validation-and-authorization.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (empty($value)) { | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| foreach ($value as $relation) { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -782,6 +782,24 @@ public function testManyToOneTwoWayRelationship(): void | |
| $database->getDocument('product', 'product1'); | ||
| $this->assertEquals(null, $product1->getAttribute('newStore')); | ||
|
|
||
|
|
||
| // Create child with no related parents and verify deleteSetNull succeeds | ||
| $database->createDocument('store', new Document([ | ||
| '$id' => 'store8', | ||
| '$permissions' => [ | ||
| Permission::read(Role::any()), | ||
| Permission::update(Role::any()), | ||
| Permission::delete(Role::any()), | ||
| ], | ||
| 'name' => 'Store 8', | ||
| 'opensAt' => '10:00', | ||
| ])); | ||
|
|
||
| $deleted = $database->deleteDocument('store', 'store8'); | ||
| $this->assertEquals(true, $deleted); | ||
|
|
||
| $store8 = $database->getDocument('store', 'store8'); | ||
| $this->assertEquals(true, $store8->isEmpty()); | ||
|
Comment on lines
+786
to
+802
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The added regression checks only deletion with zero referencing parents, so it cannot verify the main changed behavior: finding existing two-way references and nulling their foreign keys. The nearby check does not provide reliable coverage because it discards the refetched Prompt To Fix With AIThis is a comment left during a code review.
Path: tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php
Line: 786-802
Comment:
**Nonempty Path Remains Untested**
The added regression checks only deletion with zero referencing parents, so it cannot verify the main changed behavior: finding existing two-way references and nulling their foreign keys. The nearby check does not provide reliable coverage because it discards the refetched `product1` and reads an older object whose missing `newStore` attribute already evaluates to `null`. Add a focused case that refetches a parent immediately after deleting its related child and verifies the stored relationship is null.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| // Change on delete to cascade | ||
| $database->updateRelationship( | ||
| collection: 'product', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Skip read authorization for the relationship cleanup lookup.
Line 8219 calls
find()with its default read permission. A caller can have delete permission for the child document without read permission for the related collection or parent documents. In that case, deletion can fail before cleanup, orfind()can omit unreadable parents and leave their$twoWayKeyunchanged. Wrap this lookup in$this->authorization->skip(...), as the subsequent cleanup updates already do.🤖 Prompt for AI Agents