From 3f588096544e5de26fbadfb920f0713057bf8026 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Mon, 29 Jul 2019 14:54:22 +0200 Subject: [PATCH 1/2] Add hasIndex and dropIndexIfExists methods --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 60 +++++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 0c01c96aa..551aac1ee 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -78,21 +78,24 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio */ public function dropIndex($indexOrColumns = null) { - if (is_array($indexOrColumns)) { - $indexOrColumns = $this->fluent($indexOrColumns); + $indexOrColumns = $this->transformColumns($indexOrColumns); - // Transform the columns to the index name. - $transform = []; + $this->collection->dropIndex($indexOrColumns); - foreach ($indexOrColumns as $column) { - $transform[$column] = $column . '_1'; - } + return $this; + } - $indexOrColumns = join('_', $transform); + /** + * 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); } - - $this->collection->dropIndex($indexOrColumns); - return $this; } @@ -235,6 +238,41 @@ public function sparse_and_unique($columns = null, $options = []) 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 ($index->getName() == $indexOrColumns) { + return true; + } + } + return false; + } + + /** + * @param string|array $indexOrColumns + * @return string|array + */ + private function transformColumns($indexOrColumns) + { + if (is_array($indexOrColumns)) { + $indexOrColumns = $this->fluent($indexOrColumns); + // Transform the columns to the index name. + $transform = []; + foreach ($indexOrColumns as $column) { + $transform[$column] = $column . '_1'; + } + $indexOrColumns = join('_', $transform); + } + return $indexOrColumns; + } + /** * Allow fluent columns. * From c80d46d0342ff494e08ddf138186badd27f6c969 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 6 Sep 2019 17:37:04 +0200 Subject: [PATCH 2/2] Add hasIndex and dropIndexIfExists methods --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 77 +++++++++++---------- tests/SchemaTest.php | 72 +++++++++++++++++++ 2 files changed, 114 insertions(+), 35 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 551aac1ee..682b7e761 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -99,6 +99,48 @@ public function dropIndexIfExists($indexOrColumns = null) 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); + + // Transform the columns to the index name. + $transform = []; + + foreach ($indexOrColumns as $column) { + $transform[$column] = $column . '_1'; + } + + $indexOrColumns = implode('_', $transform); + } + return $indexOrColumns; + } + /** * @inheritdoc */ @@ -238,41 +280,6 @@ public function sparse_and_unique($columns = null, $options = []) 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 ($index->getName() == $indexOrColumns) { - return true; - } - } - return false; - } - - /** - * @param string|array $indexOrColumns - * @return string|array - */ - private function transformColumns($indexOrColumns) - { - if (is_array($indexOrColumns)) { - $indexOrColumns = $this->fluent($indexOrColumns); - // Transform the columns to the index name. - $transform = []; - foreach ($indexOrColumns as $column) { - $transform[$column] = $column . '_1'; - } - $indexOrColumns = join('_', $transform); - } - return $indexOrColumns; - } - /** * Allow fluent columns. * diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index b56cc639c..006654bf6 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -1,5 +1,7 @@ assertFalse($index); } + public function testDropIndexIfExists() + { + 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() + { + $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() { Schema::collection('newcollection', function ($collection) {