Skip to content

Commit 3aad3bd

Browse files
benargojenssegers
authored andcommitted
Fix #556 Adding sparce & unique indices (#835)
* Adding sparce & unique indices Provides a handy workaround for issue 556. * Code styling fix This addresses the issues highlighted in the StyleCI analysis.
1 parent 296fac2 commit 3aad3bd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,25 @@ public function addColumn($type, $name, array $parameters = [])
222222
return $this;
223223
}
224224

225+
/**
226+
* Specify a sparse and unique index for the collection.
227+
*
228+
* @param string|array $columns
229+
* @param array $options
230+
* @return Blueprint
231+
*/
232+
public function sparse_and_unique($columns = null, $options = [])
233+
{
234+
$columns = $this->fluent($columns);
235+
236+
$options['sparse'] = true;
237+
$options['unique'] = true;
238+
239+
$this->index($columns, $options);
240+
241+
return $this;
242+
}
243+
225244
/**
226245
* Allow fluent columns.
227246
*

tests/SchemaTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ public function testDummies()
175175
});
176176
}
177177

178+
public function testSparseUnique()
179+
{
180+
Schema::collection('newcollection', function ($collection) {
181+
$collection->sparse_and_unique('sparseuniquekey');
182+
});
183+
184+
$index = $this->getIndex('newcollection', 'sparseuniquekey');
185+
$this->assertEquals(1, $index['sparse']);
186+
$this->assertEquals(1, $index['unique']);
187+
}
188+
178189
protected function getIndex($collection, $name)
179190
{
180191
$collection = DB::getCollection($collection);

0 commit comments

Comments
 (0)