Skip to content

Commit

Permalink
Fix dropIndex for compound indexes with sorting order
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Feb 5, 2020
1 parent c55d42d commit b13bc18
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,18 @@ protected function transformColumns($indexOrColumns)
// Transform the columns to the index name.
$transform = [];

foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
foreach ($indexOrColumns as $key => $value) {
if (is_int($key)) {
// There is no sorting order, use the default.
$column = $value;
$sorting = '1';
} else {
// This is a column with sorting order e.g 'my_column' => -1.
$column = $key;
$sorting = $value;
}

$transform[$column] = $column . "_" . $sorting;
}

$indexOrColumns = implode('_', $transform);
Expand Down
28 changes: 28 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ public function testDropIndex(): void
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
$collection->index(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
$collection->index(['field_a' => 1, 'field_b' => -1]);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_-1');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
$collection->dropIndex(['field_a' => 1, 'field_b' => -1]);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_-1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});
Expand Down

0 comments on commit b13bc18

Please sign in to comment.