diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 69029730a..2c1337c77 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -196,7 +196,7 @@ public function checkIndexLength(Document $index): bool switch ($attribute->getAttribute('type')) { case Database::VAR_STRING: $attributeSize = $attribute->getAttribute('size', 0); - $indexLength = $lengths[$attributePosition] ?? $attributeSize; + $indexLength = !empty($lengths[$attributePosition]) ? $lengths[$attributePosition] : $attributeSize; break; case Database::VAR_FLOAT: $attributeSize = 2; // 8 bytes / 4 mb4 diff --git a/tests/e2e/Adapter/Scopes/IndexTests.php b/tests/e2e/Adapter/Scopes/IndexTests.php index 72bb16904..ac8b11da7 100644 --- a/tests/e2e/Adapter/Scopes/IndexTests.php +++ b/tests/e2e/Adapter/Scopes/IndexTests.php @@ -165,7 +165,8 @@ public function testIndexValidation(): void $validator = new Index( $attributes, $database->getAdapter()->getMaxIndexLength(), - $database->getAdapter()->getInternalIndexesKeys() + $database->getAdapter()->getInternalIndexesKeys(), + $database->getAdapter()->getSupportForIndexArray() ); $errorMessage = 'Index length 701 is larger than the size for title1: 700"'; @@ -239,7 +240,8 @@ public function testIndexValidation(): void $validator = new Index( $attributes, $database->getAdapter()->getMaxIndexLength(), - $database->getAdapter()->getInternalIndexesKeys() + $database->getAdapter()->getInternalIndexesKeys(), + $database->getAdapter()->getSupportForIndexArray() ); $errorMessage = 'Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'; $this->assertFalse($validator->isValid($indexes[0])); @@ -296,6 +298,34 @@ public function testIndexValidation(): void } } + public function testIndexLengthZero(): void + { + /** @var Database $database */ + $database = static::getDatabase(); + + $database->createCollection(__FUNCTION__); + + $database->createAttribute(__FUNCTION__, 'title1', Database::VAR_STRING, 1000, true); + + try { + $database->createIndex(__FUNCTION__, 'index_title1', Database::INDEX_KEY, ['title1'], [0]); + $this->fail('Failed to throw exception'); + } catch (Throwable $e) { + $this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage()); + } + + + $database->createAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, 100, true); + $database->createIndex(__FUNCTION__, 'index_title2', Database::INDEX_KEY, ['title2'], [0]); + + try { + $database->updateAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, 1000, true); + $this->fail('Failed to throw exception'); + } catch (Throwable $e) { + $this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage()); + } + } + public function testRenameIndex(): void { $database = static::getDatabase();