1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00

Merge pull request #1498 from appwrite/fix-catch-duplicate-index-exception

fix(refactor-db): catch duplicate exception when creating and updating documents
This commit is contained in:
Eldad A. Fux 2021-08-28 07:29:46 +03:00 committed by GitHub
commit 06096b4168
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 2 deletions

View file

@ -1318,8 +1318,11 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId')
catch (AuthorizationException $exception) {
throw new Exception('Unauthorized permissions', 401);
}
catch (DuplicateException $exception) {
throw new Exception('Document already exists', 409);
}
catch (StructureException $exception) {
throw new Exception('Bad structure. '.$exception->getMessage(), 400);
throw new Exception($exception->getMessage(), 400);
}
$audits
@ -1380,4 +1383,4 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
;
$response->noContent();
});
});

View file

@ -1089,4 +1089,86 @@ trait DatabaseBase
return $data;
}
/**
* @depends testDefaultPermissions
*/
public function testUniqueIndexDuplicate(array $data): array
{
$uniqueIndex = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'indexId' => 'unique_title',
'type' => 'unique',
'attributes' => ['title'],
]);
$this->assertEquals($uniqueIndex['headers']['status-code'], 201);
sleep(2);
// test for failure
$duplicate = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => 'unique()',
'data' => [
'title' => 'Captain America',
'releaseYear' => 1944,
'actors' => [
'Chris Evans',
'Samuel Jackson',
]
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(409, $duplicate['headers']['status-code']);
// Test for exception when updating document to conflict
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => 'unique()',
'data' => [
'title' => 'Captain America 5',
'releaseYear' => 1944,
'actors' => [
'Chris Evans',
'Samuel Jackson',
]
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(201, $document['headers']['status-code']);
// Test for exception when updating document to conflict
$duplicate = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['moviesId'] . '/documents/' . $document['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => 'unique()',
'data' => [
'title' => 'Captain America',
'releaseYear' => 1944,
'actors' => [
'Chris Evans',
'Samuel Jackson',
]
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(409, $duplicate['headers']['status-code']);
return $data;
}
}