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 ;
@@ -81,21 +82,21 @@ public function testIndex(): void
81
82
$ collection ->index ('mykey1 ' );
82
83
});
83
84
84
- $ index = $ this ->getIndex ('newcollection ' , 'mykey1 ' );
85
+ $ index = $ this ->getIndex ('newcollection ' , 'mykey1_1 ' );
85
86
$ this ->assertEquals (1 , $ index ['key ' ]['mykey1 ' ]);
86
87
87
88
Schema::table ('newcollection ' , function ($ collection ) {
88
89
$ collection ->index (['mykey2 ' ]);
89
90
});
90
91
91
- $ index = $ this ->getIndex ('newcollection ' , 'mykey2 ' );
92
+ $ index = $ this ->getIndex ('newcollection ' , 'mykey2_1 ' );
92
93
$ this ->assertEquals (1 , $ index ['key ' ]['mykey2 ' ]);
93
94
94
95
Schema::table ('newcollection ' , function ($ collection ) {
95
96
$ collection ->string ('mykey3 ' )->index ();
96
97
});
97
98
98
- $ index = $ this ->getIndex ('newcollection ' , 'mykey3 ' );
99
+ $ index = $ this ->getIndex ('newcollection ' , 'mykey3_1 ' );
99
100
$ this ->assertEquals (1 , $ index ['key ' ]['mykey3 ' ]);
100
101
}
101
102
@@ -105,7 +106,7 @@ public function testPrimary(): void
105
106
$ collection ->string ('mykey ' , 100 )->primary ();
106
107
});
107
108
108
- $ index = $ this ->getIndex ('newcollection ' , 'mykey ' );
109
+ $ index = $ this ->getIndex ('newcollection ' , 'mykey_1 ' );
109
110
$ this ->assertEquals (1 , $ index ['unique ' ]);
110
111
}
111
112
@@ -115,7 +116,7 @@ public function testUnique(): void
115
116
$ collection ->unique ('uniquekey ' );
116
117
});
117
118
118
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
119
+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
119
120
$ this ->assertEquals (1 , $ index ['unique ' ]);
120
121
}
121
122
@@ -126,15 +127,15 @@ public function testDropIndex(): void
126
127
$ collection ->dropIndex ('uniquekey_1 ' );
127
128
});
128
129
129
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
130
+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
130
131
$ this ->assertEquals (null , $ index );
131
132
132
133
Schema::table ('newcollection ' , function ($ collection ) {
133
134
$ collection ->unique ('uniquekey ' );
134
135
$ collection ->dropIndex (['uniquekey ' ]);
135
136
});
136
137
137
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
138
+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
138
139
$ this ->assertEquals (null , $ index );
139
140
140
141
Schema::table ('newcollection ' , function ($ collection ) {
@@ -149,35 +150,37 @@ public function testDropIndex(): void
149
150
});
150
151
151
152
$ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
152
- $ this ->assertFalse ($ index );
153
+ $ this ->assertNull ($ index );
153
154
155
+ $ indexName = 'field_a_-1_field_b_1 ' ;
154
156
Schema::table ('newcollection ' , function ($ collection ) {
155
157
$ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
156
158
});
157
159
158
- $ index = $ this ->getIndex ('newcollection ' , ' field_a_-1_field_b_1 ' );
160
+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
159
161
$ this ->assertNotNull ($ index );
160
162
161
163
Schema::table ('newcollection ' , function ($ collection ) {
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 ' , $ indexName );
168
+ $ this ->assertNull ($ index );
167
169
168
- Schema::table ('newcollection ' , function ($ collection ) {
169
- $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
170
+ $ indexName = 'custom_index_name ' ;
171
+ Schema::table ('newcollection ' , function ($ collection ) use ($ indexName ) {
172
+ $ collection ->index (['field_a ' , 'field_b ' ], $ indexName );
170
173
});
171
174
172
- $ index = $ this ->getIndex ('newcollection ' , ' custom_index_name ' );
175
+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
173
176
$ this ->assertNotNull ($ index );
174
177
175
- Schema::table ('newcollection ' , function ($ collection ) {
176
- $ collection ->dropIndex (' custom_index_name ' );
178
+ Schema::table ('newcollection ' , function ($ collection ) use ( $ indexName ) {
179
+ $ collection ->dropIndex ($ indexName );
177
180
});
178
181
179
- $ index = $ this ->getIndex ('newcollection ' , ' custom_index_name ' );
180
- $ this ->assertFalse ($ index );
182
+ $ index = $ this ->getIndex ('newcollection ' , $ indexName );
183
+ $ this ->assertNull ($ index );
181
184
}
182
185
183
186
public function testDropIndexIfExists (): void
@@ -210,7 +213,7 @@ public function testDropIndexIfExists(): void
210
213
});
211
214
212
215
$ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
213
- $ this ->assertFalse ($ index );
216
+ $ this ->assertNull ($ index );
214
217
215
218
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
216
219
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -224,7 +227,7 @@ public function testDropIndexIfExists(): void
224
227
});
225
228
226
229
$ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
227
- $ this ->assertFalse ($ index );
230
+ $ this ->assertNull ($ index );
228
231
}
229
232
230
233
public function testHasIndex (): void
@@ -256,7 +259,8 @@ public function testSparse(): void
256
259
$ collection ->sparse ('sparsekey ' );
257
260
});
258
261
259
- $ index = $ this ->getIndex ('newcollection ' , 'sparsekey ' );
262
+ $ index = $ this ->getIndex ('newcollection ' , 'sparsekey_1 ' );
263
+ $ this ->assertNotNull ($ index );
260
264
$ this ->assertEquals (1 , $ index ['sparse ' ]);
261
265
}
262
266
@@ -266,7 +270,8 @@ public function testExpire(): void
266
270
$ collection ->expire ('expirekey ' , 60 );
267
271
});
268
272
269
- $ index = $ this ->getIndex ('newcollection ' , 'expirekey ' );
273
+ $ index = $ this ->getIndex ('newcollection ' , 'expirekey_1 ' );
274
+ $ this ->assertNotNull ($ index );
270
275
$ this ->assertEquals (60 , $ index ['expireAfterSeconds ' ]);
271
276
}
272
277
@@ -280,7 +285,8 @@ public function testSoftDeletes(): void
280
285
$ collection ->string ('email ' )->nullable ()->index ();
281
286
});
282
287
283
- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
288
+ $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
289
+ $ this ->assertNotNull ($ index );
284
290
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
285
291
}
286
292
@@ -292,10 +298,12 @@ public function testFluent(): void
292
298
$ collection ->timestamp ('created_at ' );
293
299
});
294
300
295
- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
301
+ $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
302
+ $ this ->assertNotNull ($ index );
296
303
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
297
304
298
- $ index = $ this ->getIndex ('newcollection ' , 'token ' );
305
+ $ index = $ this ->getIndex ('newcollection ' , 'token_1 ' );
306
+ $ this ->assertNotNull ($ index );
299
307
$ this ->assertEquals (1 , $ index ['key ' ]['token ' ]);
300
308
}
301
309
@@ -307,13 +315,16 @@ public function testGeospatial(): void
307
315
$ collection ->geospatial ('continent ' , '2dsphere ' );
308
316
});
309
317
310
- $ index = $ this ->getIndex ('newcollection ' , 'point ' );
318
+ $ index = $ this ->getIndex ('newcollection ' , 'point_2d ' );
319
+ $ this ->assertNotNull ($ index );
311
320
$ this ->assertEquals ('2d ' , $ index ['key ' ]['point ' ]);
312
321
313
- $ index = $ this ->getIndex ('newcollection ' , 'area ' );
322
+ $ index = $ this ->getIndex ('newcollection ' , 'area_2d ' );
323
+ $ this ->assertNotNull ($ index );
314
324
$ this ->assertEquals ('2d ' , $ index ['key ' ]['area ' ]);
315
325
316
- $ index = $ this ->getIndex ('newcollection ' , 'continent ' );
326
+ $ index = $ this ->getIndex ('newcollection ' , 'continent_2dsphere ' );
327
+ $ this ->assertNotNull ($ index );
317
328
$ this ->assertEquals ('2dsphere ' , $ index ['key ' ]['continent ' ]);
318
329
}
319
330
@@ -332,7 +343,8 @@ public function testSparseUnique(): void
332
343
$ collection ->sparse_and_unique ('sparseuniquekey ' );
333
344
});
334
345
335
- $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey ' );
346
+ $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey_1 ' );
347
+ $ this ->assertNotNull ($ index );
336
348
$ this ->assertEquals (1 , $ index ['sparse ' ]);
337
349
$ this ->assertEquals (1 , $ index ['unique ' ]);
338
350
}
@@ -573,23 +585,23 @@ public function testVectorSearchIndex()
573
585
self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574
586
}
575
587
576
- protected function getIndex (string $ collection , string $ name )
588
+ protected function getIndex (string $ collection , string $ name ): ? IndexInfo
577
589
{
578
- $ collection = DB :: getCollection ($ collection );
590
+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
579
591
assert ($ collection instanceof Collection);
580
592
581
593
foreach ($ collection ->listIndexes () as $ index ) {
582
- if (isset ( $ index[ ' key ' ][ $ name]) ) {
594
+ if ($ index-> getName () === $ name ) {
583
595
return $ index ;
584
596
}
585
597
}
586
598
587
- return false ;
599
+ return null ;
588
600
}
589
601
590
602
protected function getSearchIndex (string $ collection , string $ name ): ?array
591
603
{
592
- $ collection = DB :: getCollection ($ collection );
604
+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
593
605
assert ($ collection instanceof Collection);
594
606
595
607
foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
0 commit comments