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

Merge remote-tracking branch 'origin/1.6.1' into feat-renaming-attributes

# Conflicts:
#	app/config/specs/open-api3-latest-console.json
#	app/config/specs/open-api3-latest-server.json
#	app/config/specs/swagger2-latest-console.json
#	app/config/specs/swagger2-latest-server.json
#	app/controllers/api/databases.php
This commit is contained in:
Jake Barnby 2024-09-05 15:31:04 +12:00
commit 24746de9ba
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
12 changed files with 220 additions and 20 deletions

View file

@ -699,6 +699,11 @@ return [
'description' => 'The relationship value is invalid.',
'code' => 400,
],
Exception::ATTRIBUTE_INVALID_RESIZE => [
'name' => Exception::ATTRIBUTE_INVALID_RESIZE,
'description' => "Existing data is too large for new size, truncate your existing data then try again.",
'code' => 400,
],
/** Indexes */
Exception::INDEX_NOT_FOUND => [

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -26,6 +26,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Restricted as RestrictedException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
@ -235,6 +236,7 @@ function updateAttribute(
int|float $max = null,
array $elements = null,
array $options = [],
int $size = null
string $newKey = null
): Document {
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
@ -281,6 +283,10 @@ function updateAttribute(
->setAttribute('default', $default)
->setAttribute('required', $required);
if (!empty($size)) {
$attribute->setAttribute('size', $size);
}
$formatOptions = $attribute->getAttribute('formatOptions');
switch ($attribute->getAttribute('format')) {
@ -366,14 +372,19 @@ function updateAttribute(
$dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId());
}
} else {
$dbForProject->updateAttribute(
collection: $collectionId,
id: $key,
required: $required,
default: $default,
formatOptions: $options ?? null,
newKey: $newKey,
);
try {
$dbForProject->updateAttribute(
collection: $collectionId,
id: $key,
required: $required,
default: $default,
formatOptions: $options ?? null,
size: $size ?? null,
newKey: $newKey ?? null
);
} catch (TruncateException $e) {
throw new Exception(Exception::ATTRIBUTE_INVALID_RESIZE);
}
}
if (!empty($newKey) && $key !== $newKey) {
@ -1170,6 +1181,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string
'filters' => $filters,
]), $response, $dbForProject, $queueForDatabase, $queueForEvents);
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($attribute, Response::MODEL_ATTRIBUTE_STRING);
@ -1878,10 +1890,8 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/strin
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Nullable(new Text(0, 0)), 'Default value for attribute when not provided. Cannot be set when attribute is required.')
->param('newKey', null, new Key(), 'New attribute key.', true)
->inject('response')
->inject('dbForProject')
->inject('queueForEvents')
->action(function (string $databaseId, string $collectionId, string $key, ?bool $required, ?string $default, ?string $newKey, Response $response, Database $dbForProject, Event $queueForEvents) {
->param('size', null, new Integer(), 'Maximum size of the string attribute.', true)
->action(function (string $databaseId, string $collectionId, string $key, ?bool $required, ?string $default, ?string $newKey, ?int $size, Response $response, Database $dbForProject, Event $queueForEvents) {
$attribute = updateAttribute(
databaseId: $databaseId,
@ -1892,6 +1902,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/strin
type: Database::VAR_STRING,
default: $default,
required: $required,
size: $size
newKey: $newKey
);

View file

@ -200,6 +200,7 @@ class Exception extends \Exception
public const ATTRIBUTE_LIMIT_EXCEEDED = 'attribute_limit_exceeded';
public const ATTRIBUTE_VALUE_INVALID = 'attribute_value_invalid';
public const ATTRIBUTE_TYPE_INVALID = 'attribute_type_invalid';
public const ATTRIBUTE_INVALID_RESIZE = 'attribute_invalid_resize';
/** Relationship */
public const RELATIONSHIP_VALUE_INVALID = 'relationship_value_invalid';

View file

@ -3241,6 +3241,189 @@ class DatabasesCustomServerTest extends Scope
$this->assertEquals(AppwriteException::ATTRIBUTE_DEFAULT_UNSUPPORTED, $update['body']['type']);
}
/**
* @depends testAttributeUpdate
*/
public function testAttributeUpdateStringResize(array $data)
{
$key = 'string';
$databaseId = $data['databaseId'];
$collectionId = $data['collectionId'];
$document = $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' => 'string'
],
"permissions" => ["read(\"any\")"]
]
);
// Test Resize Up
$attribute = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string/' . $key, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'size' => 2048,
'default' => '',
'required' => false
]);
$this->assertEquals(200, $attribute['headers']['status-code']);
$this->assertEquals(2048, $attribute['body']['size']);
// Test create new document with new size
$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', 2048)
],
"permissions" => ["read(\"any\")"]
]
);
$this->assertEquals(201, $newDoc['headers']['status-code']);
$this->assertEquals(2048, strlen($newDoc['body']['string']));
// Test update document with new size
$document = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'data' => [
'string' => str_repeat('a', 2048)
]
]);
$this->assertEquals(200, $document['headers']['status-code']);
$this->assertEquals(2048, strlen($document['body']['string']));
// Test Exception on resize down with data that is too large
$attribute = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string/' . $key, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'size' => 10,
'default' => '',
'required' => false
]);
$this->assertEquals(400, $attribute['headers']['status-code']);
$this->assertEquals(AppwriteException::ATTRIBUTE_INVALID_RESIZE, $attribute['body']['type']);
// original documents to original size, remove new document
$document = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'data' => [
'string' => 'string'
]
]);
$this->assertEquals(200, $document['headers']['status-code']);
$this->assertEquals('string', $document['body']['string']);
$deleteDoc = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $newDoc['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
$this->assertEquals(204, $deleteDoc['headers']['status-code']);
// Test Resize Down
$attribute = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string/' . $key, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'size' => 10,
'default' => '',
'required' => false
]);
$this->assertEquals(200, $attribute['headers']['status-code']);
$this->assertEquals(10, $attribute['body']['size']);
// Test create new document with new size
$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', 10)
],
"permissions" => ["read(\"any\")"]
]
);
$this->assertEquals(201, $newDoc['headers']['status-code']);
$this->assertEquals(10, strlen($newDoc['body']['string']));
// Test update document with new size
$document = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $document['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'data' => [
'string' => str_repeat('a', 10)
]
]);
$this->assertEquals(200, $document['headers']['status-code']);
$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']);
}
/**
* @depends testAttributeUpdate
*/