From 6b77fcf21f775c28ab328b43650300c3cfeccc9a Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Wed, 12 Jan 2022 20:51:13 +0100 Subject: [PATCH 1/3] FIxed 409 for createCollection --- app/controllers/api/database.php | 4 ++-- .../Database/DatabaseCustomServerTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index c6fb149700..00e951030f 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -166,8 +166,6 @@ App::post('/v1/database/collections') $collectionId = $collectionId == 'unique()' ? $dbForProject->getId() : $collectionId; try { - $dbForProject->createCollection('collection_' . $collectionId); - $collection = $dbForProject->createDocument('collections', new Document([ '$id' => $collectionId, '$read' => $read ?? [], // Collection permissions for collection documents (based on permission model) @@ -183,6 +181,8 @@ App::post('/v1/database/collections') throw new Exception('Collection already exists', 409); } + $dbForProject->createCollection('collection_' . $collectionId); + $audits ->setParam('event', 'database.collections.create') ->setParam('resource', 'collection/'.$collectionId) diff --git a/tests/e2e/Services/Database/DatabaseCustomServerTest.php b/tests/e2e/Services/Database/DatabaseCustomServerTest.php index 659839debe..cd14974c6c 100644 --- a/tests/e2e/Services/Database/DatabaseCustomServerTest.php +++ b/tests/e2e/Services/Database/DatabaseCustomServerTest.php @@ -7,6 +7,7 @@ use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; use Tests\E2E\Client; use Utopia\Database\Database; +use function array_merge; class DatabaseCustomServerTest extends Scope { @@ -136,6 +137,21 @@ class DatabaseCustomServerTest extends Scope ]); $this->assertEquals($response['headers']['status-code'], 400); + + // This collection already exists + $response = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'name' => 'Test 1', + 'collectionId' => 'first', + 'read' => ['role:all'], + 'write' => ['role:all'], + 'permission' => 'document' + ]); + + $this->assertEquals($response['headers']['status-code'], 409); } public function testDeleteAttribute(): array From 2f75f9d13de1f056d28996c4921f1dc5e8d7c9fd Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Wed, 12 Jan 2022 20:54:19 +0100 Subject: [PATCH 2/3] Removed leftover --- tests/e2e/Services/Database/DatabaseCustomServerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/e2e/Services/Database/DatabaseCustomServerTest.php b/tests/e2e/Services/Database/DatabaseCustomServerTest.php index cd14974c6c..117ca960d4 100644 --- a/tests/e2e/Services/Database/DatabaseCustomServerTest.php +++ b/tests/e2e/Services/Database/DatabaseCustomServerTest.php @@ -7,7 +7,6 @@ use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; use Tests\E2E\Client; use Utopia\Database\Database; -use function array_merge; class DatabaseCustomServerTest extends Scope { From 85630df661a0c5b28046dc1775c999c7391f7b1a Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Mon, 24 Jan 2022 09:20:02 +0100 Subject: [PATCH 3/3] PR review changes --- app/controllers/api/database.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 00e951030f..a1fc91207d 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -177,12 +177,14 @@ App::post('/v1/database/collections') 'name' => $name, 'search' => implode(' ', [$collectionId, $name]), ])); + + $dbForProject->createCollection('collection_' . $collectionId); } catch (DuplicateException $th) { throw new Exception('Collection already exists', 409); + } catch (LimitException $th) { + throw new Exception('Collection limit exceeded', 400); } - $dbForProject->createCollection('collection_' . $collectionId); - $audits ->setParam('event', 'database.collections.create') ->setParam('resource', 'collection/'.$collectionId)