Skip to content

Commit 9e88fe5

Browse files
make hasIndex order-sensative (#49840)
1 parent d79ef09 commit 9e88fe5

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -403,27 +403,15 @@ public function hasIndex($table, $index, $type = null)
403403
{
404404
$type = is_null($type) ? $type : strtolower($type);
405405

406-
if (is_array($index)) {
407-
sort($index);
408-
}
409-
410406
foreach ($this->getIndexes($table) as $value) {
411407
$typeMatches = is_null($type)
412408
|| ($type === 'primary' && $value['primary'])
413409
|| ($type === 'unique' && $value['unique'])
414410
|| $type === $value['type'];
415411

416-
if ($value['name'] === $index && $typeMatches) {
412+
if (($value['name'] === $index || $value['columns'] === $index) && $typeMatches) {
417413
return true;
418414
}
419-
420-
if (is_array($index)) {
421-
sort($value['columns']);
422-
423-
if ($value['columns'] === $index && $typeMatches) {
424-
return true;
425-
}
426-
}
427415
}
428416

429417
return false;

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ public function testGetUniqueIndexes()
288288
));
289289
$this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique'));
290290
$this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique', 'unique'));
291-
$this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz']));
292-
$this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'], 'unique'));
293-
$this->assertFalse(Schema::hasIndex('foo', ['bar', 'baz'], 'primary'));
291+
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar']));
292+
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'], 'unique'));
293+
$this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'primary'));
294294
}
295295

296296
public function testGetIndexesWithCompositeKeys()
@@ -335,6 +335,26 @@ public function testGetFullTextIndexes()
335335
$this->assertTrue(collect($indexes)->contains('name', 'articles_body_title_fulltext'));
336336
}
337337

338+
public function testHasIndexOrder()
339+
{
340+
Schema::create('foo', function (Blueprint $table) {
341+
$table->integer('bar');
342+
$table->integer('baz');
343+
$table->integer('qux');
344+
345+
$table->unique(['bar', 'baz']);
346+
$table->index(['baz', 'bar']);
347+
$table->index(['baz', 'qux']);
348+
});
349+
350+
$this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz']));
351+
$this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'], 'unique'));
352+
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar']));
353+
$this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'unique'));
354+
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'qux']));
355+
$this->assertFalse(Schema::hasIndex('foo', ['qux', 'baz']));
356+
}
357+
338358
public function testGetForeignKeys()
339359
{
340360
Schema::create('users', function (Blueprint $table) {

0 commit comments

Comments
 (0)