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:
commit
06096b4168
2 changed files with 87 additions and 2 deletions
|
@ -1318,8 +1318,11 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId')
|
||||||
catch (AuthorizationException $exception) {
|
catch (AuthorizationException $exception) {
|
||||||
throw new Exception('Unauthorized permissions', 401);
|
throw new Exception('Unauthorized permissions', 401);
|
||||||
}
|
}
|
||||||
|
catch (DuplicateException $exception) {
|
||||||
|
throw new Exception('Document already exists', 409);
|
||||||
|
}
|
||||||
catch (StructureException $exception) {
|
catch (StructureException $exception) {
|
||||||
throw new Exception('Bad structure. '.$exception->getMessage(), 400);
|
throw new Exception($exception->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$audits
|
$audits
|
||||||
|
@ -1380,4 +1383,4 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
|
||||||
;
|
;
|
||||||
|
|
||||||
$response->noContent();
|
$response->noContent();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1089,4 +1089,86 @@ trait DatabaseBase
|
||||||
|
|
||||||
return $data;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue