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 1cacd52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,13 @@ public function __construct(Connection $connection, $collection)
*/
public function index($columns = null, $name = null, $algorithm = null, $options = [])
{
$columns = $this->fluent($columns);

// Columns are passed as a default array.
if (is_array($columns) && is_int(key($columns))) {
// Transform the columns to the required array format.
$transform = [];

foreach ($columns as $column) {
$transform[$column] = 1;
}

$columns = $transform;
}
$indexOrColumns = $this->transformColumns($columns);

if ($name !== null) {
$options['name'] = $name;
}

$this->collection->createIndex($columns, $options);
$this->collection->createIndex($indexOrColumns, $options);

return $this;
}
Expand Down Expand Up @@ -129,8 +117,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', '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', '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 1cacd52

Please sign in to comment.