Skip to content

New way for drop index #1459

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

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 14 additions & 9 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,24 @@ public function dropIndex($columns = null)
{
$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 = [];
$listOfCurrentIndexes = [];
foreach ($this->collection->listIndexes() as $currentIndexName) {
$listOfCurrentIndexes[] = $currentIndexName['name'];
}

foreach ($columns as $column) {
$transform[$column] = $column . '_1';
//Filter columns with exists indexes
$existsIndexes = [];
foreach ($columns as $indexName) {
if (in_array($indexName, $listOfCurrentIndexes)) {
$existsIndexes[] = $indexName;
}
$postFixedIndexName = $indexName . '_1';
if (in_array($postFixedIndexName, $listOfCurrentIndexes)) {
$existsIndexes[] = $postFixedIndexName;
}

$columns = $transform;
}

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

Expand Down
11 changes: 11 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public function testDropIndex()

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

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

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

public function testBackground()
Expand Down