diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 1ff2e9b76e..3b3e477d90 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -15,6 +15,7 @@ use Utopia\Validator\JSON; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; +use Utopia\Database\Adapter\MariaDB; use Utopia\Database\Validator\Key; use Utopia\Database\Validator\Permissions; use Utopia\Database\Validator\QueryValidator; @@ -23,6 +24,7 @@ use Utopia\Database\Validator\Structure; use Utopia\Database\Validator\UID; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Duplicate as DuplicateException; +use Utopia\Database\Exception\Limit as LimitException; use Utopia\Database\Exception\Structure as StructureException; use Appwrite\Utopia\Response; use Appwrite\Database\Validator\CustomId; @@ -1116,6 +1118,16 @@ App::post('/v1/database/collections/:collectionId/indexes') throw new Exception('Collection not found', 404); } + $count = $dbForInternal->count('indexes', [ + new Query('collectionId', Query::TYPE_EQUAL, [$collectionId]) + ], 61); + + $limit = 64 - MariaDB::getNumberOfDefaultIndexes(); + + if ($count >= $limit) { + throw new Exception('Index limit exceeded', 400); + } + // Convert Document[] to array of attribute metadata $oldAttributes = \array_map(function ($a) { return $a->getArrayCopy(); @@ -1140,7 +1152,6 @@ App::post('/v1/database/collections/:collectionId/indexes') $lengths[$key] = ($attributeType === Database::VAR_STRING) ? $attributeSize : null; } - // TODO@kodumbeats should $lengths be a part of the response model? try { $index = $dbForInternal->createDocument('indexes', new Document([ '$id' => $collectionId.'_'.$indexId, @@ -1153,7 +1164,7 @@ App::post('/v1/database/collections/:collectionId/indexes') 'orders' => $orders, ])); } catch (DuplicateException $th) { - throw new Exception('Attribute already exists', 409); + throw new Exception('Index already exists', 409); } $dbForInternal->purgeDocument('collections', $collectionId); @@ -1571,8 +1582,11 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId') catch (AuthorizationException $exception) { throw new Exception('Unauthorized permissions', 401); } + catch (DuplicateException $exception) { + throw new Exception('Document already exists', 409); + } catch (StructureException $exception) { - throw new Exception('Bad structure. '.$exception->getMessage(), 400); + throw new Exception($exception->getMessage(), 400); } $usage @@ -1645,4 +1659,4 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId') ; $response->noContent(); - }); \ No newline at end of file + }); diff --git a/app/views/console/users/index.phtml b/app/views/console/users/index.phtml index ac094aeee4..9c25d3a92e 100644 --- a/app/views/console/users/index.phtml +++ b/app/views/console/users/index.phtml @@ -423,13 +423,13 @@ $auth = $this->getParam('auth', []); data-scope="console">