Skip to content

Commit 1cacd52

Browse files
committed
Fix dropIndex for compound indexes with sorting order
1 parent c55d42d commit 1cacd52

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

+14-16
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,13 @@ public function __construct(Connection $connection, $collection)
3939
*/
4040
public function index($columns = null, $name = null, $algorithm = null, $options = [])
4141
{
42-
$columns = $this->fluent($columns);
43-
44-
// Columns are passed as a default array.
45-
if (is_array($columns) && is_int(key($columns))) {
46-
// Transform the columns to the required array format.
47-
$transform = [];
48-
49-
foreach ($columns as $column) {
50-
$transform[$column] = 1;
51-
}
52-
53-
$columns = $transform;
54-
}
42+
$indexOrColumns = $this->transformColumns($columns);
5543

5644
if ($name !== null) {
5745
$options['name'] = $name;
5846
}
5947

60-
$this->collection->createIndex($columns, $options);
48+
$this->collection->createIndex($indexOrColumns, $options);
6149

6250
return $this;
6351
}
@@ -129,8 +117,18 @@ protected function transformColumns($indexOrColumns)
129117
// Transform the columns to the index name.
130118
$transform = [];
131119

132-
foreach ($indexOrColumns as $column) {
133-
$transform[$column] = $column . '_1';
120+
foreach ($indexOrColumns as $key => $value) {
121+
if (is_int($key)) {
122+
// There is no sorting order, use the default.
123+
$column = $value;
124+
$sorting = '1';
125+
} else {
126+
// This is a column with sorting order e.g 'my_column' => -1.
127+
$column = $key;
128+
$sorting = $value;
129+
}
130+
131+
$transform[$column] = $column . "_" . $sorting;
134132
}
135133

136134
$indexOrColumns = implode('_', $transform);

tests/SchemaTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,34 @@ public function testDropIndex(): void
132132
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
133133
$this->assertFalse($index);
134134

135+
Schema::collection('newcollection', function ($collection) {
136+
$collection->index(['field_a' => -1, 'field_b' => 1]);
137+
});
138+
139+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
140+
$this->assertNotNull($index);
141+
142+
Schema::collection('newcollection', function ($collection) {
143+
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
144+
});
145+
146+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
147+
$this->assertFalse($index);
148+
149+
Schema::collection('newcollection', function ($collection) {
150+
$collection->index(['field_a', 'field_b' => -1]);
151+
});
152+
153+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_-1');
154+
$this->assertNotNull($index);
155+
156+
Schema::collection('newcollection', function ($collection) {
157+
$collection->dropIndex(['field_a', 'field_b' => -1]);
158+
});
159+
160+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_-1');
161+
$this->assertFalse($index);
162+
135163
Schema::collection('newcollection', function ($collection) {
136164
$collection->index(['field_a', 'field_b'], 'custom_index_name');
137165
});

0 commit comments

Comments
 (0)