mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
updated tests and validations
This commit is contained in:
@@ -2812,13 +2812,13 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes')
|
||||
->param('key', null, new Key(), 'Index Key.')
|
||||
->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE]), 'Index type.')
|
||||
->param('attributes', null, new ArrayList(new Key(true), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of attributes to index. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' attributes are allowed, each 32 characters long.')
|
||||
->param('lengths', [], new ArrayList(new Integer(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Length of index. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE, optional:true)
|
||||
->param('orders', [], new ArrayList(new WhiteList(['ASC', 'DESC'], false, Database::VAR_STRING), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of index orders. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' orders are allowed.', true)
|
||||
->param('lengths', [], new ArrayList(new Nullable(new Integer()), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Length of index. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE, optional:true)
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->inject('queueForDatabase')
|
||||
->inject('queueForEvents')
|
||||
->action(function (string $databaseId, string $collectionId, string $key, string $type, array $attributes, array $lengths, array $orders, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents) {
|
||||
->action(function (string $databaseId, string $collectionId, string $key, string $type, array $attributes, array $orders, array $lengths, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents) {
|
||||
|
||||
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
|
||||
@@ -2832,6 +2832,10 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes')
|
||||
throw new Exception(Exception::COLLECTION_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (count($lengths) > count($attributes)) {
|
||||
throw new Exception(Exception::INDEX_LENGTHS_INVALID);
|
||||
}
|
||||
|
||||
$count = $dbForProject->count('indexes', [
|
||||
Query::equal('collectionInternalId', [$collection->getInternalId()]),
|
||||
Query::equal('databaseInternalId', [$db->getInternalId()])
|
||||
@@ -2878,6 +2882,11 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes')
|
||||
'size' => 0
|
||||
];
|
||||
|
||||
$totalIndexLength = array_sum($lengths);
|
||||
if ($totalIndexLength > 768) {
|
||||
throw new Exception(Exception::INDEX_LIMIT_EXCEEDED, 'Index total length crossing 768');
|
||||
}
|
||||
|
||||
foreach ($attributes as $i => $attribute) {
|
||||
// find attribute metadata in collection document
|
||||
$attributeIndex = \array_search($attribute, array_column($oldAttributes, 'key'));
|
||||
@@ -2899,7 +2908,11 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes')
|
||||
throw new Exception(Exception::ATTRIBUTE_NOT_AVAILABLE, 'Attribute not available: ' . $oldAttributes[$attributeIndex]['key']);
|
||||
}
|
||||
|
||||
$lengths[$i] = array_key_exists($i, $lengths) ? $lengths[$i] : null;
|
||||
if ($lengths[$i] < 0) {
|
||||
throw new Exception(Exception::INDEX_INVALID, 'Negative index provided for ' . $oldAttributes[$attributeIndex]['key']);
|
||||
}
|
||||
|
||||
$lengths[$i] ??= null;
|
||||
if ($attributeArray === true) {
|
||||
if ($lengths[$i] === null) {
|
||||
$lengths[$i] = Database::ARRAY_INDEX_LENGTH;
|
||||
|
||||
Reference in New Issue
Block a user