From 50a3493d6901f37fa822dcec7b9fb4d2a49aee36 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 2 Jul 2021 18:23:58 -0400 Subject: [PATCH] Use attribute size as index length --- app/controllers/api/database.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 01336c1009..f4dd544be0 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -476,9 +476,31 @@ App::post('/v1/database/collections/:collectionId/indexes') throw new Exception('Collection not found', 404); } + if (\count($attributes) !== \count($orders)) { + throw new Exception('Must have one order per attribute', 400); + } + + $oldAttributes = $collection->getAttribute('attributes'); + // lengths hidden by default $lengths = []; + // set attribute size as length for strings, null otherwise + foreach ($attributes as $key => &$attribute) { + // find attribute metadata in collection document + $attributeIndex = \array_search($attribute, array_column($oldAttributes, '$id')); + + if ($attributeIndex === false) { + throw new Exception('Unknown attribute: ' . $attribute, 400); + } + + $type = $oldAttributes[$attributeIndex]['type']; + $size = $oldAttributes[$attributeIndex]['size']; + + // Only set length for indexes on strings + $length[$key] = ($type === Database::VAR_STRING) ? $size : null; + } + $success = $dbForExternal->addIndexInQueue($collectionId, $id, $type, $attributes, $lengths, $orders); // Database->createIndex() does not return a document