Repository navigation
fix(index): read a stored index that covers a deleted attribute #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+92
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Indexes; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Database\Attribute; | ||
| use Utopia\Database\Collection; | ||
| use Utopia\Database\Document; | ||
| use Utopia\Database\Exception\Index as IndexException; | ||
| use Utopia\Database\Index; | ||
| use Utopia\Query\OrderDirection; | ||
|
|
||
| final class LegacyDroppedAttributeIndexTest extends TestCase | ||
| { | ||
| public function testAFulltextIndexWhoseOnlyAttributeWasDeletedIsReadWithNoAttributes(): void | ||
| { | ||
| $index = Index::fromDocument(new Document(['$id' => 'category', 'key' => 'category', 'type' => 'fulltext', 'attributes' => [null], 'orders' => ['ASC'], 'lengths' => [200]])); | ||
|
|
||
| $this->assertSame('category', $index->key); | ||
| $this->assertSame([], $index->attributes); | ||
| $this->assertSame([], $index->lengths); | ||
| $this->assertSame([], $index->orders); | ||
| } | ||
|
|
||
| public function testAUniqueIndexKeepsTheAttributesItStillCoversWithTheirLengthsAndOrders(): void | ||
| { | ||
| $index = Index::fromDocument(new Document(['$id' => 'index_2', 'key' => 'index_2', 'type' => 'unique', 'attributes' => ['playId', null], 'orders' => ['ASC', 'ASC'], 'lengths' => [null, null]])); | ||
|
|
||
| $this->assertSame(['playId'], $index->attributes); | ||
| $this->assertSame([null], $index->lengths); | ||
| $this->assertSame([OrderDirection::Asc], $index->orders); | ||
| } | ||
|
|
||
| public function testADroppedAttributeBetweenOthersKeepsEachLengthAndOrderAtItsAttribute(): void | ||
| { | ||
| $index = Index::fromDocument(new Document(['$id' => 'mixed', 'key' => 'mixed', 'type' => 'key', 'attributes' => ['a', null, 'c'], 'orders' => ['ASC', 'DESC', 'DESC'], 'lengths' => [10, 20, 30]])); | ||
|
|
||
| $this->assertSame(['a', 'c'], $index->attributes); | ||
| $this->assertSame([10, 30], $index->lengths); | ||
| $this->assertSame([OrderDirection::Asc, OrderDirection::Desc], $index->orders); | ||
| } | ||
|
|
||
| public function testACollectionHoldingSuchAnIndexIsRead(): void | ||
| { | ||
| $collection = Collection::fromDocument(new Document([ | ||
| '$id' => 'plays', | ||
| 'name' => 'plays', | ||
| 'attributes' => [Attribute::string(key: 'playId', size: 36)->toDocument()], | ||
| 'indexes' => [new Document(['$id' => 'index_2', 'key' => 'index_2', 'type' => 'unique', 'attributes' => ['playId', null], 'orders' => ['ASC', 'ASC'], 'lengths' => [null, null]])], | ||
| ])); | ||
|
|
||
| $this->assertSame(['playId'], $collection->indexes()[0]->attributes); | ||
| } | ||
|
|
||
| public function testAnIndexDefinedByACallerStillRefusesAnAttributeThatIsNotAString(): void | ||
| { | ||
| $this->expectException(IndexException::class); | ||
| $this->expectExceptionMessage('Index attributes must be strings'); | ||
|
|
||
| Index::fromArray(['$id' => 'bad', 'type' => 'key', 'attributes' => ['playId', null]]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: utopia-php/database
Length of output: 37492
🏁 Script executed:
Repository: utopia-php/database
Length of output: 40853
🏁 Script executed:
Repository: utopia-php/database
Length of output: 15765
Filter companion arrays by retained attribute positions regardless of their count.
The stored format permits companion arrays shorter than the attribute list. For example,
attributes = ['a', null, 'c']andlengths = [10, 20]uses positional entries. The helper removes the null attribute but leaveslengthsunchanged because its count differs from the original attribute count. The value20then attaches tocinstead of the deleted attribute. Apply the same filtering to every array companion.Suggested fix
🤖 Prompt for AI Agents