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