From b21fdf4e8f169921a75355aceb62d78c1795d80f Mon Sep 17 00:00:00 2001 From: Ben Argo Date: Wed, 11 May 2016 12:50:18 +0100 Subject: [PATCH 1/2] Adding sparce & unique indices Provides a handy workaround for issue 556. --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 19 +++++++++++++++++++ tests/SchemaTest.php | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 144675b39..207a80c14 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -216,6 +216,25 @@ public function addColumn($type, $name, array $parameters = []) return $this; } + /** + * Specify a sparse and unique index for the collection. + * + * @param string|array $columns + * @param array $options + * @return Blueprint + */ + public function sparse_and_unique($columns = null, $options = []) + { + $columns = $this->fluent($columns); + + $options['sparse'] = true; + $options['unique'] = true; + + $this->index($columns, $options); + + return $this; + } + /** * Allow fluent columns. * diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index fd9650dac..5f28efe63 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -176,6 +176,17 @@ public function testDummies() }); } + public function testSparseUnique() + { + Schema::collection('newcollection', function ($collection) { + $collection->sparse_and_unique('sparseuniquekey'); + }); + + $index = $this->getIndex('newcollection', 'sparseuniquekey'); + $this->assertEquals(1, $index['sparse']); + $this->assertEquals(1, $index['unique']); + } + protected function getIndex($collection, $name) { $collection = DB::getCollection($collection); From a93a53b347717cce6cd204b87b2c662cfa1f2926 Mon Sep 17 00:00:00 2001 From: Ben Argo Date: Wed, 11 May 2016 13:03:51 +0100 Subject: [PATCH 2/2] Code styling fix This addresses the issues highlighted in the StyleCI analysis. --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 10 +++++----- tests/SchemaTest.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 207a80c14..d53336fb4 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -225,14 +225,14 @@ public function addColumn($type, $name, array $parameters = []) */ public function sparse_and_unique($columns = null, $options = []) { - $columns = $this->fluent($columns); + $columns = $this->fluent($columns); - $options['sparse'] = true; - $options['unique'] = true; + $options['sparse'] = true; + $options['unique'] = true; - $this->index($columns, $options); + $this->index($columns, $options); - return $this; + return $this; } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 5f28efe63..ed96e81ff 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -178,13 +178,13 @@ public function testDummies() public function testSparseUnique() { - Schema::collection('newcollection', function ($collection) { + Schema::collection('newcollection', function ($collection) { $collection->sparse_and_unique('sparseuniquekey'); - }); + }); - $index = $this->getIndex('newcollection', 'sparseuniquekey'); - $this->assertEquals(1, $index['sparse']); - $this->assertEquals(1, $index['unique']); + $index = $this->getIndex('newcollection', 'sparseuniquekey'); + $this->assertEquals(1, $index['sparse']); + $this->assertEquals(1, $index['unique']); } protected function getIndex($collection, $name)