Commit 3aad3bdd authored by Ben Argo's avatar Ben Argo Committed by Jens Segers

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.
parent 296fac28
......@@ -222,6 +222,25 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
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.
*
......
......@@ -175,6 +175,17 @@ class SchemaTest extends TestCase
});
}
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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment