diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 407e333de4ade41c0c160b471020df86cdf966c8..063d5e9e93b27e0f5e4adf8f3ffd2f973f7c8c39 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -74,6 +74,54 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint * @inheritdoc */ public function dropIndex($indexOrColumns = null) + { + $indexOrColumns = $this->transformColumns($indexOrColumns); + + $this->collection->dropIndex($indexOrColumns); + + return $this; + } + + /** + * Indicate that the given index should be dropped, but do not fail if it didn't exist. + * + * @param string|array $indexOrColumns + * @return Blueprint + */ + public function dropIndexIfExists($indexOrColumns = null) + { + if ($this->hasIndex($indexOrColumns)) { + $this->dropIndex($indexOrColumns); + } + return $this; + } + + /** + * Check whether the given index exists. + * + * @param string|array $indexOrColumns + * @return bool + */ + public function hasIndex($indexOrColumns = null) + { + $indexOrColumns = $this->transformColumns($indexOrColumns); + foreach ($this->collection->listIndexes() as $index) { + if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) { + return true; + } + + if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) { + return true; + } + } + return false; + } + + /** + * @param string|array $indexOrColumns + * @return string + */ + protected function transformColumns($indexOrColumns) { if (is_array($indexOrColumns)) { $indexOrColumns = $this->fluent($indexOrColumns); @@ -85,12 +133,9 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint $transform[$column] = $column . '_1'; } - $indexOrColumns = join('_', $transform); + $indexOrColumns = implode('_', $transform); } - - $this->collection->dropIndex($indexOrColumns); - - return $this; + return $indexOrColumns; } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index e3ca81976743f1c35126bb537629c8365d3d5017..147cf8b9635990e932aba19fe014496c416a6f58 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -147,6 +147,76 @@ class SchemaTest extends TestCase $this->assertFalse($index); } + public function testDropIndexIfExists(): void + { + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->unique('uniquekey'); + $collection->dropIndexIfExists('uniquekey_1'); + }); + + $index = $this->getIndex('newcollection', 'uniquekey'); + $this->assertEquals(null, $index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->unique('uniquekey'); + $collection->dropIndexIfExists(['uniquekey']); + }); + + $index = $this->getIndex('newcollection', 'uniquekey'); + $this->assertEquals(null, $index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->index(['field_a', 'field_b']); + }); + + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); + $this->assertNotNull($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->dropIndexIfExists(['field_a', 'field_b']); + }); + + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); + $this->assertFalse($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->index(['field_a', 'field_b'], 'custom_index_name'); + }); + + $index = $this->getIndex('newcollection', 'custom_index_name'); + $this->assertNotNull($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->dropIndexIfExists('custom_index_name'); + }); + + $index = $this->getIndex('newcollection', 'custom_index_name'); + $this->assertFalse($index); + } + + public function testHasIndex(): void + { + $instance = $this; + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index('myhaskey1'); + $instance->assertTrue($collection->hasIndex('myhaskey1_1')); + $instance->assertFalse($collection->hasIndex('myhaskey1')); + }); + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index('myhaskey2'); + $instance->assertTrue($collection->hasIndex(['myhaskey2'])); + $instance->assertFalse($collection->hasIndex(['myhaskey2_1'])); + }); + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index(['field_a', 'field_b']); + $instance->assertTrue($collection->hasIndex(['field_a_1_field_b'])); + $instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1'])); + }); + } + public function testBackground(): void { Schema::collection('newcollection', function ($collection) {