1
0
Fork 0
mirror of synced 2024-09-28 15:31:43 +12:00

Address Comments

This commit is contained in:
Bradley Schofield 2024-08-30 19:18:45 +09:00
parent 38bc2bb925
commit 627cbe285f
3 changed files with 60 additions and 46 deletions

View file

@ -27,6 +27,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException; use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Restricted as RestrictedException; use Utopia\Database\Exception\Restricted as RestrictedException;
use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role; use Utopia\Database\Helpers\Role;
@ -364,14 +365,18 @@ function updateAttribute(
$dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId());
} }
} else { } else {
$dbForProject->updateAttribute( try {
collection: $collectionId, $dbForProject->updateAttribute(
id: $key, collection: $collectionId,
required: $required, id: $key,
default: $default, required: $required,
formatOptions: $options ?? null, default: $default,
size: $size ?? null, formatOptions: $options ?? null,
); size: $size ?? null,
);
} catch (TruncateException $e) {
throw new Exception(Exception::ATTRIBUTE_INVALID_RESIZE);
}
} }
$attribute = $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key, $attribute); $attribute = $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key, $attribute);
@ -1149,19 +1154,15 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string
$filters[] = 'encrypt'; $filters[] = 'encrypt';
} }
try { $attribute = createAttribute($databaseId, $collectionId, new Document([
$attribute = createAttribute($databaseId, $collectionId, new Document([ 'key' => $key,
'key' => $key, 'type' => Database::VAR_STRING,
'type' => Database::VAR_STRING, 'size' => $size,
'size' => $size, 'required' => $required,
'required' => $required, 'default' => $default,
'default' => $default, 'array' => $array,
'array' => $array, 'filters' => $filters,
'filters' => $filters, ]), $response, $dbForProject, $queueForDatabase, $queueForEvents);
]), $response, $dbForProject, $queueForDatabase, $queueForEvents);
} catch (DatabaseException $e) {
var_dump($e);
}
$response $response
@ -1877,25 +1878,17 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/strin
->inject('queueForEvents') ->inject('queueForEvents')
->action(function (string $databaseId, string $collectionId, string $key, ?bool $required, ?string $default, ?int $size, Response $response, Database $dbForProject, Event $queueForEvents) { ->action(function (string $databaseId, string $collectionId, string $key, ?bool $required, ?string $default, ?int $size, Response $response, Database $dbForProject, Event $queueForEvents) {
try { $attribute = updateAttribute(
$attribute = updateAttribute( databaseId: $databaseId,
databaseId: $databaseId, collectionId: $collectionId,
collectionId: $collectionId, key: $key,
key: $key, dbForProject: $dbForProject,
dbForProject: $dbForProject, queueForEvents: $queueForEvents,
queueForEvents: $queueForEvents, type: Database::VAR_STRING,
type: Database::VAR_STRING, default: $default,
default: $default, required: $required,
required: $required, size: $size
size: $size );
);
} catch (DatabaseException $e) {
if ($e->getMessage() === "Resize would result in data truncation") {
throw new Exception(Exception::ATTRIBUTE_INVALID_RESIZE);
} else {
throw $e;
}
}
$response $response
->setStatusCode(Response::STATUS_CODE_OK) ->setStatusCode(Response::STATUS_CODE_OK)

12
composer.lock generated
View file

@ -1723,16 +1723,16 @@
}, },
{ {
"name": "utopia-php/database", "name": "utopia-php/database",
"version": "0.53.1", "version": "0.53.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/database.git", "url": "https://github.com/utopia-php/database.git",
"reference": "3f17072a33649a3451122bfeb630d57330c0a6df" "reference": "87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/3f17072a33649a3451122bfeb630d57330c0a6df", "url": "https://api.github.com/repos/utopia-php/database/zipball/87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78",
"reference": "3f17072a33649a3451122bfeb630d57330c0a6df", "reference": "87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1773,9 +1773,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/database/issues", "issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/0.53.1" "source": "https://github.com/utopia-php/database/tree/0.53.2"
}, },
"time": "2024-08-30T09:02:45+00:00" "time": "2024-08-30T10:14:51+00:00"
}, },
{ {
"name": "utopia-php/domains", "name": "utopia-php/domains",

View file

@ -3402,6 +3402,27 @@ class DatabasesCustomServerTest extends Scope
$this->assertEquals(200, $document['headers']['status-code']); $this->assertEquals(200, $document['headers']['status-code']);
$this->assertEquals(10, strlen($document['body']['string'])); $this->assertEquals(10, strlen($document['body']['string']));
// Try create document with string that is too large
$newDoc = $this->client->call(
Client::METHOD_POST,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/documents',
array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]),
[
'documentId' => 'unique()',
'data' => [
'string' => str_repeat('a', 11)
],
"permissions" => ["read(\"any\")"]
]
);
$this->assertEquals(400, $newDoc['headers']['status-code']);
$this->assertEquals(AppwriteException::DOCUMENT_INVALID_STRUCTURE, $newDoc['body']['type']);
} }
/** /**