Skip to content

Commit b743f4a

Browse files
committed
Index Length 0
1 parent a1be186 commit b743f4a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Database/Validator/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function checkIndexLength(Document $index): bool
196196
switch ($attribute->getAttribute('type')) {
197197
case Database::VAR_STRING:
198198
$attributeSize = $attribute->getAttribute('size', 0);
199-
$indexLength = $lengths[$attributePosition] ?? $attributeSize;
199+
$indexLength = !empty($lengths[$attributePosition]) ? $lengths[$attributePosition] : $attributeSize;
200200
break;
201201
case Database::VAR_FLOAT:
202202
$attributeSize = 2; // 8 bytes / 4 mb4

tests/e2e/Adapter/Scopes/IndexTests.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public function testIndexValidation(): void
165165
$validator = new Index(
166166
$attributes,
167167
$database->getAdapter()->getMaxIndexLength(),
168-
$database->getAdapter()->getInternalIndexesKeys()
168+
$database->getAdapter()->getInternalIndexesKeys(),
169+
$database->getAdapter()->getSupportForIndexArray()
169170
);
170171

171172
$errorMessage = 'Index length 701 is larger than the size for title1: 700"';
@@ -239,7 +240,8 @@ public function testIndexValidation(): void
239240
$validator = new Index(
240241
$attributes,
241242
$database->getAdapter()->getMaxIndexLength(),
242-
$database->getAdapter()->getInternalIndexesKeys()
243+
$database->getAdapter()->getInternalIndexesKeys(),
244+
$database->getAdapter()->getSupportForIndexArray()
243245
);
244246
$errorMessage = 'Attribute "integer" cannot be part of a FULLTEXT index, must be of type string';
245247
$this->assertFalse($validator->isValid($indexes[0]));
@@ -296,6 +298,34 @@ public function testIndexValidation(): void
296298
}
297299
}
298300

301+
public function testIndexLengthZero(): void
302+
{
303+
/** @var Database $database */
304+
$database = static::getDatabase();
305+
306+
$database->createCollection(__FUNCTION__);
307+
308+
$database->createAttribute(__FUNCTION__, 'title1', Database::VAR_STRING, 1000, true);
309+
310+
try {
311+
$database->createIndex(__FUNCTION__, 'index_title1', Database::INDEX_KEY, ['title1'], [0]);
312+
$this->fail('Failed to throw exception');
313+
} catch (Throwable $e) {
314+
$this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage());
315+
}
316+
317+
318+
$database->createAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, 100, true);
319+
$database->createIndex(__FUNCTION__, 'index_title2', Database::INDEX_KEY, ['title2'], [0]);
320+
321+
try {
322+
$database->updateAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, 1000, true);
323+
$this->fail('Failed to throw exception');
324+
} catch (Throwable $e) {
325+
$this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage());
326+
}
327+
}
328+
299329
public function testRenameIndex(): void
300330
{
301331
$database = static::getDatabase();

0 commit comments

Comments
 (0)