11
11
use MongoDB \Collection ;
12
12
use MongoDB \Database ;
13
13
use MongoDB \Laravel \Schema \Blueprint ;
14
+ use MongoDB \Model \IndexInfo ;
14
15
15
16
use function assert ;
16
17
use function collect ;
17
18
use function count ;
19
+ use function str_starts_with ;
18
20
19
21
class SchemaTest extends TestCase
20
22
{
@@ -149,7 +151,7 @@ public function testDropIndex(): void
149
151
});
150
152
151
153
$ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
152
- $ this ->assertFalse ($ index );
154
+ $ this ->assertNull ($ index );
153
155
154
156
Schema::table ('newcollection ' , function ($ collection ) {
155
157
$ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
@@ -162,8 +164,8 @@ public function testDropIndex(): void
162
164
$ collection ->dropIndex (['field_a ' => -1 , 'field_b ' => 1 ]);
163
165
});
164
166
165
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_-1_field_b_1 ' );
166
- $ this ->assertFalse ($ index );
167
+ $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
168
+ $ this ->assertNull ($ index );
167
169
168
170
Schema::table ('newcollection ' , function ($ collection ) {
169
171
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -177,7 +179,7 @@ public function testDropIndex(): void
177
179
});
178
180
179
181
$ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
180
- $ this ->assertFalse ($ index );
182
+ $ this ->assertNull ($ index );
181
183
}
182
184
183
185
public function testDropIndexIfExists (): void
@@ -210,7 +212,7 @@ public function testDropIndexIfExists(): void
210
212
});
211
213
212
214
$ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
213
- $ this ->assertFalse ($ index );
215
+ $ this ->assertNull ($ index );
214
216
215
217
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
216
218
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -224,7 +226,7 @@ public function testDropIndexIfExists(): void
224
226
});
225
227
226
228
$ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
227
- $ this ->assertFalse ($ index );
229
+ $ this ->assertNull ($ index );
228
230
}
229
231
230
232
public function testHasIndex (): void
@@ -257,6 +259,7 @@ public function testSparse(): void
257
259
});
258
260
259
261
$ index = $ this ->getIndex ('newcollection ' , 'sparsekey ' );
262
+ $ this ->assertNotNull ($ index );
260
263
$ this ->assertEquals (1 , $ index ['sparse ' ]);
261
264
}
262
265
@@ -573,23 +576,24 @@ public function testVectorSearchIndex()
573
576
self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574
577
}
575
578
576
- protected function getIndex (string $ collection , string $ name )
579
+ /** MongoDB generates index names by concatenating the key field names and an incrementing integer. */
580
+ protected function getIndex (string $ collection , string $ name ): ?IndexInfo
577
581
{
578
- $ collection = DB :: getCollection ($ collection );
582
+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
579
583
assert ($ collection instanceof Collection);
580
584
581
585
foreach ($ collection ->listIndexes () as $ index ) {
582
- if (isset ($ index[ ' key ' ][ $ name] )) {
586
+ if (str_starts_with ($ index-> getName (), $ name )) {
583
587
return $ index ;
584
588
}
585
589
}
586
590
587
- return false ;
591
+ return null ;
588
592
}
589
593
590
594
protected function getSearchIndex (string $ collection , string $ name ): ?array
591
595
{
592
- $ collection = DB :: getCollection ($ collection );
596
+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
593
597
assert ($ collection instanceof Collection);
594
598
595
599
foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
0 commit comments