Skip to content

Commit

Permalink
Update Index Duplicate check to work with ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Jun 8, 2023
1 parent 39143b3 commit e10a336
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/Database/Validator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ public function checkDuplicatedAttributes(Document $collection): bool
{
foreach ($collection->getAttribute('indexes', []) as $index) {
$attributes = $index->getAttribute('attributes', []);
if (count($attributes) !== count(array_unique($attributes))) {
$this->message = 'Duplicate attributes provided';
return false;
$orders = $index->getAttribute('orders', []);
$stack = [];
foreach ($attributes as $key => $attribute) {
$direction = $orders[$key] ?? 'asc';
$value = strtolower($attribute . '|' . $direction);
if (in_array($value, $stack)) {
$this->message = 'Duplicate attributes provided';
return false;
}
$stack[] = $value;
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Database/Validator/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,41 @@ public function testDuplicatedAttributes(): void
$this->assertFalse($validator->isValid($collection));
$this->assertEquals('Duplicate attributes provided', $validator->getDescription());
}

/**
* @throws Exception
*/
public function testDuplicatedAttributesDifferentOrder(): 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' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
])
],
'indexes' => [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_FULLTEXT,
'attributes' => ['title', 'title'],
'lengths' => [],
'orders' => ['asc', 'desc'],
]),
],
]);

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

0 comments on commit e10a336

Please sign in to comment.