Skip to content

Commit

Permalink
Merge pull request utopia-php#283 from utopia-php/fix-index-validation
Browse files Browse the repository at this point in the history
Fix second index validation when first index is fulltext
  • Loading branch information
abnegate authored Jun 15, 2023
2 parents 626918a + 1377230 commit 4035d3f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Database/Validator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function checkIndexLength(Document $collection): bool
{
foreach ($collection->getAttribute('indexes', []) as $index) {
if ($index->getAttribute('type') === Database::INDEX_FULLTEXT) {
return true;
continue;
}

$total = 0;
Expand Down
55 changes: 55 additions & 0 deletions tests/Database/Validator/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,61 @@ public function testIndexLength(): void
$this->assertEquals('Index length is longer than the maximum: 768', $validator->getDescription());
}

/**
* @throws Exception
*/
public function testMultipleIndexLength(): void
{
$validator = new Index(768);

$collection = new Document([
'$id' => ID::custom('test'),
'name' => 'test',
'attributes' => [
new Document([
'$id' => ID::custom('title'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 256,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
]),
new Document([
'$id' => ID::custom('description'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 1024,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
]),
],
'indexes' => [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_FULLTEXT,
'attributes' => ['title'],
]),
],
]);

$this->assertTrue($validator->isValid($collection));

$collection->setAttribute('indexes', new Document([
'$id' => ID::custom('index2'),
'type' => Database::INDEX_KEY,
'attributes' => ['title', 'description'],
]), Document::SET_TYPE_APPEND);

$this->assertFalse($validator->isValid($collection));
$this->assertEquals('Index length is longer than the maximum: 768', $validator->getDescription());
}

/**
* @throws Exception
*/
Expand Down

0 comments on commit 4035d3f

Please sign in to comment.