mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Fix unique constraint duplicate errors (#13187)
This commit is contained in:
1 parent
ba6835cbe4
commit
a6d21fcda6
8 files changed
+63
-15
No files matched your search
@@ -839,6 +839,11 @@ return [
|
||||
'description' => 'Document with the requested ID \'%s\' already exists. Try again with a different ID or use ID.unique() to generate a unique ID.',
|
||||
'code' => 409,
|
||||
],
|
||||
Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION => [
|
||||
'name' => Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION,
|
||||
'description' => 'Document violates a unique attribute constraint. Try again with different value(s).',
|
||||
'code' => 409,
|
||||
],
|
||||
Exception::DOCUMENT_UPDATE_CONFLICT => [
|
||||
'name' => Exception::DOCUMENT_UPDATE_CONFLICT,
|
||||
'description' => 'Remote document is newer than local.',
|
||||
@@ -876,6 +881,11 @@ return [
|
||||
'description' => 'Row with the requested ID \'%s\' already exists. Try again with a different ID or use ID.unique() to generate a unique ID.',
|
||||
'code' => 409,
|
||||
],
|
||||
Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION => [
|
||||
'name' => Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION,
|
||||
'description' => 'Row violates a unique column constraint. Try again with different value(s).',
|
||||
'code' => 409,
|
||||
],
|
||||
Exception::ROW_UPDATE_CONFLICT => [
|
||||
'name' => Exception::ROW_UPDATE_CONFLICT,
|
||||
'description' => 'Remote row is newer than local.',
|
||||
|
||||
@@ -232,6 +232,7 @@ class Exception extends \Exception
|
||||
public const string DOCUMENT_MISSING_DATA = 'document_missing_data';
|
||||
public const string DOCUMENT_MISSING_PAYLOAD = 'document_missing_payload';
|
||||
public const string DOCUMENT_ALREADY_EXISTS = 'document_already_exists';
|
||||
public const string DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION = 'document_unique_constraint_violation';
|
||||
public const string DOCUMENT_UPDATE_CONFLICT = 'document_update_conflict';
|
||||
public const string DOCUMENT_DELETE_RESTRICTED = 'document_delete_restricted';
|
||||
|
||||
@@ -241,6 +242,7 @@ class Exception extends \Exception
|
||||
public const string ROW_MISSING_DATA = 'row_missing_data';
|
||||
public const string ROW_MISSING_PAYLOAD = 'row_missing_payload';
|
||||
public const string ROW_ALREADY_EXISTS = 'row_already_exists';
|
||||
public const string ROW_UNIQUE_CONSTRAINT_VIOLATION = 'row_unique_constraint_violation';
|
||||
public const string ROW_UPDATE_CONFLICT = 'row_update_conflict';
|
||||
public const string ROW_DELETE_RESTRICTED = 'row_delete_restricted';
|
||||
|
||||
|
||||
+10
@@ -164,6 +164,16 @@ abstract class Action extends DatabasesAction
|
||||
: Exception::ROW_ALREADY_EXISTS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate unique constraint exception.
|
||||
*/
|
||||
protected function getUniqueConstraintException(): string
|
||||
{
|
||||
return $this->isCollectionsAPI()
|
||||
? Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION
|
||||
: Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate conflict exception.
|
||||
*/
|
||||
|
||||
+8
-4
@@ -20,6 +20,7 @@ use Utopia\Database\Exception\Conflict as ConflictException;
|
||||
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
||||
use Utopia\Database\Exception\Relationship as RelationshipException;
|
||||
use Utopia\Database\Exception\Structure as StructureException;
|
||||
use Utopia\Database\Exception\Unique as UniqueException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
|
||||
@@ -170,12 +171,13 @@ class Upsert extends Action
|
||||
}
|
||||
|
||||
$dbForDatabases = $getDatabasesDB($database);
|
||||
$collectionTableId = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence();
|
||||
$upserted = [];
|
||||
|
||||
try {
|
||||
$modified = $dbForDatabases->withPreserveDates(function () use ($dbForDatabases, $database, $collection, $documents, $plan, &$upserted) {
|
||||
$modified = $dbForDatabases->withPreserveDates(function () use ($dbForDatabases, $collectionTableId, $documents, $plan, &$upserted) {
|
||||
return $dbForDatabases->upsertDocuments(
|
||||
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
|
||||
$collectionTableId,
|
||||
$documents,
|
||||
onNext: function (Document $document) use ($plan, &$upserted) {
|
||||
if (\count($upserted) < ($plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH)) {
|
||||
@@ -186,8 +188,10 @@ class Upsert extends Action
|
||||
});
|
||||
} catch (ConflictException) {
|
||||
throw new Exception($this->getConflictException());
|
||||
} catch (DuplicateException) {
|
||||
throw new Exception($this->getDuplicateException(), params: ['multiple']);
|
||||
} catch (UniqueException $e) {
|
||||
throw new Exception($this->getUniqueConstraintException(), previous: $e);
|
||||
} catch (DuplicateException $e) {
|
||||
throw new Exception($this->getDuplicateException(), previous: $e, params: ['multiple']);
|
||||
} catch (RelationshipException $e) {
|
||||
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage());
|
||||
} catch (StructureException $e) {
|
||||
|
||||
+8
-4
@@ -22,6 +22,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
|
||||
use Utopia\Database\Exception\NotFound as NotFoundException;
|
||||
use Utopia\Database\Exception\Relationship as RelationshipException;
|
||||
use Utopia\Database\Exception\Structure as StructureException;
|
||||
use Utopia\Database\Exception\Unique as UniqueException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Helpers\Permission;
|
||||
use Utopia\Database\Helpers\Role;
|
||||
@@ -463,12 +464,13 @@ class Create extends Action
|
||||
}
|
||||
|
||||
$dbForDatabases = $getDatabasesDB($database);
|
||||
$collectionTableId = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence();
|
||||
try {
|
||||
$created = [];
|
||||
$dbForDatabases->withPreserveDates(
|
||||
function () use (&$created, $dbForDatabases, $database, $collection, $documents) {
|
||||
function () use (&$created, $dbForDatabases, $collectionTableId, $documents) {
|
||||
$dbForDatabases->createDocuments(
|
||||
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
|
||||
$collectionTableId,
|
||||
$documents,
|
||||
onNext: function ($doc) use (&$created) {
|
||||
$created[] = $doc;
|
||||
@@ -476,8 +478,10 @@ class Create extends Action
|
||||
);
|
||||
}
|
||||
);
|
||||
} catch (DuplicateException) {
|
||||
throw new Exception($this->getDuplicateException(), params: [$documentId]);
|
||||
} catch (UniqueException $e) {
|
||||
throw new Exception($this->getUniqueConstraintException(), previous: $e);
|
||||
} catch (DuplicateException $e) {
|
||||
throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]);
|
||||
} catch (NotFoundException) {
|
||||
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
|
||||
} catch (RelationshipException $e) {
|
||||
|
||||
+6
-3
@@ -19,6 +19,7 @@ use Utopia\Database\Exception\Conflict as ConflictException;
|
||||
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
||||
use Utopia\Database\Exception\Relationship as RelationshipException;
|
||||
use Utopia\Database\Exception\Structure as StructureException;
|
||||
use Utopia\Database\Exception\Unique as UniqueException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Helpers\Permission;
|
||||
use Utopia\Database\Helpers\Role;
|
||||
@@ -326,15 +327,17 @@ class Update extends Action
|
||||
$document = $dbForDatabases->withRequestTimestamp(
|
||||
$requestTimestamp,
|
||||
fn () => $dbForDatabases->withPreserveDates(fn () => $dbForDatabases->updateDocument(
|
||||
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
|
||||
$collectionTableId,
|
||||
$document->getId(),
|
||||
$newDocument
|
||||
))
|
||||
);
|
||||
} catch (ConflictException) {
|
||||
throw new Exception($this->getConflictException());
|
||||
} catch (DuplicateException) {
|
||||
throw new Exception($this->getDuplicateException(), params: [$documentId]);
|
||||
} catch (UniqueException $e) {
|
||||
throw new Exception($this->getUniqueConstraintException(), previous: $e);
|
||||
} catch (DuplicateException $e) {
|
||||
throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]);
|
||||
} catch (RelationshipException $e) {
|
||||
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage());
|
||||
} catch (StructureException $e) {
|
||||
|
||||
+7
-4
@@ -20,6 +20,7 @@ use Utopia\Database\Exception\Conflict as ConflictException;
|
||||
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
||||
use Utopia\Database\Exception\Relationship as RelationshipException;
|
||||
use Utopia\Database\Exception\Structure as StructureException;
|
||||
use Utopia\Database\Exception\Unique as UniqueException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Helpers\Permission;
|
||||
use Utopia\Database\Helpers\Role;
|
||||
@@ -329,9 +330,9 @@ class Upsert extends Action
|
||||
|
||||
$upserted = [];
|
||||
try {
|
||||
$dbForDatabases->withPreserveDates(function () use (&$upserted, $dbForDatabases, $database, $collection, $newDocument) {
|
||||
$dbForDatabases->withPreserveDates(function () use (&$upserted, $dbForDatabases, $collectionTableId, $newDocument) {
|
||||
return $dbForDatabases->upsertDocuments(
|
||||
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
|
||||
$collectionTableId,
|
||||
[$newDocument],
|
||||
onNext: function (Document $document) use (&$upserted) {
|
||||
$upserted[] = $document;
|
||||
@@ -340,8 +341,10 @@ class Upsert extends Action
|
||||
});
|
||||
} catch (ConflictException) {
|
||||
throw new Exception($this->getConflictException());
|
||||
} catch (DuplicateException) {
|
||||
throw new Exception($this->getDuplicateException(), params: [$documentId]);
|
||||
} catch (UniqueException $e) {
|
||||
throw new Exception($this->getUniqueConstraintException(), previous: $e);
|
||||
} catch (DuplicateException $e) {
|
||||
throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]);
|
||||
} catch (RelationshipException $e) {
|
||||
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage());
|
||||
} catch (StructureException $e) {
|
||||
|
||||
@@ -6262,6 +6262,8 @@ trait DatabasesBase
|
||||
]);
|
||||
|
||||
$this->assertEquals(409, $duplicate['headers']['status-code']);
|
||||
$this->assertEquals($this->getUniqueConstraintException(), $duplicate['body']['type']);
|
||||
$this->assertStringNotContainsString('requested ID', $duplicate['body']['message']);
|
||||
|
||||
// Test for exception when inserting new doc and then updating to conflict
|
||||
$document = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $collectionId), array_merge([
|
||||
@@ -6298,6 +6300,16 @@ trait DatabasesBase
|
||||
]);
|
||||
|
||||
$this->assertEquals(409, $duplicate['headers']['status-code']);
|
||||
$this->assertEquals($this->getUniqueConstraintException(), $duplicate['body']['type']);
|
||||
$this->assertStringNotContainsString('requested ID', $duplicate['body']['message']);
|
||||
|
||||
}
|
||||
|
||||
private function getUniqueConstraintException(): string
|
||||
{
|
||||
return $this->getRecordResource() === 'rows'
|
||||
? Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION
|
||||
: Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION;
|
||||
}
|
||||
|
||||
public function testPersistentCreatedAt(): void
|
||||
|
||||
Reference in new issue
Block a user