From f24cf729c3b37dc6e2241a99bd9545ff13713ef4 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Fri, 7 Jul 2023 23:50:06 +0530 Subject: [PATCH 1/4] fixed relationship update bug in two way type --- app/controllers/api/databases.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 418c36cc86..4459322baf 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -327,22 +327,22 @@ function updateAttribute( } if ($type === Database::VAR_RELATIONSHIP) { - $options = \array_merge($attribute->getAttribute('options', []), $options); - $attribute->setAttribute('options', $options); + $primaryDocumentOptions = \array_merge($attribute->getAttribute('options', []), $options); + $attribute->setAttribute('options', $primaryDocumentOptions); $dbForProject->updateRelationship( collection: $collectionId, id: $key, - onDelete: $options['onDelete'], + onDelete: $primaryDocumentOptions['onDelete'], ); - if ($options['twoWay']) { - $relatedCollection = $dbForProject->getDocument('database_' . $db->getInternalId(), $options['relatedCollection']); - $relatedAttribute = $dbForProject->getDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']); + if ($primaryDocumentOptions['twoWay']) { + $relatedCollection = $dbForProject->getDocument('database_' . $db->getInternalId(), $primaryDocumentOptions['relatedCollection']); + + $relatedAttribute = $dbForProject->getDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $primaryDocumentOptions['twoWayKey']); $relatedOptions = \array_merge($relatedAttribute->getAttribute('options'), $options); $relatedAttribute->setAttribute('options', $relatedOptions); - - $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey'], $relatedAttribute); + $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $primaryDocumentOptions['twoWayKey'], $relatedAttribute); $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); } } else { From 4542ec34aec1b3b2eac22093464252fd7982c40e Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Sat, 8 Jul 2023 01:28:24 +0530 Subject: [PATCH 2/4] added test case to verify update of two way relationship --- .../Databases/DatabasesCustomClientTest.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php index de59723e53..8f5e5161d4 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php @@ -225,4 +225,95 @@ class DatabasesCustomClientTest extends Scope return []; } + + public function testUpdateTwoWayRelationship(): void + { + + $database = $this->client->call(Client::METHOD_POST, '/databases', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'databaseId' => ID::unique(), + 'name' => 'Test Database' + ]); + + $databaseId = $database['body']['$id']; + + + //creating collection 1 + $collection1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'collectionId' => ID::unique(), + 'name' => 'level1', + 'documentSecurity' => false, + 'permissions' => [ + Permission::create(Role::user($this->getUser()['$id'])), + Permission::read(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::delete(Role::user($this->getUser()['$id'])), + ] + ]); + + //creating collection 2 + $collection2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'collectionId' => ID::unique(), + 'name' => 'level2', + 'documentSecurity' => false, + 'permissions' => [ + Permission::create(Role::user($this->getUser()['$id'])), + Permission::read(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::delete(Role::user($this->getUser()['$id'])), + ] + ]); + + \sleep(2); + + //creating two way relationship between collection 1 and collection 2 from collection 1 + $relation = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collection1['body']['$id'] . '/attributes/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'relatedCollectionId' => $collection2['body']['$id'], + 'type' => 'oneToMany', + 'twoWay' => true, + 'onDelete' => 'cascade', + 'key' => $collection2['body']['$id'], + 'twoWayKey' => $collection1['body']['$id'] + ]); + + \sleep(3); + + //update relation from collection 2 to on delete restrict + $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collection2['body']['$id'] . '/attributes/' . $collection1['body']['$id'] . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'onDelete' => 'restrict', + ]); + + //fetching attributes after updating relation to compare + $collection1Attributes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collection1['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]); + + $collection1RelationAttribute = $collection1Attributes['body']['attributes'][0]; + + $this->assertEquals($relation['body']['side'], $collection1RelationAttribute['side']); + $this->assertEquals($relation['body']['twoWayKey'], $collection1RelationAttribute['twoWayKey']); + $this->assertEquals($relation['body']['relatedCollection'], $collection1RelationAttribute['relatedCollection']); + $this->assertEquals('restrict', $collection1RelationAttribute['onDelete']); + } } From a98248478f72515ba535ddc392913964aa168e3c Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Sat, 8 Jul 2023 01:31:20 +0530 Subject: [PATCH 3/4] lint issues --- tests/e2e/Services/Databases/DatabasesCustomClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php index 8f5e5161d4..9b0c337bc7 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php @@ -302,7 +302,7 @@ class DatabasesCustomClientTest extends Scope 'onDelete' => 'restrict', ]); - //fetching attributes after updating relation to compare + //fetching attributes after updating relation to compare $collection1Attributes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collection1['body']['$id'], [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], From e1ba324196e0efa8b2fa1e9e674a9dd0a933f1dd Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Mon, 10 Jul 2023 13:34:32 +0530 Subject: [PATCH 4/4] fix formatting issues in comments according to feedback in PR. --- .../Services/Databases/DatabasesCustomClientTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php index 9b0c337bc7..b0ab884a07 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php @@ -241,7 +241,7 @@ class DatabasesCustomClientTest extends Scope $databaseId = $database['body']['$id']; - //creating collection 1 + // Creating collection 1 $collection1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -258,7 +258,7 @@ class DatabasesCustomClientTest extends Scope ] ]); - //creating collection 2 + // Creating collection 2 $collection2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -277,7 +277,7 @@ class DatabasesCustomClientTest extends Scope \sleep(2); - //creating two way relationship between collection 1 and collection 2 from collection 1 + // Creating two way relationship between collection 1 and collection 2 from collection 1 $relation = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collection1['body']['$id'] . '/attributes/relationship', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -293,7 +293,7 @@ class DatabasesCustomClientTest extends Scope \sleep(3); - //update relation from collection 2 to on delete restrict + // Update relation from collection 2 to on delete restrict $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collection2['body']['$id'] . '/attributes/' . $collection1['body']['$id'] . '/relationship', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -302,7 +302,7 @@ class DatabasesCustomClientTest extends Scope 'onDelete' => 'restrict', ]); - //fetching attributes after updating relation to compare + // Fetching attributes after updating relation to compare $collection1Attributes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collection1['body']['$id'], [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],