@@ -147,6 +147,76 @@ public function testDropIndex(): void
147
147
$ this ->assertFalse ($ index );
148
148
}
149
149
150
+ public function testDropIndexIfExists (): void
151
+ {
152
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
153
+ $ collection ->unique ('uniquekey ' );
154
+ $ collection ->dropIndexIfExists ('uniquekey_1 ' );
155
+ });
156
+
157
+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
158
+ $ this ->assertEquals (null , $ index );
159
+
160
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
161
+ $ collection ->unique ('uniquekey ' );
162
+ $ collection ->dropIndexIfExists (['uniquekey ' ]);
163
+ });
164
+
165
+ $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
166
+ $ this ->assertEquals (null , $ index );
167
+
168
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
169
+ $ collection ->index (['field_a ' , 'field_b ' ]);
170
+ });
171
+
172
+ $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
173
+ $ this ->assertNotNull ($ index );
174
+
175
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
176
+ $ collection ->dropIndexIfExists (['field_a ' , 'field_b ' ]);
177
+ });
178
+
179
+ $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
180
+ $ this ->assertFalse ($ index );
181
+
182
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
183
+ $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
184
+ });
185
+
186
+ $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
187
+ $ this ->assertNotNull ($ index );
188
+
189
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) {
190
+ $ collection ->dropIndexIfExists ('custom_index_name ' );
191
+ });
192
+
193
+ $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
194
+ $ this ->assertFalse ($ index );
195
+ }
196
+
197
+ public function testHasIndex (): void
198
+ {
199
+ $ instance = $ this ;
200
+
201
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) use ($ instance ) {
202
+ $ collection ->index ('myhaskey1 ' );
203
+ $ instance ->assertTrue ($ collection ->hasIndex ('myhaskey1_1 ' ));
204
+ $ instance ->assertFalse ($ collection ->hasIndex ('myhaskey1 ' ));
205
+ });
206
+
207
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) use ($ instance ) {
208
+ $ collection ->index ('myhaskey2 ' );
209
+ $ instance ->assertTrue ($ collection ->hasIndex (['myhaskey2 ' ]));
210
+ $ instance ->assertFalse ($ collection ->hasIndex (['myhaskey2_1 ' ]));
211
+ });
212
+
213
+ Schema::collection ('newcollection ' , function (Blueprint $ collection ) use ($ instance ) {
214
+ $ collection ->index (['field_a ' , 'field_b ' ]);
215
+ $ instance ->assertTrue ($ collection ->hasIndex (['field_a_1_field_b ' ]));
216
+ $ instance ->assertFalse ($ collection ->hasIndex (['field_a_1_field_b_1 ' ]));
217
+ });
218
+ }
219
+
150
220
public function testBackground (): void
151
221
{
152
222
Schema::collection ('newcollection ' , function ($ collection ) {
0 commit comments