Skip to content

Commit

Permalink
Merge pull request #1885 from mauri870/drop-compound-index
Browse files Browse the repository at this point in the history
Fix dropIndex for compound indexes with sorting order
  • Loading branch information
Smolevich committed Feb 8, 2020
2 parents c6313cb + 9b062ac commit 4d81b40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
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
14 changes: 14 additions & 0 deletions tests/SchemaTest.php
Expand Up @@ -132,6 +132,20 @@ 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', 'field_b'], 'custom_index_name');
});
Expand Down

0 comments on commit 4d81b40

Please sign in to comment.