1111use MongoDB \Collection ;
1212use MongoDB \Database ;
1313use MongoDB \Laravel \Schema \Blueprint ;
14+ use MongoDB \Model \IndexInfo ;
1415
1516use function assert ;
1617use function collect ;
1718use function count ;
19+ use function str_starts_with ;
1820
1921class SchemaTest extends TestCase
2022{
@@ -81,21 +83,21 @@ public function testIndex(): void
8183 $ collection ->index ('mykey1 ' );
8284 });
8385
84- $ index = $ this ->getIndex ('newcollection ' , 'mykey1 ' );
86+ $ index = $ this ->getIndex ('newcollection ' , 'mykey1_1 ' );
8587 $ this ->assertEquals (1 , $ index ['key ' ]['mykey1 ' ]);
8688
8789 Schema::table ('newcollection ' , function ($ collection ) {
8890 $ collection ->index (['mykey2 ' ]);
8991 });
9092
91- $ index = $ this ->getIndex ('newcollection ' , 'mykey2 ' );
93+ $ index = $ this ->getIndex ('newcollection ' , 'mykey2_1 ' );
9294 $ this ->assertEquals (1 , $ index ['key ' ]['mykey2 ' ]);
9395
9496 Schema::table ('newcollection ' , function ($ collection ) {
9597 $ collection ->string ('mykey3 ' )->index ();
9698 });
9799
98- $ index = $ this ->getIndex ('newcollection ' , 'mykey3 ' );
100+ $ index = $ this ->getIndex ('newcollection ' , 'mykey3_1 ' );
99101 $ this ->assertEquals (1 , $ index ['key ' ]['mykey3 ' ]);
100102 }
101103
@@ -105,7 +107,7 @@ public function testPrimary(): void
105107 $ collection ->string ('mykey ' , 100 )->primary ();
106108 });
107109
108- $ index = $ this ->getIndex ('newcollection ' , 'mykey ' );
110+ $ index = $ this ->getIndex ('newcollection ' , 'mykey_1 ' );
109111 $ this ->assertEquals (1 , $ index ['unique ' ]);
110112 }
111113
@@ -115,7 +117,7 @@ public function testUnique(): void
115117 $ collection ->unique ('uniquekey ' );
116118 });
117119
118- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
120+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
119121 $ this ->assertEquals (1 , $ index ['unique ' ]);
120122 }
121123
@@ -126,15 +128,15 @@ public function testDropIndex(): void
126128 $ collection ->dropIndex ('uniquekey_1 ' );
127129 });
128130
129- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
131+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
130132 $ this ->assertEquals (null , $ index );
131133
132134 Schema::table ('newcollection ' , function ($ collection ) {
133135 $ collection ->unique ('uniquekey ' );
134136 $ collection ->dropIndex (['uniquekey ' ]);
135137 });
136138
137- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
139+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
138140 $ this ->assertEquals (null , $ index );
139141
140142 Schema::table ('newcollection ' , function ($ collection ) {
@@ -149,35 +151,37 @@ public function testDropIndex(): void
149151 });
150152
151153 $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
152- $ this ->assertFalse ($ index );
154+ $ this ->assertNull ($ index );
153155
156+ $ indexName = 'field_a_-1_field_b_1 ' ;
154157 Schema::table ('newcollection ' , function ($ collection ) {
155158 $ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
156159 });
157160
158- $ index = $ this ->getIndex ('newcollection ' , ' field_a_-1_field_b_1 ' );
161+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
159162 $ this ->assertNotNull ($ index );
160163
161164 Schema::table ('newcollection ' , function ($ collection ) {
162165 $ collection ->dropIndex (['field_a ' => -1 , 'field_b ' => 1 ]);
163166 });
164167
165- $ index = $ this ->getIndex ('newcollection ' , ' field_a_-1_field_b_1 ' );
166- $ this ->assertFalse ($ index );
168+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
169+ $ this ->assertNull ($ index );
167170
168- Schema::table ('newcollection ' , function ($ collection ) {
169- $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
171+ $ indexName = 'custom_index_name ' ;
172+ Schema::table ('newcollection ' , function ($ collection ) use ($ indexName ) {
173+ $ collection ->index (['field_a ' , 'field_b ' ], $ indexName );
170174 });
171175
172- $ index = $ this ->getIndex ('newcollection ' , ' custom_index_name ' );
176+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
173177 $ this ->assertNotNull ($ index );
174178
175- Schema::table ('newcollection ' , function ($ collection ) {
176- $ collection ->dropIndex (' custom_index_name ' );
179+ Schema::table ('newcollection ' , function ($ collection ) use ( $ indexName ) {
180+ $ collection ->dropIndex ($ indexName );
177181 });
178182
179- $ index = $ this ->getIndex ('newcollection ' , ' custom_index_name ' );
180- $ this ->assertFalse ($ index );
183+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
184+ $ this ->assertNull ($ index );
181185 }
182186
183187 public function testDropIndexIfExists (): void
@@ -210,7 +214,7 @@ public function testDropIndexIfExists(): void
210214 });
211215
212216 $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
213- $ this ->assertFalse ($ index );
217+ $ this ->assertNull ($ index );
214218
215219 Schema::table ('newcollection ' , function (Blueprint $ collection ) {
216220 $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -224,7 +228,7 @@ public function testDropIndexIfExists(): void
224228 });
225229
226230 $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
227- $ this ->assertFalse ($ index );
231+ $ this ->assertNull ($ index );
228232 }
229233
230234 public function testHasIndex (): void
@@ -256,7 +260,8 @@ public function testSparse(): void
256260 $ collection ->sparse ('sparsekey ' );
257261 });
258262
259- $ index = $ this ->getIndex ('newcollection ' , 'sparsekey ' );
263+ $ index = $ this ->getIndex ('newcollection ' , 'sparsekey_1 ' );
264+ $ this ->assertNotNull ($ index );
260265 $ this ->assertEquals (1 , $ index ['sparse ' ]);
261266 }
262267
@@ -266,7 +271,8 @@ public function testExpire(): void
266271 $ collection ->expire ('expirekey ' , 60 );
267272 });
268273
269- $ index = $ this ->getIndex ('newcollection ' , 'expirekey ' );
274+ $ index = $ this ->getIndex ('newcollection ' , 'expirekey_1 ' );
275+ $ this ->assertNotNull ($ index );
270276 $ this ->assertEquals (60 , $ index ['expireAfterSeconds ' ]);
271277 }
272278
@@ -280,7 +286,8 @@ public function testSoftDeletes(): void
280286 $ collection ->string ('email ' )->nullable ()->index ();
281287 });
282288
283- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
289+ $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
290+ $ this ->assertNotNull ($ index );
284291 $ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
285292 }
286293
@@ -292,10 +299,12 @@ public function testFluent(): void
292299 $ collection ->timestamp ('created_at ' );
293300 });
294301
295- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
302+ $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
303+ $ this ->assertNotNull ($ index );
296304 $ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
297305
298- $ index = $ this ->getIndex ('newcollection ' , 'token ' );
306+ $ index = $ this ->getIndex ('newcollection ' , 'token_1 ' );
307+ $ this ->assertNotNull ($ index );
299308 $ this ->assertEquals (1 , $ index ['key ' ]['token ' ]);
300309 }
301310
@@ -307,13 +316,16 @@ public function testGeospatial(): void
307316 $ collection ->geospatial ('continent ' , '2dsphere ' );
308317 });
309318
310- $ index = $ this ->getIndex ('newcollection ' , 'point ' );
319+ $ index = $ this ->getIndex ('newcollection ' , 'point_2d ' );
320+ $ this ->assertNotNull ($ index );
311321 $ this ->assertEquals ('2d ' , $ index ['key ' ]['point ' ]);
312322
313- $ index = $ this ->getIndex ('newcollection ' , 'area ' );
323+ $ index = $ this ->getIndex ('newcollection ' , 'area_2d ' );
324+ $ this ->assertNotNull ($ index );
314325 $ this ->assertEquals ('2d ' , $ index ['key ' ]['area ' ]);
315326
316- $ index = $ this ->getIndex ('newcollection ' , 'continent ' );
327+ $ index = $ this ->getIndex ('newcollection ' , 'continent_2dsphere ' );
328+ $ this ->assertNotNull ($ index );
317329 $ this ->assertEquals ('2dsphere ' , $ index ['key ' ]['continent ' ]);
318330 }
319331
@@ -332,7 +344,8 @@ public function testSparseUnique(): void
332344 $ collection ->sparse_and_unique ('sparseuniquekey ' );
333345 });
334346
335- $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey ' );
347+ $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey_1 ' );
348+ $ this ->assertNotNull ($ index );
336349 $ this ->assertEquals (1 , $ index ['sparse ' ]);
337350 $ this ->assertEquals (1 , $ index ['unique ' ]);
338351 }
@@ -573,23 +586,24 @@ public function testVectorSearchIndex()
573586 self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574587 }
575588
576- protected function getIndex (string $ collection , string $ name )
589+ /** MongoDB generates index names by concatenating the key field names and an incrementing integer. */
590+ protected function getIndex (string $ collection , string $ name ): ?IndexInfo
577591 {
578- $ collection = DB :: getCollection ($ collection );
592+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
579593 assert ($ collection instanceof Collection);
580594
581595 foreach ($ collection ->listIndexes () as $ index ) {
582- if (isset ( $ index[ ' key ' ][ $ name]) ) {
596+ if ($ index-> getName () === $ name ) {
583597 return $ index ;
584598 }
585599 }
586600
587- return false ;
601+ return null ;
588602 }
589603
590604 protected function getSearchIndex (string $ collection , string $ name ): ?array
591605 {
592- $ collection = DB :: getCollection ($ collection );
606+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
593607 assert ($ collection instanceof Collection);
594608
595609 foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
0 commit comments