Skip to content

Fix drop index #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,22 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio
/**
* @inheritdoc
*/
public function dropIndex($columns = null)
public function dropIndex($indexOrColumns = null)
{
$columns = $this->fluent($columns);
if (is_array($indexOrColumns)) {
$indexOrColumns = $this->fluent($indexOrColumns);

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

foreach ($columns as $column) {
foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
}

$columns = $transform;
$indexOrColumns = join('_', $transform);
}

foreach ($columns as $column) {
$this->collection->dropIndex($column);
}
$this->collection->dropIndex($indexOrColumns);

return $this;
}
Expand Down
30 changes: 29 additions & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testDropIndex()
{
Schema::collection('newcollection', function ($collection) {
$collection->unique('uniquekey');
$collection->dropIndex('uniquekey');
$collection->dropIndex('uniquekey_1');
});

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

$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);

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

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

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

$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');
});

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

Schema::collection('newcollection', function ($collection) {
$collection->dropIndex('custom_index_name');
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertFalse($index);
}

public function testBackground()
Expand Down