Skip to content

Commit 158e3b6

Browse files
committed
Fix drop index
Fixes bug that prevents dropping compound indexes with multiple fields. Indexes can now be dropped by name or column list, adhering to the Illuminate Blueprint interface.
1 parent 854cf7e commit 158e3b6

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,22 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio
7676
/**
7777
* @inheritdoc
7878
*/
79-
public function dropIndex($columns = null)
79+
public function dropIndex($indexOrColumns = null)
8080
{
81-
$columns = $this->fluent($columns);
81+
if (is_array($indexOrColumns)) {
82+
$indexOrColumns = $this->fluent($indexOrColumns);
8283

83-
// Columns are passed as a default array.
84-
if (is_array($columns) && is_int(key($columns))) {
85-
// Transform the columns to the required array format.
84+
// Transform the columns to the index name.
8685
$transform = [];
8786

88-
foreach ($columns as $column) {
87+
foreach ($indexOrColumns as $column) {
8988
$transform[$column] = $column . '_1';
9089
}
9190

92-
$columns = $transform;
91+
$indexOrColumns = join('_', $transform);
9392
}
9493

95-
foreach ($columns as $column) {
96-
$this->collection->dropIndex($column);
97-
}
94+
$this->collection->dropIndex($indexOrColumns);
9895

9996
return $this;
10097
}

tests/SchemaTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testDropIndex()
9393
{
9494
Schema::collection('newcollection', function ($collection) {
9595
$collection->unique('uniquekey');
96-
$collection->dropIndex('uniquekey');
96+
$collection->dropIndex('uniquekey_1');
9797
});
9898

9999
$index = $this->getIndex('newcollection', 'uniquekey');
@@ -106,6 +106,34 @@ public function testDropIndex()
106106

107107
$index = $this->getIndex('newcollection', 'uniquekey');
108108
$this->assertEquals(null, $index);
109+
110+
Schema::collection('newcollection', function ($collection) {
111+
$collection->index(['field_a', 'field_b']);
112+
});
113+
114+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
115+
$this->assertNotNull($index);
116+
117+
Schema::collection('newcollection', function ($collection) {
118+
$collection->dropIndex(['field_a', 'field_b']);
119+
});
120+
121+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
122+
$this->assertFalse($index);
123+
124+
Schema::collection('newcollection', function ($collection) {
125+
$collection->index(['field_a', 'field_b'], 'custom_index_name');
126+
});
127+
128+
$index = $this->getIndex('newcollection', 'custom_index_name');
129+
$this->assertNotNull($index);
130+
131+
Schema::collection('newcollection', function ($collection) {
132+
$collection->dropIndex('custom_index_name');
133+
});
134+
135+
$index = $this->getIndex('newcollection', 'custom_index_name');
136+
$this->assertFalse($index);
109137
}
110138

111139
public function testBackground()

0 commit comments

Comments
 (0)